/* * 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); }
static void printaliases(void) { int i, j; struct alias **sorted, *ap; sorted = ckmalloc(aliases * sizeof(*sorted)); j = 0; for (i = 0; i < ATABSIZE; i++) for (ap = atab[i]; ap; ap = ap->next) if (*ap->name != '\0') sorted[j++] = ap; qsort(sorted, aliases, sizeof(*sorted), comparealiases); for (i = 0; i < aliases; i++) printalias(sorted[i]); ckfree(sorted); }
void setalias(char* command) { /* syntax: alias [-p] word=string */ char** aliasoption=malloc(1*sizeof(char**)); char* p; parser(command, &aliasoption); //remove quotation mark in each argument like: alias dir="ls -aF" int j=0; while (aliasoption[j]!='\0') { char *tmp; if ((tmp=strstr(aliasoption[j], "\""))!=NULL) { char* squzeed=malloc(1*sizeof(char)); int i=0,m=0; while (aliasoption[j][i]!='\0') { if (aliasoption[j][i]=='"') { i++; //skip '"' } else { m++; squzeed=realloc(squzeed, (m+1)*sizeof(char)); squzeed[m-1]=aliasoption[j][i]; i++; } } squzeed[m]='\0'; //aliasoption[j]=realloc(aliasoption[j], (strlen(squzeed)+1)*sizeof(char)); //strcpy(aliasoption[j],squzeed); aliasoption[j]=strdup(squzeed); free(squzeed); squzeed=NULL; } j++; } if (aliasoption[1]=='\0') //no argument { printallalias(); } else if (strcmp(aliasoption[1],"-p")==0) //argument is "-p" { printallalias(); int i=2; while (aliasoption[i]!='\0') //if after argument "-p" there are still arguments { //consider the rest as alias name and print out p=strstr(aliasoption[i],"="); if (p==NULL||p-aliasoption[i]==0) //alias =ls no alias name before '=' { if (first!=NULL) { printalias(aliasoption[i]); } } else { char* name=malloc((p-aliasoption[i])*sizeof(char)); char* word=malloc(strlen(aliasoption[i])-(p-aliasoption[i])-1); strncpy(name,aliasoption[i],(p-aliasoption[i])); strcpy(word,p+1); addalias(name,word); free(name); name=NULL; free(word); word=NULL; } i++; } } // if after "alias" the first argument is not empty nor "-p" // then consider them as alias names print them out else { int i=1; while (aliasoption[i]!='\0') { p=strstr(aliasoption[i],"="); if (p==NULL||p-aliasoption[i]==0) { printalias(aliasoption[i]); } else { char* name=malloc((p-aliasoption[i]+1)*sizeof(char)); char* word=malloc(strlen(aliasoption[i])-(p-aliasoption[i])-1); strncpy(name,aliasoption[i],(p-aliasoption[i])); name[p-aliasoption[i]]='\0'; strcpy(word,p+1); addalias(name,word); free(name); name=NULL; free(word); word=NULL; } i++; } } //release memory space int i=0; while (aliasoption[i]!='\0') { free(aliasoption[i]); aliasoption[i]=NULL; i++; } free(aliasoption); }
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; }