11 char **parse_command(char *my_line, char value)
16 char **args = malloc(buffer_size * sizeof(char*));
17 char *valuee = &value;
18 arg = strtok(my_line, valuee);
22 arg = strtok(NULL, valuee);
29 void trimTrailing(char * str)
33 /* Set default index */
36 /* Find last index of non-white space character */
40 if(str[i] != ' ' && str[i] != '\t' && str[i] != '\n')
48 /* Mark next character to last non-white space character as NULL */
49 str[index + 1] = '\0';
51 ////////////////////EXEC PROGRAM FUNCTION/////////////
55 static int exec_prog(int argc, const char **argv) {
58 int status, timeout /* unused ifdef WAIT_FOR_COMPLETION */;
60 // this block executes when pid=0, (I am the father process)
62 for (int i =0; i < argc; i++) {
64 if ((pid[i] = fork()) < 0) { /* fork a child process */
65 printf("*** ERROR: forking child process failed\n");
67 } else if (pid[i] == 0) { /* for the child process: */
68 trimTrailing(argv[i]);
69 char **value = parse_command(argv[i], ' ');
70 if (strcmp(value[0], "")) {
71 if (execvp(value[0], value) < 0) { /* execute the command */
72 printf("*** ERROR: exec failed\n");
84 for (int i =0; i < argc; i++) {
86 if ((pid[i] = waitpid(pid[i], &status, WNOHANG)) == -1) {
87 perror("wait() error");
89 else if (pid[i] == 0) {
90 /*printf("child is still running ");*/
93 if (WIFEXITED(status))
94 printf("child exited with status of %d\n", WEXITSTATUS(status));
95 /*else puts("child did not exit successfully");*/
97 } while (pid[i] == 0);
106 //////////////////////////////////////////////////////
108 ////////////////////BATCH MODE PROCESS////////////////
112 int batchModeStart(const char **argv) {
120 //////////////////////////////////////////////////////
123 printf("⌈[tinysh] \n↳>>> ");
127 char *read_command_line(void)
130 char *buffer = malloc(sizeof(char) * bufsize);
134 while ( c != '\n' ) {
144 int main (int argc, char *argv[]){
146 char *prompt = argv[0];
148 bool batchMode = false;
152 } else if (argc != 1) {
153 printf("Wrong ammount of arguments, only one file per batch \n");
157 /*exec_prog(1, filename);*/
158 /*exec_prog(&argv[1]);*/
162 //Oscar J Rodriguez B (@josuer08)
168 arguments = read_command_line();
169 char **args = parse_command(arguments, ';');
170 trimTrailing(args[0]);
171 if (strcmp(args[0], "exit") == 0) {/* is it an "exit"? */
172 active = false; /* exit if it is */
174 exec_prog(COMMANDS_SIZE,args);
176 /*printf("%d", COMMANDS_SIZE);*/
177 /*printf("%s and %s", args[0], args[1]);*/
182 FILE * f = fopen (filename, "rb");
185 fseek (f, 0, SEEK_END);
187 fseek (f, 0, SEEK_SET);
188 buffer = malloc (length);
190 fread (buffer, 1, length, f);
196 char **args = parse_command(buffer, ';');
197 trimTrailing(args[0]);
198 /*printf("%s", buffer);*/
199 exec_prog(COMMANDS_SIZE,args);