static int timeInterrupt(IRQRegisters *reg,void *data) { u64 rflags; ++ticks; for(;;){ lockSpinLockCloseInterrupt(&timerLock,&rflags); if(listEmpty(&timers)) break; /*Just break if empty.*/ Timer *timer = listEntry(timers.next,Timer,list); if(timer->ticks > ticks) break; /*Break if timers are not timeout.*/ listDelete(&timer->list); /*Timeout!Delete it.*/ unlockSpinLockRestoreInterrupt(&timerLock,&rflags); (*timer->callback)(timer->data); /*Call callback.*/ } unlockSpinLockRestoreInterrupt(&timerLock,&rflags); /*Need unlock.*/ return 0; }
int main(int argc, char *argv[]) { //variables declaration FILE *file = fopen(argv[1],"r"); //open file given by user char ch; char x[50]; int i = 0; //create new list list *list; listCreate(&list); if(file!=NULL) { //word only if file exists while(fscanf(file, "%s", x)!=EOF) { //read string from file listAdd(x, list); //call listAdd to add word } fclose(file); //close file after operation list_sort(list); //sort the list according to word count listPrint(list); //print the list listDelete(list); //free the list free(list); } return 0; //bye! }
/* *Function : mlistDeleteList *Description: delete list from mlist *Parameters : hlist - mlist handle * list - list number to get 'next' element *Return : TRUE id sucess, otherwise FALSE */ BOOL mlistDeleteList(HLIST hlist, int list) { /*first - delete all elements of the list, than - delete reference to the list in mlist */ mlistDeleteAll(hlist, list); return listDelete(hlist, list); }
soc::SocSystem_Ors::~SocSystem_Ors(){ if(WS->newedOrs){ listDelete(vars); delete swift; delete gl; delete ors; } delete WS; }
void listTest(void) { List *list; int i; list = createList(); printf("delete when list is empty\n"); listDelete(list, 0); printf("insert 0\n"); listInsert(list, 0, 0); listTraverse(list); printf("delect position:0\n"); listDelete(list, 0); listTraverse(list); printf("insert 5 element to list\n"); for(i = 0; i < 5; i++) { listInsert(list, 5 - i, 0); } listTraverse(list); printf("insert 55 to position 3\n"); listInsert(list, 55, 3); listTraverse(list); printf("delete first tree element\n"); for(i = 0; i < 3; i++) { listDelete(list, 0); } listTraverse(list); printf("delete position 1\n"); listDelete(list, 1); listTraverse(list); printf("get position 3\n"); printf("postion 3:%d\n", listGet(list, 3)); destroyList(list); }
/* ====================================================================== Function: init Purpose : configure ULPNode I/O ports Input : - Output : - Comments: - ====================================================================== */ void TInfo::init() { // free up linked list (in case on recall init()) listDelete(); // clear our receive buffer clearBuffer(); // We're in INIT in term of receive data _state = TINFO_INIT; }
/* *Function : mlistDelete *Description: delete an element located at 'location' from the list in mlist *Parameters : hlist - mlist handle * list - list number to delete element from * location - element to delete *Return : TRUE if success, otherwise FALSE */ BOOL mlistDelete (HLIST hlist, int list, int location) { mListDesc *mlist=(mListDesc*)listGetElem(hlist, list); if (!mlist) return FALSE; if (!mlist->count) return FALSE; if (mlist->tail==location) mlist->tail=listPrev(hlist,location); if (mlist->tail==list) mlist->tail=RVERROR; if (!listDelete(hlist, location)) return FALSE; mlist->count--; return TRUE; }
int removeTimer(Timer *timer) { u64 rflags; lockSpinLockCloseInterrupt(&timerLock,&rflags); listDelete(&timer->list); /*A list can be deleted more than one times.*/ /*So this will never be failed.*/ unlockSpinLockRestoreInterrupt(&timerLock,&rflags); if(!timer->onStack) kfree(timer); return 0; }
int etimerDelete(HETIMER timer, HETIMERELEM tNode) { etimerStruct *tm = (etimerStruct *)timer; int location = (int)tNode; etimerNode *node; int loc; if (!timer) return RVERROR; /* NULL function */ if (! (node = (etimerNode*)listGetElem(tm->timer, location) )) return RVERROR; loc = (int)(bheapDeleteNode(tm->heap, (BHeapNode*)node->heapRef )); if (loc!=location) { loc=location; } listDelete(tm->timer, location); return TRUE; }
int main(int argc, char* argv[]) { head_ptr ls_head = (head_ptr)malloc(sizeof(head)); ls_head->head = NULL; list ele_x1; ele_x1.key = 1; list ele_x2; ele_x2.key = 4; list ele_x3; ele_x3.key = 16; list ele_x4; ele_x4.key = 9; listInsert(ls_head, &ele_x1); listInsert(ls_head, &ele_x2); listInsert(ls_head, &ele_x3); listInsert(ls_head, &ele_x4); printList(ls_head); list_ptr ele_find = listSearch(ls_head, 4); if (ele_find != NULL) printf("find element of key in 4\n"); else printf("find not element of key in 4\n"); list ele_x5; ele_x5.key = 25; listInsert(ls_head, &ele_x5); printList(ls_head); if (ele_find != NULL) { listDelete(ls_head, ele_find); printList(ls_head); } free(ls_head); ls_head = NULL; return 0; }
// pass number of terminals on commandline int main(int argc, char *argv[]) { struct virtualTerminal *vtPointer; // nCurses related variables WINDOW *ncursesScreen; int screenWidth, screenHeight, vtColumns, vtRows; // to hold screen's height and width // Miscellaneous variables unsigned int input; char ch; int in; char inputString[33]; int i, j, k, done, fdMax, fdCurrent, inputSize; int *vtCoordinates, *vtCoordPointer; fd_set fileDescriptors; struct timespec timeout; struct sigaction sa; linkedList *llTerminals; linkedListNode *llNode, *llCurrent; //mtrace(); // Set up signal handling memset(&sa, 0, sizeof(sa)); sa.sa_handler = &signalHandler; sigaction(SIGTERM, &sa, NULL); // Initializations llTerminals = listNew(); done = fdMax = 0; timeout.tv_sec = 0; timeout.tv_nsec = 500000000; // 10E-9 // Initialize terminal ncursesScreen = initscr(); if(ncursesScreen == NULL) { perror("Error initializing nCurses\n"); exit(1); } if(has_colors()) { start_color(); init_pair(1, COLOR_WHITE, COLOR_BLACK); init_pair(2, COLOR_BLACK, COLOR_WHITE); init_pair(3, COLOR_RED, COLOR_BLACK); init_pair(4, COLOR_BLUE, COLOR_BLACK); init_pair(5, COLOR_WHITE, COLOR_BLACK); } //keypad(ncursesScreen, true); // cause nCurses to package key combos into special, single values //raw(); noecho(); // don't echo input refresh(); // clear the main window // Get main window dimensions. This will be used when creating additional virtual terminals getmaxyx(ncursesScreen, screenHeight, screenWidth); #ifdef DEBUG fprintf(stderr, "Height %d Width %d\n", screenHeight, screenWidth); #endif //vtRows = (vtRows / 2); // - (vtRows % 4); //vtCols = (vtCols / 2); // - (vtCols % 4); //fprintf(stderr, "Rows: %d Cols: %d\n", vtRows, vtCols); // set j equal to number of terminals if(argc > 1) j = atoi(argv[1]); else j = 1; vtPointer = vtCreate(screenWidth, 2, 0, screenHeight - 2, 0); // pass width, height, x, y, number listAppendCargo(llTerminals, vtPointer); screenHeight -= 2; vtCoordPointer = vtCoordinates = divideRectangle_favorHeight(screenWidth, screenHeight, j, 0); for(i = 0; i < j; i++) { vtPointer = vtCreate(*(vtCoordPointer + 2), *(vtCoordPointer + 3), *vtCoordPointer, *(vtCoordPointer + 1), i + 1); // pass width, height, x, y, number if(vtPointer == NULL) break; listAppendCargo(llTerminals, vtPointer); vtCoordPointer += 4; } llCurrent = llNode = listFirst(llTerminals); vtPointer = (struct virtualTerminal*) nodeCargo(llNode); fdCurrent = vtPointer->fd; fprintf(stderr, "fd %d\n",fdCurrent); k = 0; while(!done) { FD_ZERO(&fileDescriptors); FD_SET(0, &fileDescriptors); fdMax = 0; llNode = listFirst(llTerminals); while(llNode != NULL) { vtPointer = (struct virtualTerminal*) nodeCargo(llNode); if(vtPointer->fd > 0 && vtPointer->state != VT_PAUSED) { // fd will be -1 if shell program has exited j = vtPointer->fd; if(j > fdMax) fdMax = j; FD_SET(j, &fileDescriptors); } llNode = listNext(llNode); } pselect(fdMax + 1, &fileDescriptors, NULL, NULL, &timeout, NULL); // process keyboard input, if any if(FD_ISSET(0, &fileDescriptors)) { //inputSize = read(0, &ch, 1); in = getch(); switch(in) { //inputString[i]) { case '`': // tab vtPointer = (struct virtualTerminal*) nodeCargo(llCurrent); if(vtPointer->wBorder) { wattron(vtPointer->wBorder, COLOR_PAIR(1)); box(vtPointer->wBorder, ACS_VLINE, ACS_HLINE); mvwprintw(vtPointer->wBorder, 0, 0, "%i ", vtPointer->id); wnoutrefresh(vtPointer->wBorder); } llCurrent = listNext(llCurrent); if(llCurrent == NULL) llCurrent = listFirst(llTerminals); vtPointer = (struct virtualTerminal*) nodeCargo(llCurrent); if(vtPointer->wBorder) { wattron(vtPointer->wBorder, COLOR_PAIR(3)); box(vtPointer->wBorder, ACS_VLINE, ACS_HLINE); mvwprintw(vtPointer->wBorder, 0, 0, "%i ", vtPointer->id); wattroff(vtPointer->wBorder, COLOR_PAIR(3)); wnoutrefresh(vtPointer->wBorder); } doupdate(); //vtPointer = (struct virtualTerminal*) to->pointer; //wcolor_set(vtPointer->window, 1, NULL); k++; if(k == llTerminals->length) k = 0; fdCurrent = vtPointer->fd; break; case '~': done = 1; break; /* case KEY_ENTER: ch = '\n'; write(fdCurrent, &ch, 1); break; */ default: ch = (char)in; write(fdCurrent, &ch, 1); fprintf(stderr, "wrote: %c\n", ch); break; } } j = 0; // temporarily keep track of whether any windows were updated llNode = listFirst(llTerminals); while(llNode != NULL) { vtPointer = (struct virtualTerminal*) nodeCargo(llNode); if(vtPointer->fd > 0 && FD_ISSET(vtPointer->fd, &fileDescriptors) && vtPointer->state != VT_PAUSED) { // fd will be -1 if shell has closed //tesi_handleInput(to); // handle escape sequences VTCore_dispatch(vtPointer->core); //if(vtPointer->wBorder) //wnoutrefresh(vtPointer->wBorder); // updates backbuffered nCurses window wnoutrefresh(vtPointer->window); // updates backbuffered nCurses window j = 1; // specifies whether we should flush nCurses buffered windows } llNode = listNext(llNode); } if(j) doupdate(); // updates were made, wnoutrefresh() was called, now flush updates switch(previousSignal) { case SIGTERM: fprintf(stderr, "Caught SIGTERM\n"); done = 1; break; default: break; } } //strcpy(inputString, "\nexit\n"); /* llNode = listFirst(llTerminals); while(llNode != NULL) { vtPointer = (struct virtualTerminal*) nodeCargo(llNode); kill(vtPointer->pid, 9); //write(to->pipeToChild[1], &inputString, strlen(inputString)); // send exit to bash waitpid(vtPointer->pid, NULL, 0); // bash should have quit by the time it gets here vtDestroy(vtPointer); llNode = listNext(llNode); } * */ free(vtCoordinates); listDelete(llTerminals, vtDestroy); endwin(); return 0; }
// pass number of terminals on commandline int main(int argc, char *argv[]) { // Virtual Terminal related struct virtualTerminal *vt; // nCurses related variables WINDOW *ncursesScreen; int screenWidth, screenHeight, vtColumns, vtRows; // to hold screen's height and width // Miscellaneous variables unsigned int input; char ch; char inputString[33]; int i, j, k, done, fdMax, fdCurrent, inputSize; int *vtCoordinates, *vtPointer; fd_set fileDescriptors; struct timespec timeout; struct sigaction sa; linkedList *llTerminals; linkedListNode *llNode; //mtrace(); // Set up signal handling memset(&sa, 0, sizeof(sa)); sa.sa_handler = &signalHandler; sigaction(SIGTERM, &sa, NULL); // Initializations llTerminals = listNew(); done = fdMax = 0; timeout.tv_sec = 0; timeout.tv_nsec = 500000000; // 10E-9 // Initialize terminal ncursesScreen = initscr(); if(ncursesScreen == NULL) { perror("Error initializing nCurses\n"); exit(1); } if(has_colors()) { start_color(); } keypad(ncursesScreen, true); // cause nCurses to package key combos into special, single values noecho(); // don't echo input refresh(); // clear the main window // Get main window dimensions. This will be used when creating additional virtual terminals getmaxyx(ncursesScreen, screenHeight, screenWidth); //vtRows = (vtRows / 2); // - (vtRows % 4); //vtCols = (vtCols / 2); // - (vtCols % 4); //fprintf(stderr, "Rows: %d Cols: %d\n", vtRows, vtCols); // set j equal to number of terminals if(argc > 1) j = atoi(argv[1]); else j = 1; //vtPointer = vtCoordinates = rectangleCoordinates_favorWidth(screenWidth, screenHeight, j, 1); vtPointer = vtCoordinates = rectangleCoordinates_favorHeight(screenWidth, screenHeight, j, 0); for(i = 0; i < j; i++) { vt = vtCreate( *(vtPointer + 2), *(vtPointer + 3), *vtPointer, *(vtPointer + 1), i); listAppendCargo(llTerminals, vt); vtPointer += 4; } llNode = listFirst(llTerminals); vt = (struct virtualTerminal*) nodeCargo(llNode); fdCurrent = vt->fd; //fprintf(stderr, "fd %d\n",fdCurrent); k = 0; while(!done) { FD_ZERO(&fileDescriptors); FD_SET(0, &fileDescriptors); llNode = listFirst(llTerminals); fdMax = 0; while(llNode != NULL) { vt = (struct virtualTerminal*) nodeCargo(llNode); if(vt->fd > 0 && vt->state != VT_PAUSED) { // fd will be -1 if shell program has exited j = vt->fd; if(j > fdMax) fdMax = j; FD_SET(j, &fileDescriptors); } llNode = listNext(llNode); } pselect(fdMax + 1, &fileDescriptors, NULL, NULL, &timeout, NULL); if(FD_ISSET(0, &fileDescriptors)) { inputSize = read(0, &ch, 1); //for(i = 0; i < inputSize; i++) { switch(ch) { //inputString[i]) { case '`': // tab wcolor_set(((struct virtualTerminal*)listNodeIndex(llTerminals ,k))->window, 1, NULL); k++; if(k == llTerminals->length) k = 0; fdCurrent = TtyTerminalIO_get_associated_fd( ((struct virtualTerminal*)listNodeIndex(llTerminals ,k))->terminalIO ); break; case '~': done = 1; break; default: //ch = inputString[i]; write(fdCurrent, &ch, 1); break; } //} } j = 0; // temporarily keep track of whether any windows were updated llNode = listFirst(llTerminals); while(llNode != NULL) { vt = (struct virtualTerminal*) nodeCargo(llNode); if(vt->fd > 0 && FD_ISSET(vt->fd, &fileDescriptors) && vt->state != VT_PAUSED) { // fd will be -1 if shell has closed VTCore_dispatch(vt->core); wnoutrefresh(vt->wBorder); // updates backbuffered nCurses window wnoutrefresh(vt->window); // updates backbuffered nCurses window j = 1; // specifies whether we should flush nCurses buffered windows } llNode = listNext(llNode); } if(j) doupdate(); // updates were made, wnoutrefresh() was called, now flush updates switch(previousSignal) { case SIGTERM: fprintf(stderr, "Caught SIGTERM\n"); done = 1; break; default: break; } } llNode = listFirst(llTerminals); while(llNode != NULL) { vt = (struct virtualTerminal*) nodeCargo(llNode); //fprintf(stderr,"Terminal %d\n", vt->id); llNode = listNext(llNode); } free(vtCoordinates); listDelete(llTerminals, vtDestroy); endwin(); return 0; }
int eventsDel(Iter iter) { listDelete(&iter); return E_OK; }
void gVertexDelete(GVertex **v) { free((*v)->content); listDelete(&((*v)->LAdjacents)); free(&v); }
int triangulation(LIST *srchead, TRG *trghead){ int s1, s2, s3; POINT *TRGp1, *TRGp2, *TRGp3 ,*TRGtemp; EDGE *ptr ; EDGE *check_edge_ptr; LIST *check_point_list; LIST *originallist; EDGE *cpptr; TRGp1 = NULL; TRGp2 = NULL; TRGp3 = NULL; originallist = (LIST*)malloc(sizeof(LIST)); memcpy(originallist, srchead, sizeof(srchead)); check_edge_ptr = (EDGE*)malloc(sizeof(EDGE)); check_point_list = NULL; cpptr = srchead->head; for(;;){ check_point_list = listLink(check_point_list, ¤t, cpptr->p1.x, cpptr->p1.y, cpptr->p2.x, cpptr->p2.y, 1); cpptr = cpptr->next; if(cpptr == srchead->head) break; } ptr = srchead->head; while(1){ if(TRGp1 != NULL){ while(1){ TRGp2->x= TRGp3->x; TRGp2->y= TRGp3->y; ptr = ptr-> next; TRGp3->x= ptr->p1.x; TRGp3->y= ptr->p1.y; while(check_triangle(TRGp1->x, TRGp1->y, TRGp2->x, TRGp2->y, TRGp3->x, TRGp3->y, check_point_list) == 0){ if(srchead->count == 3){ TRGp1->x = srchead->head->p1.x; TRGp1->y = srchead->head->p1.y; TRGp2->x = srchead->head->next->p1.x; TRGp2->y = srchead->head->next->p1.y; TRGp3->x = srchead->rear->p1.x; TRGp3->y = srchead->rear->p1.y; if(check_triangle(TRGp1->x, TRGp1->y, TRGp2->x, TRGp2->y, TRGp3->x, TRGp3->y, check_point_list) != 0){ //檢查三角形第一條邊 check_edge_ptr->p1.x = srchead->head->p1.x; check_edge_ptr->p1.y = srchead->head->p1.y; check_edge_ptr->p2.x = srchead->head->next->p1.x; check_edge_ptr->p2.y = srchead->head->next->p1.y; s1 = check_edge_same(check_edge_ptr, srchead); //檢查三角形第二條邊 check_edge_ptr->p1.x = srchead->head->next->p1.x; check_edge_ptr->p1.y = srchead->head->next->p1.y; check_edge_ptr->p2.x = srchead->rear->p1.x; check_edge_ptr->p2.y = srchead->rear->p1.y; s2 = check_edge_same(check_edge_ptr, srchead); //檢查三角形第三條邊 check_edge_ptr->p1.x = srchead->rear->p1.x; check_edge_ptr->p1.y = srchead->rear->p1.y; check_edge_ptr->p2.x = srchead->head->p1.x; check_edge_ptr->p2.y = srchead->head->p1.y; s3 = check_edge_same(check_edge_ptr, srchead); trianglehead = TRGlink(trianglehead, &TRGprvious, &TRGcurrent, TRGp1, TRGp2, TRGp3, s1, s2, s3); } listDelete(listhead, TRGp2->x, TRGp2->y, TRGp3->x, TRGp3->y); return 0; } else if(srchead->count < 3) return 0; else{ PrintList(srchead); TRGp1->x = TRGp2->x; TRGp1->y = TRGp2->y; TRGp2->x = TRGp3->x; TRGp2->y = TRGp3->y; ptr = ptr-> next; TRGp3->x = ptr->p1.x; TRGp3->y = ptr->p1.y; } } break; } } else{ TRGp1 = (POINT*)malloc(sizeof(POINT)); TRGp2 = (POINT*)malloc(sizeof(POINT)); TRGp3 = (POINT*)malloc(sizeof(POINT)); TRGp1->x = ptr->p1.x; TRGp1->y = ptr->p1.y; ptr = ptr-> next; TRGp2->x = ptr->p1.x; TRGp2->y = ptr->p1.y; ptr = ptr-> next; TRGp3->x = ptr->p1.x; TRGp3->y = ptr->p1.y; if(srchead->count == 3){ trianglehead = TRGlink(trianglehead, &TRGprvious, &TRGcurrent, TRGp1, TRGp2, TRGp3, 1, 1, 1); listDelete(srchead, TRGp2->x, TRGp2->y, TRGp3->x, TRGp3->y); return 0; } while(check_triangle(TRGp1->x, TRGp1->y, TRGp2->x, TRGp2->y, TRGp3->x, TRGp3->y, check_point_list) == 0){ TRGp1->x = TRGp2->x; TRGp1->y = TRGp2->y; TRGp2->x = TRGp3->x; TRGp2->y = TRGp3->y; ptr = ptr-> next; TRGp3->x = ptr->p1.x; TRGp3->y = ptr->p1.y; //找下一個三角形,直到沒有點在此三角形內部為止 } } //檢查三角形第一條邊(此邊是外框或內框) check_edge_ptr->p1.x = TRGp1->x; check_edge_ptr->p1.y = TRGp1->y; check_edge_ptr->p2.x = TRGp2->x; check_edge_ptr->p2.y = TRGp2->y; s1 = check_edge_same(check_edge_ptr, srchead); //檢查三角形第二條邊(此邊是外框或內框) check_edge_ptr->p1.x = TRGp2->x; check_edge_ptr->p1.y = TRGp2->y; check_edge_ptr->p2.x = TRGp3->x; check_edge_ptr->p2.y = TRGp3->y; s2 = check_edge_same(check_edge_ptr, srchead); //檢查三角形第三條邊(此邊是外框或內框) check_edge_ptr->p1.x = TRGp3->x; check_edge_ptr->p1.y = TRGp3->y; check_edge_ptr->p2.x = TRGp1->x; check_edge_ptr->p2.y = TRGp1->y; s3 = check_edge_same(check_edge_ptr, srchead); //加入三角形linked-list內 trianglehead = TRGlink(trianglehead, &TRGprvious, &TRGcurrent, TRGp1, TRGp2, TRGp3, s1, s2, s3); listDelete(srchead, TRGp2->x, TRGp2->y, TRGp3->x, TRGp3->y); list_link_orphan(srchead); } free(check_point_list); return 1; }
int main(int argc, char **argv) { //Establish anything we'll need constantly. int mode = 1;//Start in sequential mode. int usepath = 0;//does our path file exist? int futuremode = mode;//This keeps track of mode changes until the end of the line. int printPrompt = 0; char *prompt = "s-term> ";//The prompt string. printf("%s", prompt);//Print the prompt. fflush(stdout); //do pathstuff char **paths = readFile("shell-config"); if(paths != NULL){ usepath = 1; } char **firststep = NULL;//The array of commands made by splitting the buffer along semicolons. char ***secondstep = NULL;//The array of commands, with each command split along whitespace into its arguments. struct node *head = NULL;//The head of the linked list of ongoing jobs. char buffer[1024];//The buffer. while (1) {// struct pollfd pfd = {0, POLLIN}; if(printPrompt){ //Need to reprint the prompt. printf("%s",prompt); fflush(stdout); printPrompt = 0; } int rv = poll(&pfd, 1, 1000); if (rv==0){ //No change, use time to do other tasks struct node *anode = head; while(anode != NULL){ int pstatus = 0; int pstate = waitpid((*anode).pid,&pstatus,WNOHANG); if(pstate>0){ //Process has returned; print confirmation message and delete this node. printf("Command %s, id %i was executed.\n",(*anode).command, anode->pid); anode = (*anode).next; listDelete(pstate, &head); printPrompt = 1; } else if(pstate<0){ //Error in waitpid, print error message and break from while loop. printf("Error retrieving process status.\n"); break; } else{ //Process has not returned. anode = (*anode).next; } } } else if (rv < 0){ //Poll went horribly wrong and we're bailing out of the flaming wreckage, screaming at the tops of our lungs. printf("Polling error; shutting the (s)hell down."); } else { //Keyboard I/O if(fgets(buffer, 1024, stdin) != NULL){ mode = futuremode;//Ensure that mode is up-to-date. //Remove any comments after a hash. removeComment(buffer); //Tokenize buffer by semicolons- each will be an executable command. firststep = tokenify(buffer,";"); secondstep = tokenify2(firststep," \t\n"); //Free firststep, as it is no longer needed. Free the sub-arrays first, then the array proper. freeAll1(firststep); free(firststep); int j = 0; int futureExit = 0; int status = 0; pid_t p = 1; //Execute all commands in a loop. while(secondstep[j] != NULL && secondstep[j][0] != NULL){ //check for the special commands mode or exit. If neither of these, fork and execv. if(!strcasecmp(secondstep[j][0],"exit")){ int canwequit = 1; struct node *tmp = head; while(tmp != NULL){ if(tmp->state != 1){ canwequit = 0; } tmp = tmp->next; } if (canwequit){ futureExit = 1;//Will be checked at the end of the loop. } else { printf("Error: Jobs are currently running. Please wait for tasks to finish before exiting.\n"); } } else if(!strcasecmp(secondstep[j][0],"pause")){ setState(atoi(secondstep[j][1]), 1, head); if(kill(atoi(secondstep[j][1]), SIGSTOP) != -1){ printf("Paused process %i\n",atoi(secondstep[j][1])); } else{ printf("Something went terribly wrong\n"); } } else if(!strcasecmp(secondstep[j][0],"resume")){ setState(atoi(secondstep[j][1]), 0, head); if(kill(atoi(secondstep[j][1]), SIGCONT) != -1){ printf("Resumed process %i\n",atoi(secondstep[j][1])); } } else if(!strcasecmp(secondstep[j][0],"jobs")){ struct node *tmp = head; printf("\npid cmd paused\n"); while(tmp != NULL){ printf("%i %s %i\n",tmp->pid,tmp->command,tmp->state); tmp = tmp->next; } } /*else if(!strcasecmp(secondstep[j][0],"path")&& !strcasecmp(secondstep[j][1],"refresh")){ if(paths != NULL){ freeAll1(paths); free(paths); } //do pathstuff char **paths = readFile("shell-config"); if(paths == NULL){ usepath = 0; }else{ usepath = 1; } }*/ else if(!strcasecmp(secondstep[j][0],"MODE")){ if(secondstep[j][1] == NULL){ if(mode == 0){ printf("\nCurrent mode is parallel\n"); } else { printf("\nCurrent mode is sequential\n"); } } else if(!strcasecmp(secondstep[j][1],"PARALLEL") || !strcasecmp(secondstep[j][1],"p")){ futuremode = 0; } else if(!strcasecmp(secondstep[j][1],"SEQUENTIAL") || !strcasecmp(secondstep[j][1],"s")){ futuremode = 1; } else { //Bullshit users with their bullshit commands - throw an error. printf("\nError: Your command was pretty awful.\n"); } } else{ //Fork and execute/wait depending on process id. p = fork(); if (p == 0){ break;//Child processes handled outside the while loop. } if(mode==1){//Sequential mode. wait(&status); //Do something with childp; error checking, probably } else {//Parallel mode; add this to the list listInsert(p, secondstep[j][0], 0, &head); } } j++; } if (p == 0){ if(usepath==1){ int k = 0; while(paths[k] != NULL){ struct stat sr; char tempbuffer[1024]; strcpy(tempbuffer, paths[k]); strcat(tempbuffer, "/"); strcat(tempbuffer, secondstep[j][0]); int rv = stat(tempbuffer, &sr); if (rv < 0){ k++; } else{ secondstep[j][0]=tempbuffer; if(execv(secondstep[j][0],secondstep[j])<0){ exit(0); } } } } //Execv for an actual, non-hardcoded command. printf("\n%s\n",secondstep[j][0]); if(execv(secondstep[j][0],secondstep[j])<0){ fprintf(stderr, "Your command failed, and here's why you're a bad person: %s\n", strerror(errno)); } exit(0);//Close out the child process corpse. } //check if there was an exit command earlier if(futureExit == 1){ break; } //If we don't exit, free current buffer freeAll2(secondstep); free(secondstep); //Make sure firststep and secondstep have an assigned value in case of early termination. firststep = NULL; secondstep = NULL; printf("%s", prompt); fflush(stdout); } if(feof(stdin)){ break;//End of file or Ctrl+D } } } //on a quit, flush our command array if it's not null already if(secondstep != NULL){ freeAll2(secondstep); free(secondstep); } //Free the paths array as well. if(paths!=NULL){ freeAll1(paths); free(paths); } //Check time spent in user mode and kernel mode. Right now I've got it separated by shell and processes, but we can add it together later. int idParent = RUSAGE_SELF; int idChild = RUSAGE_CHILDREN; int statParent = 0; int statChildren = 0; struct rusage dataParent; struct rusage dataChildren; statParent = getrusage(idParent, &dataParent); statChildren = getrusage(idChild, &dataChildren); if(!statParent){//If the getrvalue operation was a success printf("Shell time in user mode: %ld.%06ld seconds.\n", dataParent.ru_utime.tv_sec, dataParent.ru_utime.tv_usec); printf("Shell time in kernel mode: %ld.%06ld seconds. \n", dataParent.ru_stime.tv_sec, dataParent.ru_stime.tv_usec); } if(!statChildren){ printf("Process time in user mode: %ld.%06ld seconds. \n", dataChildren.ru_utime.tv_sec, dataChildren.ru_utime.tv_usec); printf("Process time in kernel mode: %ld.%06ld seconds. \n", dataChildren.ru_stime.tv_sec, dataChildren.ru_stime.tv_usec); } exit(0); //Ctrl+D will drop you down here; need to make sure any cleanup also happens here too. return 0; }
void graphDelete(Graph **G) { listDelete(G); }