#include <stdio.h> #include <signal.h> #include <unistd.h> #include <string.h> #define MAX_LINE 80 #define HISTORY_SIZE 10 #define BUFFER_SIZE 50 // Globals struct Command { unsigned int mNumber; char* mCommand; }; struct Command* history[HISTORY_SIZE]; unsigned int cmdCount = 0; // total number of entered commands unsigned int historyIndex = 0; // current index of history char buffer[BUFFER_SIZE]; // Prototypes void setup(char inputBuffer[], char* args[], int* background); void handle_SIGINT(); int main(void) { char inputBuffer[MAX_LINE]; char* args[MAX_LINE]; // Signal handling struct sigaction handler; handler.sa_handler = &handle_SIGINT; sigaction(SIGINT, &handler, NULL); // Generate output message strcpy(buffer, "Caught Control C\n"); // Wait to recieve ctrl+c while (1) { } return 0; } void setup(char inputBuffer[], char* args[], int* background) { // Get input and store in inputBuffer if (!gets(inputBuffer)) { exit(1); } // Create a command object struct Command cmd; cmd.mNumber = cmdCount++; cmd.mCommand = inputBuffer; // Store the command in the history buffer history[historyIndex] = cmd; // Wrap the index back to the beginning if we reach capacity historyIndex = (historyIndex + 1) % HISTORY_SIZE; // Tokenize inputBuffer and store each string delimited by ' ' in args char* token = strtok (inputBuffer, " "); while (token != NULL) { *args++ = token; token = strtok (NULL, " "); } } void handle_SIGINT() { write(STDOUT_FILENO, buffer, strlen(buffer)); // Display last 10 commands for (unsigned int i=0; i < HISTORY_SIZE; ++i) { printf("%d\t%s\n", history[i].mNumber, history[i].mCommand); } exit(0); } totypes void setup(char inputBuffer[], char* args[], int* background); void handle_SIGINT(); int main(void) { char inputBuffer[MAX_LINE]; char* args[MAX_LINE]; // Signal handling struct sigaction handler; handler.sa_handler = handle_SIGINT; sigaction(SIGINT, &handler, NULL); // Generate output message strcpy(buffer, "Caught Control C\n"); // Wait to recieve ctrl+c while (1) { } return 0; } void setup(char inputBuffer[], char* args[], int* background) { // Get input and store in inputBuffer if (!gets(inputBuffer)) { exit(1); } // Create a command object struct Command cmd; cmd.mNumber = cmdCount++; cmd.mCommand = inputBuffer; // Store the command in the history buffer history[historyIndex] = cmd; // Wrap the index back to the beginning if we reach capacity historyIndex = (historyIndex + 1) % HISTORY_SIZE; // Tokenize inputBuffer and store each string delimited by ' ' in args char* token = strtok (inputBuffer, " "); while (token != NULL) { *args++ = token; token = strtok (NULL, " "); } } void handle_SIGINT() { write(STDOUT_FILENO, buffer, strlen(buffer)); // Display last 10 commands for (unsigned int i=0; i < HISTORY_SIZE; ++i) { printf("%d\t%s\n", history[i].mNumber, history[i].mCommand); } exit(0); }
ls /
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); ^
Standard output is empty