#include #include #include #include #include #include #include #include #include #include #define BUFFERSIZE 255 #define BLU "\x1B[34m" #define GRN "\x1B[32m" #define YEL "\x1B[33m" #define RESET "\x1B[0m" void help(){ printf(BLU " Krystian Janowicz \n Available commands: \n echo [arg] \n pwd \n cd [arg] \n date \n help \n exit \n ls \n" RESET); } void pwd(){ char path[BUFFERSIZE]; getcwd(path,BUFFERSIZE); printf(GRN " %s" RESET,path); } void date(){ time_t t = time(NULL); struct tm *tm = localtime(&t); char s[64]; strftime(s, sizeof(s), "%c", tm); printf("%s\n", s); } void prompt(){ char *user; char *path; char hostname[BUFFERSIZE]; user = getenv( "USER" ); memset(hostname, 0, BUFFERSIZE); gethostname(hostname, BUFFERSIZE); printf(YEL "%s@" RESET, user); printf(YEL "%s" RESET, hostname); pwd(); printf(" $ "); } int echo(char command[BUFFERSIZE]){ char *checkThatSign = " "; int lenght=strlen(command); for(int i=0;i<=lenght-1; i++){ char temp; temp=command[i]; if(temp==' '){ char *rest = strpbrk(command, checkThatSign); printf("%s", rest); return 0; } } } int exxit(char command[BUFFERSIZE]){ char *checkThatSign = " "; int lenght=strlen(command); for(int i=0;i<=lenght-1; i++){ char temp; temp=command[i]; char *checkThatSign = " "; if(temp==' '){ char *rest = strpbrk(command, checkThatSign); return rest[1]-48; } else{ return 0; } } } void cd(char command[BUFFERSIZE]){ char *tok; tok = strchr(command,' '); char *tempTok = tok + 1; tok = tempTok; char *locationOfNewLine = strchr(tok, '\n'); if(locationOfNewLine) { *locationOfNewLine = '\0'; } chdir(tok); } void list_dir(char *name) { DIR *dir; struct dirent *dp; struct stat statbuf; if ((dir = opendir(name)) == NULL) { perror("Blad"); } while ((dp = readdir(dir)) != NULL) { if (stat(dp->d_name, &statbuf) == -1) continue; if (dp->d_name[0] == '.') continue; printf( "%s ", dp->d_name); } closedir(dir); } int main(int argc, char **argv) { char command[BUFFERSIZE]; do{ prompt(); bzero(command, BUFFERSIZE); fgets(command, BUFFERSIZE, stdin); if (strcmp(command, "help\n") == 0){ help(); } else if (strcmp(command, "ls\n") == 0){ chdir(*(++argv)); list_dir("."); printf("\n"); } else if (strcmp(command, "pwd\n") == 0){ pwd() ; printf("\n"); } else if (strcmp(command, "date\n") == 0){ date() ; } else if((command[0]=='c') && (command[1]=='d')) { cd(command); } else if ((command[0]=='e') && (command[1]=='c') && (command[2]=='h') && (command[3]=='o')){ echo(command); } else if ((command[0]=='e') && (command[1]=='x') && (command[2]=='i') && (command[3]=='t')){ int r=exxit(command); return r; } else{ printf ("ERROR: didn't found that command, try 'help' \n" ) ; } }while(1); }