void executa_comando(struct command_t *cmd) //execução do comando { const char *cmdname = cmd->argv[0]; glob_t *globbuf = (glob_t *) malloc(sizeof(glob_t)); globbuf = expande_glob(cmd); if(strcmp(cmdname,"cd") == 0) func_cd(cmd->argc,cmd->argv); //comandos internos - sem uso do fork() else if(strcmp(cmdname,"pwd") == 0) func_pwd(); if(strcmp(cmdname,"exit") == 0) func_exit(); else{ //uso do fork aqui //pid_t cpid = fork(); int wait_pid; int status; int lastchild; if (cpid < 0) { perror("fork"); exit(127); } else if (cpid > 0) { /* pai */ lastchild = cpid; wait_pid = wait(&status); } else { /* filho */ if(globbuf == NULL) execvp(cmdname, cmd->argv); else execvp(cmdname, globbuf->gl_pathv); perror("exec"); exit(127); } if(wait_pid < 0) perror("wait"); } }
int main(int argc, char *argv[]) { void (*OldSig)(int); OldSig=signal(SIGINT,SIG_IGN); printf("nn************************************************************rn"); printf("* zFTP *rn"); printf("************************************************************rn"); printf(" Copyright (c) 2005rn"); printf(" All rights reserved.rn"); printf(" By Ji Surn"); if(argc > 0) { printf("nPlease use 'connect <system name> <user>' command to connect to a FTPserverrn"); printf("For anonymous user, please leave <user> blank.rn"); } //Main loop starts; //Checks on keyboard i/o for interaction and socket communication. int exit=0; char command[1024]; printf("nzftp>"); while(!exit) { if( ReadCommand ) { printf("nzftp>"); fflush(stdout); } if( !CheckFds(command) ) continue; switch(check_cmd(command) ) { case READ: func_read(command); break; case WRITE: func_write(command); break; case BINARY: func_binary_mode(); break; case ASCII: func_ascii_mode(); break; case CLOSE: func_close(); break; case CONNECT: func_connect(command); break; case CD: func_cdir(command); break; case DELETE: func_delete(command); break; case PWD: func_pwd(command); break; case LS: func_list(command); break; case HELP: func_help(command); break; case SHELL: func_Shell_cmd(command); break; case EXIT: exit = 1; if( flag_connected ) func_close(); break; default: if(command[0] == 0 ) ; else printf("Invalid command: %sn",command); break; } } (void)signal(SIGINT,OldSig); return 0; }