/* * TODO - sort output */ int aliascmd(int argc, char **argv) { char *n, *v; int ret = 0; struct alias *ap; if (argc == 1) { int i; for (i = 0; i < ATABSIZE; i++) for (ap = atab[i]; ap; ap = ap->next) { printalias(ap); } return (0); } while ((n = *++argv) != NULL) { if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */ if ((ap = *__lookupalias(n)) == NULL) { outfmt(out2, "%s: %s not found\n", "alias", n); ret = 1; } else printalias(ap); } else { *v++ = '\0'; setalias(n, v); } } return (ret); }
int aliascmd(int argc, char **argv) { char *n, *v; int ret = 0; struct alias *ap; if (argc == 1) { printaliases(); return (0); } while ((n = *++argv) != NULL) { if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */ if ((ap = lookupalias(n, 0)) == NULL) { outfmt(out2, "alias: %s not found\n", n); ret = 1; } else printalias(ap); else { *v++ = '\0'; setalias(n, v); } } return (ret); }
void login(struct USER *me) { int recvd; if(isconnected) { fprintf(stderr, "Juz jestes zalogowany %s:%d\n", ipcon, portnom); return; } sockfd = connect_with_server(); if(sockfd >= 0) { isconnected = 1; me->sockfd = sockfd; if(strcmp(me->alias, "Anonymous")) setalias(me); printf("Zalogowano jako %s\n", me->alias); printf("Nasluchiwanie rozpoczete [%d]...\n", sockfd); struct THREADINFO threadinfo; pthread_create(&threadinfo.thread_ID, NULL, receiver, (void *)&threadinfo); } else { fprintf(stderr, "Polaczenie odrzucone...\n"); } }
/* * TODO - sort output */ int aliascmd(int argc, char **argv) { char *n, *v; int ret = 0; struct alias *ap; if (argc == 1) { int i; for (i = 0; i < ATABSIZE; i++) for (ap = atab[i]; ap; ap = ap->next) { if (*ap->name != '\0') { out1fmt("alias %s=", ap->name); print_quoted(ap->val); out1c('\n'); } } return (0); } while ((n = *++argv) != NULL) { if ((v = strchr(n+1, '=')) == NULL) { /* n+1: funny ksh stuff */ if ((ap = lookupalias(n, 0)) == NULL) { outfmt(out2, "alias: %s not found\n", n); ret = 1; } else { out1fmt("alias %s=", n); print_quoted(ap->val); out1c('\n'); } } else { *v++ = '\0'; setalias(n, v); } } return (ret); }
int client(int argc, char *argv[]) { int sockfd, aliaslen; int portno = atoi(argv[2]); portnom=portno; ipcon = gethostbyname(argv[1]); memset(&me, 0, sizeof(struct USER)); //Komendy while(gets(option)) { if(!strncmp(option, "exit", 4)) { logout(&me); break; } if(!strncmp(option, "help", 4)) { FILE *fin = fopen("help.txt", "r"); if(fin != NULL) { while(fgets(option, LINEBUFF-1, fin)) puts(option); fclose(fin); } else { fprintf(stderr, "Plik pomocy nie znaleziony...\n"); } } else if(!strncmp(option, "login", 5)) { char *ptr = strtok(option, " "); ptr = strtok(0, " "); memset(me.alias, 0, sizeof(char) * ALIASLEN); if(ptr != NULL) { aliaslen = strlen(ptr); if(aliaslen > ALIASLEN) ptr[ALIASLEN] = 0; strcpy(me.alias, ptr); } else { strcpy(me.alias, "Anonymous"); } login(&me); } else if(!strncmp(option, "alias", 5)) { char *ptr = strtok(option, " "); ptr = strtok(0, " "); memset(me.alias, 0, sizeof(char) * ALIASLEN); if(ptr != NULL) { aliaslen = strlen(ptr); if(aliaslen > ALIASLEN) ptr[ALIASLEN] = 0; strcpy(me.alias, ptr); setalias(&me); } } else if(!strncmp(option, "whisp", 5)) { char *ptr = strtok(option, " "); char temp[ALIASLEN]; ptr = strtok(0, " "); memset(temp, 0, sizeof(char) * ALIASLEN); if(ptr != NULL) { aliaslen = strlen(ptr); if(aliaslen > ALIASLEN) ptr[ALIASLEN] = 0; strcpy(temp, ptr); while(*ptr) ptr++; ptr++; while(*ptr <= ' ') ptr++; sendtoalias(&me, temp, ptr); } } else if(!strncmp(option, "send", 4)) { sendtoall(&me, &option[5]); } else if(!strncmp(option, "logout", 6)) { logout(&me); } else fprintf(stderr, "Zla komenda...\n"); } return 0; }
void execute_cmd(void) { currcmd = currcmd % MAXCMDS; int curr = currcmd; FILE *f; if(comtab[curr].external == 0) { // Built-in Command aliasroot = NULL; aliasDepth = 0; switch(comtab[curr].code) { case CHD : { if( chdir(getHOME) ) { printf("ERROR at line %d\n", __LINE__); break; } setenv("PWD", getHOME, 1); break; } case CDX : { char* dest = cleanInput(comtab[curr].atptr[0]); if( chdir(dest) == -1 ) { printf("ERROR: \n%s is not a directory\n", dest); } char pwd[5000]; getcwd( pwd, sizeof(pwd) ); setenv("PWD", pwd, 1); break; } case SETENV : { char* name = cleanInput(comtab[curr].atptr[0]); char* word = cleanInput(comtab[curr].atptr[1]); if( setenv( name, word, 1 ) == -1 ) { printf("setenv failed, could not set %s to %s\n", name, word ); } break; } case UNSETENV : { char* name = cleanInput(comtab[curr].atptr[0]); if( getenv(name) ){ unsetenv(name); } else { printf("unsetenv failed, could not find %s\n", name); } break; } case PRINTENV : { if(ofileredir) { if(comtab[curr].append) { f = fopen(comtab[curr].outfd, "a"); } else { f = fopen(comtab[curr].outfd, "w"); } if(f == NULL) return SYSERR; } else print_env(); if(ofileredir) fclose(f); break; } case SETALIAS : { char* name = cleanInput(comtab[curr].atptr[0]); char* word = cleanInput(comtab[curr].atptr[1]); setalias(name, word); break; } case UNALIAS : { char* name = cleanInput(comtab[curr].atptr[0]); removealias(name); break; } case PRINTALIAS : { if(ofileredir) { if(comtab[curr].append) { f = fopen(comtab[curr].outfd, "a"); } else { f = fopen(comtab[curr].outfd, "w"); } } else printalias(); if(ofileredir) fclose(f); break; } case PWD : { printf("%s\n", getPWD); break; } } } else { // Handle aliasing int acurr = isalias(comtab[curr].comname); if(acurr != -1) { comtab[curr].external = 0; if(aliasroot == NULL) { aliasroot = aliastab[acurr].alname; } // Check for infinite aliasing if( aliasDepth > 30 ) { printf("ERR: Infinite aliasing detected. Exiting...\n"); return; } else { ignoreEOF = 1; parse_string(aliastab[acurr].alstring); aliasDepth++; execute_cmd(); } } else { // External Command aliasroot = NULL; aliasDepth = 0; pid_t child = fork(); int stat; int success = -1; while(waitpid(child, &stat, 0) == -1) { if(errno != EINTR) { stat = -1; break; } } if(child < 0) exit(1); else if(child == 0) { // Prepare for execv call char tmp[256]; char *paths = strcpy(tmp, getenv("PATH")); char *tok = strtok(paths, ":"); char *cmp = "./"; while(tok) { char place[255]; if(comtab[curr].comname[0] == cmp[0] || comtab[curr].comname[0] == cmp[1]) { // If destination is specified strcpy(place, comtab[curr].comname); } else { // If destination is not specified strcpy(place, tok); strcat(place, "/"); // Append command name strcat(place, comtab[curr].comname); } char *cmds[comtab[curr].nargs + 2]; cmds[0] = place; cmds[comtab[curr].nargs + 1] = (char *)NULL; int i = 0; for(i; i<comtab[curr].nargs; i++) { cmds[i+1] = comtab[curr].atptr[i]; } if(execv(place, cmds) == -1) { tok = strtok(NULL, ":"); continue; } else { _exit(0); success = 1; break; } } if(success == -1) { printf("ERR: Command not found: %s\n", comtab[curr].comname); _exit(1); } } } } currcmd += 1; comtab[currcmd].external = 0; ignoreEOF = 0; }