Command* listToCommand(const List* list) { ListNode* node; Command* command; if(list == NULL || linkedListSize(list) == 0) return NULL; command = (Command*) malloc( sizeof(Command) ); command->command = newLinkedList(); node = list->head; /* each argument is just inserted into the command's argument list */ while(node != NULL) { linkedListInsert(command->command, node->data); node = node->next; } command->infile = NULL; command->outfile = NULL; return command; }
int testInsert() { LinkedList list; linkedListInit(&list, INTEGER); _assert(linkedListInsert(&list, 1, (mixed)1) == 1); // it succeeds // 1 _assert(linkedListIndexOf(&list, (mixed)1) == 0); _assert(linkedListInsert(&list, 0, (mixed)2) == 1); // it succeeds // 2 -> 1 _assert(linkedListIndexOf(&list, (mixed)2) == 0); _assert(linkedListInsert(&list, 1, (mixed)3) == 1); // it succeeds // 2 -> 3 -> 1 _assert(linkedListIndexOf(&list, (mixed)2) == 0); _assert(linkedListIndexOf(&list, (mixed)3) == 1); _assert(linkedListIndexOf(&list, (mixed)1) == 2); return 0; }
int commandChangeDirectory(Command* cmd) { int size; int ret; char* errorMsg; if(cmd == NULL) return 0; size = linkedListSize(cmd->command); /* if the user specifies more than one argument it is an error */ if(size > 2) { fprintf(stderr, "cd: Too many arguments.\n"); return -1; } /* if the user did not specify a directory, the default behavior is to go to the user's home directory */ if(size < 2) linkedListInsert(cmd->command, getenv("HOME")); ret = chdir((char*)cmd->command->head->next->data); /* check for error */ if(ret == -1) { /* determine the specific type of error */ switch(errno) { case EACCES: errorMsg = "Search permission is denied."; break; case ELOOP: errorMsg = "A symbolic link loop was encountered."; break; case ENAMETOOLONG: errorMsg = "The pathname is too long."; break; case ENOENT: errorMsg = "The path does not exist."; break; case ENOTDIR: errorMsg = "The path is not a directory."; break; default: errorMsg = "Unknown error."; break; } printf("%s: %s\n",(char*)cmd->command->head->next->data,errorMsg); return -1; } /* otherwise we were successful */ return 0; }
/** * @see linkedListInsert */ void linkedListPrepend(LinkedList* dis, void* data) { linkedListInsert(dis, data, 0); }
/** * @see linkedListInsert */ void linkedListAppend(LinkedList* dis, void* data) { linkedListInsert(dis, data, linkedListSize(dis)); }