char* creat_dir_Command(char *command, int n_paths) { int i; char *dir; int ok = 0; while(!ok) { if(access(cat_command(command, array_paths[i]), X_OK) == 0) { dir = cat_command(command, array_paths[i]); ok = 1; }else{ i++; } } return dir; }
char* ok_command(char *command) { char dir_1[] = "/bin/"; char dir_2[] = "/usr/bin/"; char dir_3[] = "/usr/local/bin/"; if (access(cat_command(command, dir_1), X_OK)==0){ return cat_command(command, dir_1); }else if(access(cat_command(command, dir_2), X_OK)==0){ return cat_command(command, dir_2); }else if (access(cat_command(command, dir_3), X_OK)==0){ return cat_command(command, dir_3); }else { return NULL; } }
static void console_task(layer_t* layer, unsigned int mem_total) { #define INPUT_BEG_POS_X (24) #define INPUT_BEG_POS_Y (28) #define PROMPT_CHAR ("> ") #define PROMPT_LEN (2) timer_t* timer; task_t* task = task_now(); int data, fifobuf[128]; int cursor_x = INPUT_BEG_POS_X, cursor_y = INPUT_BEG_POS_Y; int cursor_c = -1; char buf[32], cmdline[32]; mem_mgr_t* memmgr = (mem_mgr_t*)MEMMGR_ADDR; fifo_init(&task->fifo, fifobuf, 128, task); timer = timer_alloc(); timer_init(timer, &task->fifo, 1); timer_settimer(timer, 50); /* display the prompt of console window */ drawstring_and_refresh(layer, 8, 28, COLOR8_FFFFFF, COLOR8_000000, PROMPT_CHAR, PROMPT_LEN); for ( ; ; ) { io_cli(); if (0 == fifo_size(&task->fifo)) { task_sleep(task); io_sti(); } else { data = fifo_get(&task->fifo); io_sti(); if (data <= 1) { /* timer for cursor */ if (0 != data) { timer_init(timer, &task->fifo, 0); if (cursor_c >= 0) cursor_c = COLOR8_FFFFFF; } else { timer_init(timer, &task->fifo, 1); if (cursor_c >= 0) cursor_c = COLOR8_000000; } timer_settimer(timer, 50); } if (2 == data) /* cursor ON */ cursor_c = COLOR8_FFFFFF; if (3 == data) { /* cursor OFF */ fill_box8(layer->buf, layer->w_size, COLOR8_000000, cursor_x, 28, cursor_x + 7, cursor_y + 15); cursor_c = -1; } if (256 <= data && data <= 511) { /* keyboard data */ if ((8 + 256) == data) { /* backspace */ if (cursor_x > INPUT_BEG_POS_X) { /* erase the cursor and move forward one character */ drawstring_and_refresh(layer, cursor_x, cursor_y, COLOR8_FFFFFF, COLOR8_000000, " ", 1); cursor_x -= 8; } } else if ((10 + 256) == data) { /* Enter Key */ /* erase cursor by space */ drawstring_and_refresh(layer, cursor_x, cursor_y, COLOR8_FFFFFF, COLOR8_000000, " ", 1); cmdline[cursor_x / 8 - 3] = 0; cursor_y = console_newline(cursor_y, layer); /* execute command */ if (0 == strcmp("mem", cmdline)) { /* check memory command */ cursor_y = memory_command(layer, memmgr, mem_total, cursor_y); } else if (0 == strcmp("clear", cmdline)) { /* clear command */ cursor_y = clear_command(layer); } else if (0 == strcmp("ls", cmdline)) { /* ls(dir) command */ cursor_y = list_command(layer, cursor_y); } else if (0 == strncmp("cat ", cmdline, 4)) { /* cat command */ cursor_y = cat_command(layer, cursor_x, cursor_y, cmdline); } else if (0 != cmdline[0]) { /* neither command nor null string */ drawstring_and_refresh(layer, 8, cursor_y, COLOR8_FFFFFF, COLOR8_000000, "Bad Command.", 12); cursor_y = console_newline(cursor_y, layer); cursor_y = console_newline(cursor_y, layer); } /* show prompt */ drawstring_and_refresh(layer, 8, cursor_y, COLOR8_FFFFFF, COLOR8_000000, PROMPT_CHAR, PROMPT_LEN); cursor_x = INPUT_BEG_POS_X; } else { /* general character */ if (cursor_x < 240) { /* display one character and move backward one character */ buf[0] = data - 256; buf[1] = 0; cmdline[cursor_x / 8 - 3] = data - 256; drawstring_and_refresh(layer, cursor_x, cursor_y, COLOR8_FFFFFF, COLOR8_000000, buf, 1); cursor_x += 8; } } } /* show the cursor again */ if (cursor_c >= 0) { fill_box8(layer->buf, layer->w_size, cursor_c, cursor_x, cursor_y, cursor_x + 7, cursor_y + 15); } layers_refresh(layer, cursor_x, cursor_y, cursor_x + 8, cursor_y + 16); } } }