static char *rewrite_env_sender(const char *from) { const char *host; if (!from) { if ((from=env("MAILSUSER")) == 0 && (from=env("MAILUSER")) == 0 && (from=env("LOGNAME")) == 0 && (from=env("USER")) == 0) { struct passwd *pw=mypwd(); from=pw ? pw->pw_name:"nobody"; } if ((host=env("MAILSHOST")) != 0 || (host=env("MAILHOST")) != 0) { char *p=courier_malloc(strlen(from)+strlen(host)+2); return (strcat(strcat(strcpy(p, from), "@"), host)); } } return (strcpy(courier_malloc(strlen(from)+1), from)); }
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 mypwd_above(int k){ int i, rep; rep = 0; for( i = 0; i < k && rep == 0; i++){ rep = chdir(".."); } mypwd(); }
//добовляем функцию int main(int argc, char *argv[]) { //выделяем буфер для вывода, увеличиваем его char buff[1024]; mypwd (argv[1], buff, sizeof (buff)); printf("%s\n", buff); //выводим строку на экран return 0; }
static char *get_gecos() { struct passwd *pw=mypwd(); char *p, *q; if (!pw || !pw->pw_gecos) return (0); p=strcpy(courier_malloc(strlen(pw->pw_gecos)+1), pw->pw_gecos); if ((q=strchr(p, ',')) != 0) *q=0; return (p); }
void myls(char* dir,int _d_mod) { char* _wd; DIR* _dirp; struct dirent* _entry; if (dir == NULL) _wd = mypwd(); else _wd = dir; if ((_dirp = opendir(_wd)) == NULL) //something goes wrong { perror(_wd); return; } if (_d_mod) //detailed mode { struct stat _fileStat; char* path = (char*) malloc(MAXPATHLEN); struct tm* _filetime; while((_entry = readdir(_dirp)) != NULL) { strcpy(path,_wd); strcat(path,"/"); strcat(path,_entry->d_name); stat(path,&_fileStat); _filetime = localtime(&_fileStat.st_mtime); printf("%us %s %s %lld %s %s\n", _fileStat.st_mode, getpwuid(_fileStat.st_uid)->pw_name, getgrgid(_fileStat.st_gid)->gr_name, (long long)_fileStat.st_size, asctime(_filetime), _entry->d_name); } } else //simple mode while((_entry = readdir(_dirp)) != NULL) printf("%s\n",_entry->d_name); if (dir == NULL) free(_wd); if (closedir(_dirp) != 0) //something goes wrong perror("error when closing directory"); }
/* *选择: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 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; }
void printPrompt() { char* _cwd = mypwd(); printf("%s->$ ",_cwd); free(_cwd); }
int main(int argc, char **argv) { const char *from=0; const char *From=0; const char *ret=0, *dsn=0, *security=0, *envid=0; int argn, argp; int errflag=0; int c; char *args[6]; char *envs[7]; int envp; int tostdout=0; char frombuf[NUMBUFSIZE+30]; int doverp=0; int bcconly=0; char ubuf[NUMBUFSIZE]; char *uucprmail=0; char *s; char ret_buf[2], security_buf[40]; int submit_errcode; /* ** Immediately drop uid, force GID to MAILGID ** The reason we can't just setgid ourselves to MAILGID is because ** that only sets the effective uid. We need to also set both ** real and effective GIDs, otherwise maildrop filtering will fail, ** because the real gid won't be trusted. */ setgid(MAILGID); setuid(getuid()); signal(SIGCHLD, SIG_DFL); signal(SIGPIPE, SIG_IGN); argn=1; putenv("AUTHMODULES="); /* See module.local/local.c */ putenv("LANG=en_US"); putenv("CHARSET=iso-8859-1"); putenv("MM_CHARSET=iso-8859-1"); #if HAVE_SETLOCALE setlocale(LC_ALL, "C"); #endif /* Only uucp can run rmail (this better be installed setgid) */ if ((s=strrchr(argv[0], '/')) != 0) ++s; else s=argv[0]; if (strcmp(s, "rmail") == 0) { struct passwd *p=mypwd(); char *uu_machine, *uu_user; if (!p || strcmp(p->pw_name, "uucp")) { fprintf(stderr, "rmail: permission denied.\n"); exit(EX_NOPERM); } uu_machine=getenv("UU_MACHINE"); uu_user=getenv("UU_USER"); if (!uu_machine || !uu_user) { fprintf(stderr, "rmail: UU_MACHINE!UU_USER required.\n"); exit(EX_NOPERM); } uucprmail=malloc(strlen(uu_machine)+strlen(uu_user)+2); if (!uucprmail) { perror("malloc"); exit(EX_TEMPFAIL); } strcat(strcat(strcpy(uucprmail, uu_machine), "!"), uu_user); } while (argn < argc) { const char *arg; int c; if (argv[argn][0] != '-') break; if (strcmp(argv[argn], "-") == 0) { ++argn; break; } if (uucprmail && strncmp(argv[argn], "-f", 2) /* ** Ok, obviously no UUCP version will give me the following ** options, must I can hope, can't I? */ && strcmp(argv[argn], "-verp") && strncmp(argv[argn], "-N", 2) && strncmp(argv[argn], "-R", 2) && strncmp(argv[argn], "-V", 2) /* Ignore the ignorable */ && strncmp(argv[argn], "-o", 2) && strncmp(argv[argn], "-t", 2) ) { fprintf(stderr, "rmail: invalid option %s\n", argv[argn]); exit(EX_NOPERM); } if (strcmp(argv[argn], "-verp") == 0) { doverp=1; ++argn; continue; } if (strcmp(argv[argn], "-bcc") == 0) { bcconly=1; ++argn; continue; } if (strcmp(argv[argn], "-bs") == 0) { esmtpd(); } switch (c=argv[argn][1]) { case 'o': case 'f': case 'F': case 'R': case 'N': case 'S': case 'V': break; case 'n': tostdout=1; ++argn; continue; default: ++argn; continue; } arg=argv[argn]+2; if (!*arg && argn+1 < argc) arg=argv[++argn]; ++argn; if (c == 'f') from=arg; else if (c == 'F') From=arg; else if (c == 'N') dsn=arg; else if (c == 'S') { char *q; if (strcasecmp(arg, "NONE") && strcasecmp(arg, "STARTTLS")) { fprintf(stderr, "sendmail: invalid option" " -S %s\n", arg); exit(EX_NOPERM); } strcpy(security_buf, arg); for (q=security_buf; *q; q++) *q=toupper((int)(unsigned char)*q); security=security_buf; } else if (c == 'R') { ret_buf[0]= toupper((int)(unsigned char)*arg); ret_buf[1]= 0; ret=ret_buf; } else if (c == 'V') envid=arg; } sprintf(frombuf, "uid %s", libmail_str_uid_t(getuid(), ubuf)); argp=0; args[argp++]="submit"; if (bcconly) args[argp++]="-bcc"; if (uucprmail) { if (argn >= argc) { fprintf(stderr, "rmail: missing recipients\n"); exit(EX_NOPERM); } args[argp++]="uucp"; s=malloc(sizeof("unknown; uucp ()")+strlen(uucprmail)); if (!s) { perror("malloc"); exit(EX_TEMPFAIL); } strcat(strcat(strcpy(s, "unknown; uucp ("), uucprmail), ")"); args[argp++]=s; } else { args[argp++]="local"; args[argp++]="dns; localhost (localhost [127.0.0.1])"; args[argp++]=frombuf; } args[argp++]=0; envp=0; clog_open_stderr("sendmail"); if (ret || security || doverp) { envs[envp]=strcat(strcat(strcpy(courier_malloc(strlen(ret ? ret:"")+strlen(security ? security:"") + 30), "DSNRET="), (ret ? ret:"")), (doverp ? "V":"")); if (security) strcat(strcat(strcat(envs[envp], "S{"), security), "}"); ++envp; } if (dsn) { envs[envp]=strcat(strcpy(courier_malloc(strlen(dsn)+11), "DSNNOTIFY="), dsn); ++envp; } if (envid) { envs[envp]=strcat(strcpy(courier_malloc(strlen(envid)+10), "DSNENVID="), envid); ++envp; } envs[envp++]="TCPREMOTEHOST=localhost"; envs[envp++]="TCPREMOTEIP=127.0.0.1"; if (!uucprmail) envs[envp++]=strcat(strcpy(courier_malloc(strlen(frombuf)+ sizeof("TCPREMOTEINFO=")), "TCPREMOTEINFO="), frombuf); envs[envp]=0; if (!tostdout && submit_fork(args, envs, submit_print_stdout)) { fprintf(stderr, "sendmail: Service temporarily unavailable.\n"); exit(EX_TEMPFAIL); } if (tostdout) submit_to=stdout; else { if (uucprmail) { submit_write_message(from ? from:""); } else { char *p; p=rewrite_env_sender(from); submit_write_message(p); free(p); } if ((submit_errcode=submit_readrcprinterr()) != 0) { exit_submit(submit_errcode); } } while (!tostdout && argn < argc) { submit_write_message(argv[argn]); if ((errflag=submit_readrcprinterr()) != 0) { fprintf(stderr, "%s: invalid address.\n", argv[argn]); } ++argn; } if (errflag) exit_submit(errflag); if (!tostdout) putc('\n', submit_to); if (!uucprmail) rewrite_headers(From); while ((c=getchar()) != EOF) { putc(c, submit_to); } fflush(submit_to); signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); signal(SIGHUP, SIG_IGN); errflag=0; if (ferror(submit_to) || (!tostdout && ( fclose(submit_to) || (errflag=submit_readrcprinterr()) || submit_wait())) ) { fprintf(stderr, "sendmail: Unable to submit message.\n"); if (errflag) exit_submit(errflag); exit(EX_TEMPFAIL); } exit(0); }
static char *rewrite_from(const char *oldfrom, const char *newuser, const char *newhost, const char *newname) { struct rfc822t *rfct; struct rfc822a *rfca; struct rfc822t *usert, *hostt, *namet; struct rfc822token attoken, **tp; char *p; const char *q; char *gecosname=0; if (!oldfrom) { char *p=courier_malloc( (newuser ? strlen(newuser):0)+ (newhost ? strlen(newhost):0)+4); strcpy(p, "<"); if (newuser) strcat(p, newuser); if (newuser && newhost) strcat(strcat(p, "@"), newhost); strcat(p, ">"); if (newname) { char *q, *r; namet=tokenize_name(newname); q=rfc822_gettok(namet->tokens); rfc822t_free(namet); r=courier_malloc(strlen(p)+strlen(q)+2); strcat(strcat(strcpy(r, q), " "), p); free(p); p=r; free(q); } return (p); } if ((rfct=rfc822t_alloc_new(oldfrom, NULL, NULL)) == 0 || (rfca=rfc822a_alloc(rfct)) == 0) { clog_msg_errno(); return(0); } if ((q=env("MAILNAME")) || (q=env("NAME"))) newname=q; if (!newname && rfca->naddrs == 0) newname=gecosname=get_gecos(); if ((rfca->naddrs == 0 || rfca->addrs[0].tokens == 0) && newuser == 0) { struct passwd *pw=mypwd(); if (pw) newuser=pw->pw_name; } namet=newname ? tokenize_name(newname):0; usert=newuser ? rw_rewrite_tokenize(newuser):0; hostt=newhost ? rw_rewrite_tokenize(newhost):0; if (rfca->naddrs == 0 || rfca->addrs[0].tokens == 0) { struct rfc822addr a; struct rfc822a fakea; if (hostt) { struct rfc822token *t; attoken.token='@'; attoken.next=hostt->tokens; attoken.ptr=0; attoken.len=0; for (t=usert->tokens; t->next; t=t->next) ; t->next=&attoken; } fakea.naddrs=1; fakea.addrs= &a; if (!namet) namet=tokenize_name(""); if (!usert) usert=rw_rewrite_tokenize(""); a.name=namet->tokens; a.tokens=usert->tokens; p=rfc822_getaddrs(&fakea); } else { struct rfc822token *t, *u; rfca->naddrs=1; if (usert) { for (t=rfca->addrs[0].tokens; t; t=t->next) if (t->token == '@') break; for (u=usert->tokens; u->next; u=u->next) ; u->next=t; rfca->addrs[0].tokens=usert->tokens;; } if (hostt && rfca->addrs[0].tokens) { for (tp= &rfca->addrs[0].tokens; *tp; tp= &(*tp)->next) if ( (*tp)->token == '@') break; *tp=&attoken; attoken.token='@'; attoken.next=hostt->tokens; attoken.ptr=0; attoken.len=0; } if (namet) rfca->addrs[0].name=namet->tokens; p=rfc822_getaddrs(rfca); } if (!p) clog_msg_errno(); if (usert) rfc822t_free(usert); if (hostt) rfc822t_free(hostt); if (namet) rfc822t_free(namet); rfc822t_free(rfct); rfc822a_free(rfca); if (gecosname) free(gecosname); return (p); }
static void rewrite_headers(const char *From) { int seen_from=0; char headerbuf[5000]; int c, i; const char *mailuser, *mailuser2, *mailhost; char *p; char *pfrom=From ? strcpy(courier_malloc(strlen(From)+1), From):0; if ((mailuser=env("MAILUSER")) == 0 && (mailuser=env("LOGNAME")) == 0) mailuser=env("USER"); mailuser2=env("MAILUSER"); mailhost=env("MAILHOST"); while (fgets(headerbuf, sizeof(headerbuf), stdin)) { char *p=strchr(headerbuf, '\n'); if (p) { *p=0; if (p == headerbuf || strcmp(headerbuf, "\r") == 0) break; } #if HAVE_STRNCASECMP if (strncasecmp(headerbuf, "from:", 5)) #else if (strnicmp(headerbuf, "from:", 5)) #endif { fprintf(submit_to, "%s", headerbuf); if (!p) while ((c=getchar()) != EOF && c != '\n') putc(c, submit_to); putc('\n', submit_to); continue; } if (!p) while ((c=getchar()) != EOF && c != '\n') ; /* I don't care */ if (seen_from) continue; /* Screwit */ seen_from=1; i=strlen(headerbuf); for (;;) { c=getchar(); if (c != EOF) ungetc(c, stdin); if (c == EOF || c == '\r' || c == '\n') break; if (!isspace((int)(unsigned char)c)) break; while ((c=getchar()) != EOF && c != '\n') { if (i < sizeof(headerbuf)-1) headerbuf[i++]=c; } headerbuf[i]=0; } p=rewrite_from(headerbuf+5, mailuser2, mailhost, pfrom); fprintf(submit_to, "From: %s\n", p); free(p); } if (!seen_from) { if (!mailuser) { struct passwd *pw=mypwd(); mailuser=pw ? pw->pw_name:"nobody"; } if (!pfrom) { if ( !(From=env("MAILNAME")) && !(From=env("NAME"))) { pfrom=get_gecos(); } else pfrom=strcpy(courier_malloc(strlen(From)+1), From); } p=rewrite_from(NULL, mailuser, mailhost, pfrom); fprintf(submit_to, "From: %s\n", p); free(p); } putc('\n', submit_to); if (pfrom) free(pfrom); }
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; }