fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /*---*/
  5. #include <signal.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8.  
  9. static volatile int flagSignalS = 0;
  10.  
  11. void signal_handler(int sig) {
  12. char mes[] = "jumped into the signal handler, aborting.\n";
  13. write(2, mes, sizeof(mes) - 1);
  14. flagSignalS = 1;
  15. }
  16.  
  17. enum sigentry { INIT = 0, DISABLE, ENABLE };
  18. void signal_handling_entry(enum sigentry entry) {
  19. sigset_t set;
  20. static sigset_t defmaskS, restoreS;
  21. struct sigaction act;
  22. switch (entry) {
  23. case INIT:
  24. sigemptyset(&set);
  25. sigaddset(&set, SIGINT);
  26. sigaddset(&set, SIGQUIT);
  27. sigaddset(&set, SIGTERM);
  28. sigaddset(&set, SIGTSTP);
  29. act.sa_handler = signal_handler;
  30. act.sa_flags = set;
  31. sigemptyset(&act.sa_mask);
  32. sigaction(SIGINT, &act, 0);
  33. sigaction(SIGQUIT, &act, 0);
  34. sigaction(SIGTERM, &act, 0);
  35. sigaction(SIGTSTP, &act, 0);
  36. defmaskS = set;
  37. return;
  38.  
  39. case DISABLE:
  40. sigprocmask(SIG_BLOCK, &defmaskS, &restoreS);
  41. return;
  42.  
  43. case ENABLE:
  44. sigprocmask(SIG_SETMASK, &restoreS, 0);
  45. return;
  46.  
  47. default:
  48. return;
  49. }
  50. }
  51.  
  52. struct node {
  53. char *data;
  54. struct node *next;
  55. struct node *prev;
  56. };
  57.  
  58. int push_head(struct node **root, char *data) {
  59. struct node *p;
  60. if ((p = malloc(sizeof(struct node))) != 0) {
  61. p->data = data;
  62. if (*root == 0) {
  63. p->next = p;
  64. p->prev = p;
  65. } else {
  66. p->next = *root;
  67. p->prev = (*root)->prev;
  68. p->prev->next = p;
  69. p->next->prev = p;
  70. }
  71. *root = p;
  72. return 1;
  73. }
  74. return 0;
  75. }
  76.  
  77. int push_tail(struct node **root, char *data) {
  78. struct node *p;
  79. if ((p = malloc(sizeof(struct node))) != 0) {
  80. p->data = data;
  81. if (*root == 0) {
  82. p->next = p;
  83. p->prev = p;
  84. *root = p;
  85. } else {
  86. p->next = *root;
  87. p->prev = (*root)->prev;
  88. p->prev->next = p;
  89. p->next->prev = p;
  90. }
  91. return 1;
  92. }
  93. return 0;
  94. }
  95.  
  96. char *pop(struct node **root) {
  97. char *r;
  98. struct node *p;
  99. if (*root == 0)
  100. return 0;
  101. p = *root;
  102. r = p->data;
  103. if (p->next == p) {
  104. *root = 0;
  105. free(p);
  106. } else {
  107. p->next->prev = p->prev;
  108. p->prev->next = p->next;
  109. *root = p->next;
  110. free(p);
  111. }
  112. return r;
  113. }
  114.  
  115. #define BUFFSIZE 1024
  116. int main(int argc, char *argv[]) {
  117. struct node *rootS = 0;
  118. static char buff[BUFFSIZE];
  119. char *p;
  120. int flag_reverse;
  121. int flagsig;
  122. if (argc != 2) {
  123. usage:
  124. fprintf(stderr, "usage: %s -r/f.\n option -r:reverse, -f:forward\n", argv[0]);
  125. exit(1);
  126. }
  127. if (*argv[1] == '-') {
  128. if (*(argv[1] + 1) == 'f')
  129. flag_reverse = 0;
  130. else if (*(argv[1] + 1) == 'r')
  131. flag_reverse = 1;
  132. else
  133. goto usage;
  134. } else {
  135. goto usage;
  136. }
  137.  
  138. signal_handling_entry(INIT);
  139. rootS = 0;
  140. for (;;) {
  141. signal_handling_entry(DISABLE);
  142. flagsig = flagSignalS;
  143. signal_handling_entry(ENABLE);
  144. if (flagsig)
  145. break;
  146. if (fgets(buff, BUFFSIZE, stdin) == 0) {
  147. if (errno != EINTR)
  148. break;
  149. else
  150. continue;
  151. }
  152. if ((p = malloc(strlen(buff) + 1)) != 0) {
  153. strcpy(p, buff);
  154. if (flag_reverse) {
  155. if (!push_head(&rootS, p))
  156. free(p);
  157. } else {
  158. if (!push_tail(&rootS, p))
  159. free(p);
  160. }
  161. }
  162. }
  163. putchar('\n');
  164. for (;;) {
  165. signal_handling_entry(DISABLE);
  166. flagsig = flagSignalS;
  167. signal_handling_entry(ENABLE);
  168. if (flagsig)
  169. break;
  170. if((p = pop(&rootS)) == 0)
  171. break;
  172. printf("%s", p);
  173. free(p);
  174. }
  175. while ((p = pop(&rootS)) != 0)
  176. free(p);
  177. return 0;
  178. }
  179. /* end */
  180.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘signal_handler’:
prog.c:13: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result
prog.c: In function ‘signal_handling_entry’:
prog.c:30: error: incompatible types in assignment
stdout
Standard output is empty