Shell/myshell.c

145 lines
3.1 KiB
C
Raw Normal View History

2019-02-11 12:27:35 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <string.h>
#include <time.h>
2019-02-11 21:05:34 +01:00
#define BUFFERSIZE 255
#define BLU "\x1B[34m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define RESET "\x1B[0m"
2019-02-11 12:27:35 +01:00
void help(){
printf(BLU " Krystian Janowicz \n Available commands: \n echo [arg] \n pwd \n cd [arg] \n clear \n date \n help \n exit \n ls \n" RESET);
}
void clear(){
printf("\033[H\033[J");
2019-02-11 12:27:35 +01:00
}
void pwd(){
2019-02-11 21:05:34 +01:00
char path[BUFFERSIZE];
getcwd(path,BUFFERSIZE);
printf(GRN " %s" RESET,path);
2019-02-11 12:27:35 +01:00
}
2019-02-21 19:20:22 +01:00
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);
}
2019-02-11 12:27:35 +01:00
void prompt(){
char *user;
char *path;
2019-02-11 21:05:34 +01:00
char hostname[BUFFERSIZE];
2019-02-11 12:27:35 +01:00
user = getenv( "USER" );
2019-02-11 21:05:34 +01:00
memset(hostname, 0, BUFFERSIZE);
gethostname(hostname, BUFFERSIZE);
printf(YEL "%s@" RESET, user);
printf(YEL "%s" RESET, hostname);
pwd();
2019-02-11 12:27:35 +01:00
printf(" $ ");
}
2019-02-21 19:20:22 +01:00
int echo(char command[BUFFERSIZE]){
char *checkThatSign = " ";
int lenght=strlen(command);
for(int i=0;i<=lenght-1; i++){
2019-02-11 12:27:35 +01:00
char temp;
2019-02-21 19:20:22 +01:00
temp=command[i];
2019-02-11 12:27:35 +01:00
if(temp==' '){
2019-02-21 19:20:22 +01:00
char *rest = strpbrk(command, checkThatSign);
printf("%s", rest);
2019-02-11 21:05:34 +01:00
return 0;
2019-02-11 12:27:35 +01:00
}
}
}
2019-02-21 19:20:22 +01:00
int exxit(char command[BUFFERSIZE]){
char *checkThatSign = " ";
int lenght=strlen(command);
char temp;
2019-02-11 12:27:35 +01:00
2019-02-21 19:20:22 +01:00
for(int i=0;i<=lenght-1; i++){
2019-02-11 12:27:35 +01:00
2019-02-21 19:20:22 +01:00
temp=command[i];
if(temp==' '){
return command[i+1]-48;
2019-02-21 19:20:22 +01:00
}
}
return 0;
2019-02-11 12:27:35 +01:00
}
2019-02-21 19:20:22 +01:00
void cd(char command[BUFFERSIZE]){
2019-02-11 21:05:34 +01:00
char *tok;
2019-02-21 19:20:22 +01:00
tok = strchr(command,' ');
2019-02-11 21:05:34 +01:00
char *tempTok = tok + 1;
tok = tempTok;
char *locationOfNewLine = strchr(tok, '\n');
if(locationOfNewLine) {
*locationOfNewLine = '\0';
}
chdir(tok);
}
2019-02-11 12:27:35 +01:00
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;
2019-02-11 21:05:34 +01:00
printf( "%s ", dp->d_name);
2019-02-11 12:27:35 +01:00
}
closedir(dir);
}
int main(int argc, char **argv) {
2019-02-21 19:20:22 +01:00
char command[BUFFERSIZE];
2019-02-11 12:27:35 +01:00
do{
2019-02-11 21:05:34 +01:00
prompt();
2019-02-21 19:20:22 +01:00
bzero(command, BUFFERSIZE);
fgets(command, BUFFERSIZE, stdin);
2019-02-11 12:27:35 +01:00
if (strcmp(command, "help\n") == 0){ help(); }
2019-02-21 19:20:22 +01:00
else if (strcmp(command, "ls\n") == 0){
2019-02-11 12:27:35 +01:00
chdir(*(++argv));
list_dir(".");
2019-02-11 21:05:34 +01:00
printf("\n");
2019-02-11 12:27:35 +01:00
}
2019-02-21 19:20:22 +01:00
else if (strcmp(command, "pwd\n") == 0){
pwd();
2019-02-11 21:05:34 +01:00
printf("\n");
2019-02-11 12:27:35 +01:00
}
else if (strcmp(command, "date\n") == 0){ date(); }
else if (strcmp(command, "clear\n") == 0){ clear(); }
2019-02-21 19:20:22 +01:00
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')){ return exxit(command); }
else{ printf ("ERROR: didn't found that command, try 'help' \n" ) ; }
2019-02-11 12:27:35 +01:00
}while(1);
}