static int make_dir_path(const char * const run_dir) { struct stat st; int res = 0; char tmp_path[MAX_PATH_LEN]; char *ndx; strlcpy(tmp_path, run_dir, sizeof(tmp_path)); /* Strip any trailing dir sep char. */ chop_char(tmp_path, PATH_SEP); for(ndx = tmp_path+1; *ndx; ndx++) { if(*ndx == '/') { *ndx = '\0'; /* Stat this part of the path to see if it is a valid directory. * If it does not exist, attempt to create it. If it does, and * it is a directory, go on. Otherwise, any other error cause it * to bail. */ if(stat(tmp_path, &st) != 0) { if(errno == ENOENT) { res = mkdir(tmp_path, S_IRWXU); if(res != 0) return res; /* run stat() against the component since we just * created it */ if(stat(tmp_path, &st) != 0) { log_msg(LOG_ERR, "Could not create component: %s of %s", tmp_path, run_dir ); return(ENOTDIR); } } } if(! S_ISDIR(st.st_mode)) { log_msg(LOG_ERR, "Component: %s of %s is NOT a directory", tmp_path, run_dir ); return(ENOTDIR); } *ndx = '/'; } } res = mkdir(tmp_path, S_IRWXU); return(res); }
//processes command by forking, redirecting i/o in the child process, //and using execvp in the child process int do_command(){ printf("Executing command %s", my_argv[0]); if(my_argc > 1) printf(" with arguments"); for(int i =1 ; i < my_argc; i++) printf(" \"%s\"", my_argv[i]); printf("\n"); if(gettimeofday(&start, NULL) <0) perror("start gettimeofday"); if((pid = fork()) == -1){ perror("Fork "); exit(1); } //actions to be performed in child process if(pid == 0){ int i; // I/O Redirection if(numrs != 0){ for(i = 0; i!= numrs; ++i){ switch(redirect_check(my_argv1[i])){ case 1: chop_char(my_argv1[i], 1); // "<filename" redirect(my_argv1[i], 0, 0); break; case 2: redirect(my_argv1[i +1], 0, 0); // "< filename" break; case 3: chop_char(my_argv1[i], 1); // ">filename" redirect(my_argv1[i], 1, 1); break; case 4: //"> filename" redirect(my_argv1[i+1], 1, 1); break; case 5: //"2>filename" chop_char(my_argv1[i], 2); redirect(my_argv1[i], 2, 1); break; case 6: //"2> filename" redirect(my_argv1[i+1], 2, 1); break; case 7: //">>filename" chop_char(my_argv1[i], 2); redirect(my_argv1[i], 1, 2); break; case 8: //">> filename" redirect(my_argv1[i+1], 1, 2); break; case 9: //"2>>filename" chop_char(my_argv1[i], 3); redirect(my_argv1[i], 2, 2); break; case 10: //"2>> filename" redirect(my_argv1[i+1], 2, 2); break; } } } execvp(my_argv[0], my_argv); perror("execvp"); exit(1); } //actions to be performed in parent process if(pid != 0) printProcessInfo(); return 0; }
void chop_newline(char *str) { chop_char(str, 0x0a); return; }