fork download
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5.  
  6. #define MAX_LINE 80
  7. #define HISTORY_SIZE 10
  8. #define BUFFER_SIZE 50
  9.  
  10. // Globals
  11. struct Command
  12. {
  13. unsigned int mNumber;
  14. char* mCommand;
  15. };
  16.  
  17. struct Command* history[HISTORY_SIZE];
  18. unsigned int cmdCount = 0; // total number of entered commands
  19. unsigned int historyIndex = 0; // current index of history
  20. char buffer[BUFFER_SIZE];
  21.  
  22. // Prototypes
  23. void setup(char inputBuffer[], char* args[], int* background);
  24. void handle_SIGINT();
  25.  
  26. int main(void)
  27. {
  28. char inputBuffer[MAX_LINE];
  29. char* args[MAX_LINE];
  30.  
  31. // Signal handling
  32. struct sigaction handler;
  33. handler.sa_handler = &handle_SIGINT;
  34. sigaction(SIGINT, &handler, NULL);
  35.  
  36. // Generate output message
  37. strcpy(buffer, "Caught Control C\n");
  38.  
  39. // Wait to recieve ctrl+c
  40. while (1)
  41. {
  42. }
  43. return 0;
  44. }
  45.  
  46. void setup(char inputBuffer[], char* args[], int* background)
  47. {
  48. // Get input and store in inputBuffer
  49. if (!gets(inputBuffer))
  50. {
  51. exit(1);
  52. }
  53.  
  54. // Create a command object
  55. struct Command cmd;
  56. cmd.mNumber = cmdCount++;
  57. cmd.mCommand = inputBuffer;
  58.  
  59. // Store the command in the history buffer
  60. history[historyIndex] = cmd;
  61. // Wrap the index back to the beginning if we reach capacity
  62. historyIndex = (historyIndex + 1) % HISTORY_SIZE;
  63.  
  64.  
  65. // Tokenize inputBuffer and store each string delimited by ' ' in args
  66. char* token = strtok (inputBuffer, " ");
  67. while (token != NULL)
  68. {
  69. *args++ = token;
  70. token = strtok (NULL, " ");
  71. }
  72. }
  73.  
  74. void handle_SIGINT()
  75. {
  76. write(STDOUT_FILENO, buffer, strlen(buffer));
  77.  
  78. // Display last 10 commands
  79. for (unsigned int i=0; i < HISTORY_SIZE; ++i)
  80. {
  81. printf("%d\t%s\n", history[i].mNumber, history[i].mCommand);
  82. }
  83.  
  84. exit(0);
  85. }
  86.  
  87. totypes
  88. void setup(char inputBuffer[], char* args[], int* background);
  89. void handle_SIGINT();
  90.  
  91. int main(void)
  92. {
  93. char inputBuffer[MAX_LINE];
  94. char* args[MAX_LINE];
  95.  
  96. // Signal handling
  97. struct sigaction handler;
  98. handler.sa_handler = handle_SIGINT;
  99. sigaction(SIGINT, &handler, NULL);
  100.  
  101. // Generate output message
  102. strcpy(buffer, "Caught Control C\n");
  103.  
  104. // Wait to recieve ctrl+c
  105. while (1)
  106. {
  107. }
  108. return 0;
  109. }
  110.  
  111. void setup(char inputBuffer[], char* args[], int* background)
  112. {
  113. // Get input and store in inputBuffer
  114. if (!gets(inputBuffer))
  115. {
  116. exit(1);
  117. }
  118.  
  119. // Create a command object
  120. struct Command cmd;
  121. cmd.mNumber = cmdCount++;
  122. cmd.mCommand = inputBuffer;
  123.  
  124. // Store the command in the history buffer
  125. history[historyIndex] = cmd;
  126. // Wrap the index back to the beginning if we reach capacity
  127. historyIndex = (historyIndex + 1) % HISTORY_SIZE;
  128.  
  129.  
  130. // Tokenize inputBuffer and store each string delimited by ' ' in args
  131. char* token = strtok (inputBuffer, " ");
  132. while (token != NULL)
  133. {
  134. *args++ = token;
  135. token = strtok (NULL, " ");
  136. }
  137. }
  138.  
  139. void handle_SIGINT()
  140. {
  141. write(STDOUT_FILENO, buffer, strlen(buffer));
  142.  
  143. // Display last 10 commands
  144. for (unsigned int i=0; i < HISTORY_SIZE; ++i)
  145. {
  146. printf("%d\t%s\n", history[i].mNumber, history[i].mCommand);
  147. }
  148.  
  149. exit(0);
  150. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
ls /
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:33:21: error: invalid conversion from ‘void (*)()’ to ‘__sighandler_t {aka void (*)(int)}’ [-fpermissive]
  handler.sa_handler = &handle_SIGINT;
                     ^
prog.cpp:28:7: warning: unused variable ‘inputBuffer’ [-Wunused-variable]
  char inputBuffer[MAX_LINE];
       ^
prog.cpp:29:8: warning: unused variable ‘args’ [-Wunused-variable]
  char* args[MAX_LINE];
        ^
prog.cpp: In function ‘void setup(char*, char**, int*)’:
prog.cpp:49:7: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  if (!gets(inputBuffer))
       ^
prog.cpp:49:23: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  if (!gets(inputBuffer))
                       ^
prog.cpp:51:9: error: ‘exit’ was not declared in this scope
   exit(1);
         ^
prog.cpp:60:24: error: cannot convert ‘Command’ to ‘Command*’ in assignment
  history[historyIndex] = cmd;
                        ^
prog.cpp: In function ‘void handle_SIGINT()’:
prog.cpp:81:33: error: request for member ‘mNumber’ in ‘history[i]’, which is of pointer type ‘Command*’ (maybe you meant to use ‘->’ ?)
   printf("%d\t%s\n", history[i].mNumber, history[i].mCommand);
                                 ^
prog.cpp:81:53: error: request for member ‘mCommand’ in ‘history[i]’, which is of pointer type ‘Command*’ (maybe you meant to use ‘->’ ?)
   printf("%d\t%s\n", history[i].mNumber, history[i].mCommand);
                                                     ^
prog.cpp:84:8: error: ‘exit’ was not declared in this scope
  exit(0);
        ^
prog.cpp: At global scope:
prog.cpp:87:1: error: ‘totypes’ does not name a type
 totypes
 ^
prog.cpp: In function ‘int main()’:
prog.cpp:91:5: error: redefinition of ‘int main()’
 int main(void) 
     ^
prog.cpp:26:5: error: ‘int main()’ previously defined here
 int main(void) 
     ^
prog.cpp:98:21: error: invalid conversion from ‘void (*)()’ to ‘__sighandler_t {aka void (*)(int)}’ [-fpermissive]
  handler.sa_handler = handle_SIGINT;
                     ^
prog.cpp:93:7: warning: unused variable ‘inputBuffer’ [-Wunused-variable]
  char inputBuffer[MAX_LINE];
       ^
prog.cpp:94:8: warning: unused variable ‘args’ [-Wunused-variable]
  char* args[MAX_LINE];
        ^
prog.cpp: In function ‘void setup(char*, char**, int*)’:
prog.cpp:111:6: error: redefinition of ‘void setup(char*, char**, int*)’
 void setup(char inputBuffer[], char* args[], int* background)
      ^
prog.cpp:46:6: error: ‘void setup(char*, char**, int*)’ previously defined here
 void setup(char inputBuffer[], char* args[], int* background)
      ^
prog.cpp:114:7: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  if (!gets(inputBuffer))
       ^
prog.cpp:114:23: warning: ‘char* gets(char*)’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
  if (!gets(inputBuffer))
                       ^
prog.cpp:116:9: error: ‘exit’ was not declared in this scope
   exit(1);
         ^
prog.cpp:125:24: error: cannot convert ‘Command’ to ‘Command*’ in assignment
  history[historyIndex] = cmd;
                        ^
prog.cpp: In function ‘void handle_SIGINT()’:
prog.cpp:139:6: error: redefinition of ‘void handle_SIGINT()’
 void handle_SIGINT()
      ^
prog.cpp:74:6: error: ‘void handle_SIGINT()’ previously defined here
 void handle_SIGINT()
      ^
prog.cpp:146:33: error: request for member ‘mNumber’ in ‘history[i]’, which is of pointer type ‘Command*’ (maybe you meant to use ‘->’ ?)
   printf("%d\t%s\n", history[i].mNumber, history[i].mCommand);
                                 ^
prog.cpp:146:53: error: request for member ‘mCommand’ in ‘history[i]’, which is of pointer type ‘Command*’ (maybe you meant to use ‘->’ ?)
   printf("%d\t%s\n", history[i].mNumber, history[i].mCommand);
                                                     ^
prog.cpp:149:8: error: ‘exit’ was not declared in this scope
  exit(0);
        ^
stdout
Standard output is empty