sendcommand.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* This is an example of how to write a program that sends commands to
  2. * the pertd2 deamon */
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. int main() {
  7. FILE *fifo;
  8. char *fifo_name = "/tmp/pertd2.fifo";
  9. struct stat fifo_stat;
  10. /* Let's get some info on the fifo */
  11. if (stat(fifo_name,&fifo_stat) != 0) {
  12. printf("Unable to stat %s\n",fifo_name);
  13. /* If we can't stat the fifo, it's not there - we are done */
  14. return(1);
  15. }
  16. /* At this point, you can further interrigate the stat struct of the
  17. * fifo to see if you can write to it, etc. Such code is left to
  18. * you do to as an exercise 8-) */
  19. /* Since the fifo exists, let's open it */
  20. if ((fifo = fopen(fifo_name,"a")) == 0) {
  21. printf("Unable to open %s\n",fifo_name);
  22. /* Obviously, we can't write to it if we can't open it.
  23. * Note that we do need to check for existance first. If the
  24. * file doesn't exist, the fopen will create it, which will
  25. * cause problems when pertd2 starts up. */
  26. return(1);
  27. }
  28. /* We can use 1 big printf to print the command */
  29. fprintf(fifo,"line4\n30\n==EOD==\nThis is line 4\n==EOD==\n");
  30. /* Or we can do it with several printf's */
  31. fprintf(fifo,"line2\n");
  32. fprintf(fifo,"30\n");
  33. fprintf(fifo,"==EOD==\n");
  34. fprintf(fifo,"This is line 2\n");
  35. fprintf(fifo,"==EOD==\n");
  36. /* Or by using fputs */
  37. fputs("line3\n",fifo);
  38. fputs("30\n",fifo);
  39. fputs("==EOD==\n",fifo);
  40. fputs("This is line 3\n",fifo);
  41. fputs("==EOD==\n",fifo);
  42. fputs("line1\n",fifo);
  43. fputs("0\n",fifo);
  44. fputs("==EOD==\n",fifo);
  45. fputs("This is a really,\n",fifo);
  46. fputs("really, really, really,\n",fifo);
  47. fputs("really, really, really,\n",fifo);
  48. fputs("really, really, really,\n",fifo);
  49. fputs("really, really, really,\n",fifo);
  50. fputs("really, really, really,\n",fifo);
  51. fputs("really, really, really,\n",fifo);
  52. fputs("really, really, really,\n",fifo);
  53. fputs("really, really, really,\n",fifo);
  54. fputs("really, really, really,\n",fifo);
  55. fputs("really, really, really,\n",fifo);
  56. fputs("really, really, really,\n",fifo);
  57. fputs("really, really, really,\n",fifo);
  58. fputs("really, really, really,\n",fifo);
  59. fputs("really, really, really,\n",fifo);
  60. fputs("really, really, really,\n",fifo);
  61. fputs("really, really, really,\n",fifo);
  62. fputs("really, really, really,\n",fifo);
  63. fputs("really, really, really,\n",fifo);
  64. fputs("really, really, really,\n",fifo);
  65. fputs("really, really, really,\n",fifo);
  66. fputs("really, really, really,\n",fifo);
  67. fputs("really, really, really,\n",fifo);
  68. fputs("really, really, really,\n",fifo);
  69. fputs("really, really, really,\n",fifo);
  70. fputs("really, really, really,\n",fifo);
  71. fputs("really, really, really,\n",fifo);
  72. fputs("really, really, really,\n",fifo);
  73. fputs("really, really, really,\n",fifo);
  74. fputs("really, really, really,\n",fifo);
  75. fputs("really, really, really,\n",fifo);
  76. fputs("really, really, really,\n",fifo);
  77. fputs("really, really, really,\n",fifo);
  78. fputs("really, really, really,\n",fifo);
  79. fputs("really, really, really,\n",fifo);
  80. fputs("really, really, really,\n",fifo);
  81. fputs("really, really, really,\n",fifo);
  82. fputs("really, really, really,\n",fifo);
  83. fputs("really, really, really,\n",fifo);
  84. fputs("really, really, really,\n",fifo);
  85. fputs("long line\n",fifo);
  86. fputs("==EOD==\n",fifo);
  87. fclose(fifo);
  88. }