#include #include #include #include #include #include #include #include #include #include #include #include #include const int max = 1000; void cut(char **argv, char *command){ int w = 0; char sep[] = " "; char *ptr = strtok(command, sep); while (ptr != NULL){ argv[w]=ptr; w+=1; ptr = strtok(NULL, sep); } argv[w]=NULL; } void rmChar(char *str, char garbage) { /*przesledzic jak to dziala!!!!! w opisie sobie napisac*/ char *src, *dst; for (src = dst = str; *src != '\0'; src++) { *dst = *src; if (*dst != garbage) dst++; } *dst = '\0'; } int cd (char **argv){ if(argv[1] == NULL){ printf("Add arguments\n"); return -1; } else if(argv[2] != NULL){ printf("Too many arguments\n"); return -1; } else{ if (chdir(argv[1]) != 0){ perror("chdir error"); return -1; } return 0; } } void spathf (char **argv, int *spath){ int l; if (argv[1] == NULL){ printf("Add arguments\n"); return; } else if (argv[2] != NULL){ printf("Too many arguments\n"); return; } else if (!strcmp(argv[1], "help")){ printf("Arguments:\n0 turns off path showing\n1 turns on path showing\n"); return; } l = argv[1][0] - '0'; if (argv[1][1] != 0 || l < 0 || l > 1){ printf("Wrong number\n"); return; } else *spath = l; } void help (char **argv){ if (fork() == 0){ execlp("less", "less", "help", NULL); perror("execlp"); exit(EXIT_FAILURE); } else wait(NULL); } void exe (char **argv){ if (fork() == 0){ execvp(argv[0], argv); perror("Comand execution error"); exit(EXIT_FAILURE); } else wait(NULL); } void ptab (int ile){ int i; for (i=0; id_name; if (!(!strcmp(name, ".") || !strcmp(name, ".."))){ /*jesli nazwa rozna od . lub .. */ if (fork()==0){ if (chdir(name) == 0){ /*udalo nam sie wejsc do katalogu*/ closedir(d); ptab(tab); printf("\033[1;34m%s\033[0m\n", name); getcwd(ndr, sizeof(ndr)); /*zmieniamy directory*/ d = opendir(ndr); tab++; } else{ ptab(tab); printf("%s\n", name); exit(0); } } else wait(NULL); } } closedir(d); } exit(0); } else{ wait(NULL); return; } } } void mycp (char **argv){ if (argv[1] == NULL || argv[2] == NULL){ if(argv[1] != NULL && argv[2] == NULL) if (!strcmp(argv[1], "help")){ printf("Needs exacly 2 arguments\ncopyies file from first argument to file in second argument\n"); return; } printf("Add arguments\nfor more info type 'mycp help'\n"); return; } else if (argv[3] != NULL){ printf("Too many arguments\nfor more info type 'mycp help'\n"); return; } else{ int file1 = open(argv[1], O_RDONLY); if (file1 == -1){ perror("open error"); return; } if (open(argv[2], O_RDONLY) != -1){ printf("%s already exists!\n", argv[2]); return; } int newfile = creat(argv[2], 0666); if (newfile == -1){ perror("create error"); return; } char bufor[1024]; int r; while ((r = read(file1, bufor, sizeof(bufor))) > 0) write(newfile, bufor, r); close(file1); close(newfile); } } void myps (char **argv){ if (argv[1] != NULL){ if(!strcmp(argv[1], "help")){ printf("Showps prints all prosess and pids\ntakes no arguments\n"); } else printf("Type 'showps help'\n"); return; } if (fork() == 0){ if (chdir("/proc") != 0){ perror("cant open /proc"); exit(0); } else{ /*weszlismy w 1 fork do katalogu proc */ DIR *d; struct dirent *ent; int i, p; if ((d = opendir("/proc")) != NULL){ /*otwieramy proc*/ while ((ent = readdir(d)) != NULL){ /*czytamy zawartosc*/ char *name = ent->d_name; p=0; for (i=0; ipw_name)+22 *sizeof(char)); prompt[0]='\0'; strcat(prompt, "\033[1;32m"); strcat(prompt, "["); strcat(prompt, directory); strcat(prompt, "]\033[0m"); strcat(prompt, p->pw_name); } else{ prompt = (char*)malloc(strlen(p->pw_name)+3 *sizeof(char)); strcat(prompt, p->pw_name); } strcat(prompt, "$ "); if (!(komenda = readline(prompt))) break; if (komenda[0] == 0) continue; add_history(komenda); cut(argv, komenda); while (argv[i] != NULL){ rmChar(argv[i], 34); i+=1; } i=0; if (argv[0]==NULL) continue; else if (!strcmp(argv[0], "spath")){ spathf(argv, &spath); continue; } else if (!strcmp(argv[0], "cd")){ cd(argv); continue; } else if (!strcmp(argv[0], "help")){ help(argv); continue; } else if (!strcmp(argv[0], "showtree")){ tree(argv, directory); continue; } else if (!strcmp(argv[0], "mycp")){ mycp(argv); continue; } else if (!strcmp(argv[0], "showps")){ myps(argv); continue; } else if (!strcmp(argv[0], "exit")) break; else exe(argv); free(komenda); free(prompt); } return 0; }