int main(int argc, char *argv[]) { if (argc < 2) myls("."); else if (argc == 2) myls(argv[1]); else printf("Usage: <%s> <path>\n", argv[0]); return 0; }
int choice(int sfd,packet* pk,int* fd) { switch(pk->type) { case 0: get(pk,fd); break; case 1: mycd(pk); break; case 2: myls(pk); break; case 3: myputs(sfd,pk); break; case 4: mygets(pk,fd); break; case 5: myremove(pk); break; case 6: mypwd(pk); break; case 7: getend(fd); break; default: break; } return 0; }
void myls(char *path) { DIR *dp; struct dirent *dirp; struct stat s; struct file_info file; char next_path[1024]; int fd; int len; int slink; if (NULL == (dp = opendir(path))) { perror("opendir"); return; } while (dirp = readdir(dp)) { if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..")) { continue; } len = strlen(path); memcpy(next_path, path, len + 1); strcat(next_path, "/"); strcat(next_path, dirp->d_name); lstat(next_path, &s); file.type = file_type(s); file.mode = s.st_mode; slink = 1; file.slink = slink; file.uid = s.st_uid; file.gid = s.st_gid; file.size = s.st_size; file.time = s.st_atime; len = strlen(dirp->d_name); memcpy(file.name, dirp->d_name, len); (file.name)[len] = '\0'; print(file); if (S_ISDIR(s.st_mode)) { printf("\n%s: \n", dirp->d_name); myls(next_path); putchar('\n'); } } closedir(dp); }
/* *选择:0<往链接写报文>/1<cd>/2<myls>/3<打开文件>/4<读取服务端文件内容并发给客户端> *******5<移除文件>/6<获取当前路径>/7<关闭链接>/8<退出循环> * */ int choice(node* n,packet* pk,int* running,char *filedir) { switch(pk->type) { case 0: put(pk,n->put_fd); break; case 1: mycd(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 2: myls(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 3: myputs(pk,&(n->put_fd),filedir); send_pk(n->accept_fd,pk); break; case 4: mygets(n->accept_fd,pk,n->curdir); break; case 5: myremove(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 6: mypwd(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 7: putend(&(n->put_fd)); break; case 8: *running=0; break; default:break; } return 0; }
main(int argc, char *argv[ ]) { //*Lab 5 CODE char *tok; char cmdArgs[2][MAX] = {'\0'}; char cwd[128]; int cmdCount = 0; //LAB 5 CODE* char temp[MAX]; int n; char line[MAX], ans[MAX]; //*Lab 5 CODE getcwd(cwd, 128); // get CWD pathname // Lab 5 CODE* printf("cwd: %s\n", cwd); if (argc < 3){ printf("Usage : client ServerName ServerPort\n"); exit(1); } client_init(argv); // sock <---> server printf("******** processing loop *********\n"); while (1){ //printf("input two numbers to add : "); //bzero(line, MAX); // zero out line[ ] // *LAB 5 CODE cmdCount = 0; strcpy(cmdArgs[0],""); strcpy(cmdArgs[1],""); printf("Input a command: "); // LAB 5 CODE* fgets(line, MAX, stdin); // get a line (end with \n) from stdin //printf("line: %s\n", line); line[strlen(line)-1] = 0; // kill \n at end //printf("line[0]: %c\n", line[0]); //if(line[0]==0) // exit if NULL line //{exit(0);} // *Lab 5 CODE strcpy(temp, line); //printf("temp: %s\n", temp); tok = strtok(line, " "); cmdCount++; //printf("temp: %s\n", temp); //printf("cmdArgs[0] = %s\n", cmdArgs[0]); //printf("cmdCount: %d\n", cmdCount); while(tok != NULL) { strcpy(cmdArgs[cmdCount-1], tok); tok = strtok(NULL, " "); cmdCount++; //printf("cmdCount: %d\n", cmdCount); } //printf("cmdCount: %d\n", cmdCount); //printf("line: %s\n", line); //printf("cmdArgs[0] = %s\n", cmdArgs[0]); if(strcmp(cmdArgs[0], "lcat") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mycat(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "lpwd") == 0) { mypwd(cwd); } else if (strcmp(cmdArgs[0], "lls") == 0) { printf("args: %s\n", cmdArgs[1]); if(strcmp(cmdArgs[1],"") != 0) {myls(cmdArgs[1]);} else { printf("cwd: %s\n", cwd); myls(cwd); } } else if (strcmp(cmdArgs[0], "lcd") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mycd(cmdArgs[1]);} else {mycd(cwd);} } else if (strcmp(cmdArgs[0], "lmkdir") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mymkdir(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "lrmdir") == 0) { if(strcmp(cmdArgs[1],"") != 0) {myrmdir(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "lrm") == 0) { if(strcmp(cmdArgs[1],"") != 0) {myrm(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "get") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mycp(cmdArgs[1], cwd);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "quit") == 0) { printf("Goodbye!\n"); return 0; } else // Not a local command, work with the sever { // Send command to server n = write(server_sock, temp, MAX); printf("client: wrote n=%d bytes; line=(%s)\n", n, temp); // Send command pathname //n = write(server_sock, cmdArgs[0], MAX); //printf("client: wrote n=%d bytes; line=(%s)\n", n, line); // Now read the Reply and Results in from the Server... //REPLY //n = read(server_sock, ans, MAX); //printf("client: read n=%d bytes; echo=(%s)\n",n, ans); //RESULTS //n = read(server_sock, ans, MAX); //printf("client: read n=%d bytes; echo=(%s)\n",n, ans); readData(ans); } // Lab 5 CODE* // // Send ENTIRE line to server //n = write(server_sock, line, MAX); //printf("client: wrote n=%d bytes; line=(%s)\n", n, line); // Read a line from sock and show it //n = read(server_sock, ans, MAX); //printf("client: read n=%d bytes; echo=(%s)\n",n, ans); } }
int main(int argc, char *argv[]) { myls(argc, argv); return 0; }
int callInternal(command* C, char* homeDir) { if (C->argc == 0) return 0; else if (strcmp(C->argv[0],"exit") == 0) return 1; else if (strcmp(C->argv[0],"mycat") == 0) { if (C->argc == 2) mycat(C->argv[1]); else if (C->argc == 1) printf("Argument missing\n"); else if (C->argc > 2) printf("Incorrect usage of mycat\n"); } else if (strcmp(C->argv[0],"mycp") == 0) { if (C->argc == 3) mycp(C->argv[1],C->argv[2]); else if (C->argc == 2) printf("Destination needed\n"); else if (C->argc == 1) printf("Source & destination needed\n"); else printf("Incorrect usage of mycp\n"); } else if (strcmp(C->argv[0],"mypwd") == 0) { if (C->argc > 1) printf("Incorrect usage of mypwd\n"); else { char* pwd = mypwd(); printf("%s\n",pwd); free(pwd); } } else if (strcmp(C->argv[0],"mymkdir") == 0) { if (C->argc < 2) printf("missing argument\n"); else if (C->argc > 2) printf("Incorrect usage of mymkdir\n"); else mymkdir(C->argv[1]); } else if (strcmp(C->argv[0],"myrmdir") == 0) { if (C->argc < 2) printf("missing argument\n"); else if (C->argc > 2) printf("Incorrect usage of mymkdir\n"); else myrmdir(C->argv[1]); } else if (strcmp(C->argv[0],"mycd") == 0) { if (C->argc >2) printf("Incorrect usage of mycd\n"); else if (C->argc == 2) mycd(C->argv[1]); else mycd(homeDir); } else if (strcmp(C->argv[0],"myls") == 0) { if (C->argc == 1) myls(NULL,0); else if (C->argc == 2) if (strcmp(C->argv[1],"-l") == 0) myls(NULL,1); else myls(C->argv[1],0); else if (C->argc == 3) if (strcmp(C->argv[1],"-l") == 0) myls(C->argv[2],1); else printf("Incorrect usage of myls\n"); else printf("Incorrect usage of myls\n"); } else if (strcmp(C->argv[0],"myrm") == 0) { if (C->argc == 2) myrm(C->argv[1]); else if (C->argc == 1) printf("missing argument\n"); else printf("Incorrect usage of myrm\n"); } else printf("command not found: %s\n",C->argv[0]); return 0; }
int main(){ myls(1); }
int main(int argc, char** argv) { int count=0, i=0, cmp, j, valid=0; char key[MAX]; //run the shell till user inputs 'quit' or Ctrl-C while(strcmp(cmdline, "quit") != 0) { count = 0; //our shell's prompt printf("afreen@myShell $ "); //get command line input gets(cmdline); //tokenize the CL input delimited by " " using strtok() into args args[count++] = strtok(cmdline, " "); //tokenize the entire string till the next token encountered //does not have a NULL pointer while((args[count++] = strtok(NULL, " "))); count--; //no of arguments (argc) //if user wants to clear screen if(strcmp(args[0], "clear") == 0) { myclear(); } //checking for the input command strcpy(key, args[0]); for(i=0; i<18; i++) //look through our check array for command { if(strcmp(check[i], key) == 0) { valid = -1; switch(i) { case 0: mycat(count, args); break; case 1: mycd(count, args); break; case 2: mycp(count, args); break; case 3: mydate(count, args); break; case 4: myhead(count, args); break; case 5: myls(count, args); break; case 6: myll(count, args); break; case 7: myln(count, args); break; case 8: myld(count, args); break; case 9: mymkdir(count, args); break; case 10: mymv(count, args); break; case 11: mypwd(count, args); break; case 12: myrm(count, args); break; case 13: myrmdir(count, args); break; case 14: mytail(count, args); break; case 15: mytime(count, args); break; case 16: mytouch(count, args); break; case 17: myuname(count, args); break; default: printf("%s: Command not found\n", key); break; } break; } } } return 0; }
int main() { int choice, i, j; char name[256]; #if 0 { int error; toolfs_act_type action; char pathname[MYNAME_MAX]="/home/hsingh/tmp"; action = TOOLFS_READINODE_TABLE; error = toolfs((const char *) &pathname[0], action); printf("\n Return toolfs = %d\n",error); recover(); exit(0); } #endif do { printf("\n 1. List Folder/Directory Details"); printf("\n 2. List Current INODE Table Details"); printf("\n 3. Damage a Folder while deleting a file "); printf("\n 4. Fix/Undelete Folder/Directory"); printf("\n 5. Exit"); printf("\nEnter Your Choice : "); scanf("%d", &choice); switch (choice) { case 1: printf("***** Enter Path (relative or absolute) ***** "); do { printf("\nEnter Path Name : "); scanf("%s", name); printf("\n name=%s\n",name); //checkpathname(name); clearmystats(); myls(&name[0]); printmystats(1); } while (0); break; case 2: printf("***** Enter Path (relative or absolute) ***** "); do { printf("\nEnter Path Name : "); scanf("%s", name); //checkpathname(name); //myls(&name[0]); checkdir(name); } while (0); break; case 3: printf("***** Enter File name with Path (relative or absolute) ***** "); do { scanf("%s", name); //checkpathname(name); myls(&name[0]); } while (0); break; case 4: printf("***** Enter Path (relative or absolute) ***** "); do { printf("\nEnter Path Name : "); scanf("%s", name); //checkpathname(name); myls(&name[0]); } while (0); break; } } while (choice != 5) ; printf("\n Exiting gracefully\n"); }
main(int argc, char *argv[]) { int i, m; char cwd[128]; char pathName[255]; FILE* tmp; int failed = 0; m = getinputs(); // get user inputs name=value into entry[ ] getcwd(cwd, 128); // get CWD pathname printf("Content-type: text/html\n\n"); printf("<p>pid=%d uid=%d cwd=%s\n", getpid(), getuid(), cwd); printf("<H1>Echo Your Inputs</H1>"); printf("You submitted the following name/value pairs:<p>"); for(i=0; i <= m; i++) printf("%s = %s<p>", entry[i].name, entry[i].value); printf("<p>"); /***************************************************************** Write YOUR C code here to processs the command mkdir dirname rmdir dirname rm filename cat filename cp file1 file2 ls [dirname] <== ls CWD if no dirname *****************************************************************/ // failed = 0; if(strcmp(entry[0].value, "mkdir") == 0){ // mkdir for(i = 1; i <= m; i++) { if(strcmp(entry[i].value, "\0") != 0){ // checks to make sure it's not a NULL, strcpy(pathName, cwd); strcat(pathName, "/"); strcat(pathName, entry[i].value); printf("command is %s, Dir name is \"%s\"\n", entry[0].value, pathName); failed = mkdir(pathName, 0777); // was going to use "mode_t mode" from sys/stat.h (S_IRWXU, S_IRWXG, S_IROTH, S_IXOTH) but friend suggested 0x16D instead and it works if(failed != 0){ i = m; // this ends the loop, in hind sight I could have made a while function, but this works } } } } else if(strcmp(entry[0].value, "rmdir") == 0){ for(i = 1; i <= m; i++) { if(strcmp(entry[i].value, "\0") != 0){ // again, compares to NULL "0\" strcpy(pathName, cwd); strcat(pathName, "/"); strcat(pathName, entry[i].value); printf("command is %s, Dir name is \"%s\"\n", entry[0].value, pathName); failed = rmdir(pathName); } } } else if(strcmp(entry[0].value, "rm") == 0){ for(i = 1; i <= m; i++){ if(strcmp(entry[i].value, "\0") != 0){ strcpy(pathName, cwd); strcat(pathName, "/"); strcat(pathName, entry[i].value); printf("command is %s, file name is \"%s\"\n", entry[0].value, pathName); printf("Failed to remove\n"); failed = 1; failed = remove(pathName); } } } else if(strcmp(entry[0].value, "cat") == 0){ char buffer[1024] = {'\0'}; for(i = 1; i <= m; i++){ tmp = fopen(entry[i].value, "r"); if(tmp == NULL){ failed = 1; } else{ while(fgets(buffer, 1024, tmp) != NULL){ printf("%s <br/>", buffer); } } if(failed != 0){ i = m; } } failed = 0; } else if(strcmp(entry[0].value, "cp") == 0){ FILE *src, *dest; char buffer[1024] = {'\0'}; src = fopen(entry[1].value, "r"); dest = fopen(entry[2].value, "w"); if(src == NULL || dest == NULL){ printf("Could not open file"); } else { while(fgets(buffer, 1024, src) != NULL){ fputs(buffer, dest); } } failed = 0; } else if(strcmp(entry[0].value, "ls") == 0){ //printf("need to getdents"); if(strlen(entry[1].value) != 0){ myls(entry[1].value, cwd); } else{ myls(".", cwd); } failed = 0; // this make it so system knows that command succeeded } else { printf("Command not programmed yet"); } if(failed) { printf("<h2>%s failed</h2>", entry[0].value); // printf("Error: %s<br/><br/>", strerror(errno)); //If I want to include error decoding } else { printf("<h2>Command succeeded!</h2>"); } failed = 0; // create a FORM webpage for user to submit again printf("</title>"); printf("</head>"); printf("<body bgcolor=\"#c49561\" link=\"#330033\" leftmargin=8 topmargin=8"); printf("<p>------------------ DO IT AGAIN ----------------\n"); printf("<FORM METHOD=\"POST\" ACTION=\"http://cs360.eecs.wsu.edu/~juel/cgi-bin/mycgi\">"); //------ NOTE : CHANGE ACTION to YOUR login name ---------------------------- //printf("<FORM METHOD=\"POST\" ACTION=\"http://cs560.eecs.wsu.edu/~YOURNAME/cgi-bin/mycgi\">"); printf("Enter command : <INPUT NAME=\"command\"> <P>"); printf("Enter filename1: <INPUT NAME=\"filename1\"> <P>"); printf("Enter filename2: <INPUT NAME=\"filename2\"> <P>"); printf("Submit command: <INPUT TYPE=\"submit\" VALUE=\"Click to Submit\"><P>"); printf("</form>"); printf("------------------------------------------------<p>"); printf("</body>"); printf("</html>"); }