Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <pwd.h>
- #include <signal.h>
- #define LSH_TOK_BUFSIZE 64
- #define LSH_TOK_DELIM " \t\r\n\a"
- #define KSH_SPLIT_COMMAND ";"
- char homeDir[1000],hostname[1000];
- char *username;
- void p(char * l) {
- printf("%s\n",l);
- }
- void removeSubstring(char *s,const char *toremove)
- {
- while( s=strstr(s,toremove) )
- memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
- }
- void printStylo(char * cwd)
- {
- char *temp_cwd;
- if(strstr(cwd,homeDir)!=NULL)
- {
- removeSubstring(cwd,homeDir);
- }
- fprintf(stdout, "\033[33;1m%s@%s:\033[0m\e[32m%s$\033[0m", username, hostname, cwd);
- }
- char *lsh_read_line(void)
- {
- //printf("This is read\n");
- char *line = NULL;
- ssize_t bufsize = 0; // have getline allocate a buffer for us
- getline(&line, &bufsize, stdin);
- return line;
- }
- char **lsh_split_line(char *line,int flag)
- {
- //printf("This is split\n");
- int bufsize = LSH_TOK_BUFSIZE, position = 0;
- char **tokens = malloc(bufsize * sizeof(char*));
- char *token;
- if (!tokens) {
- fprintf(stderr, "KShell: allocation error\n");
- exit(EXIT_FAILURE);
- }
- //printf("Line:%s",line);
- if(flag==0)
- token = strtok(line, LSH_TOK_DELIM);
- else
- token = strtok(line, KSH_SPLIT_COMMAND);
- while (token != NULL) {
- //printf("Before:%s",token);
- tokens[position] = token;
- position++;
- if (position >= bufsize) {
- bufsize += LSH_TOK_BUFSIZE;
- tokens = realloc(tokens, bufsize * sizeof(char*));
- if (!tokens) {
- fprintf(stderr, "KShell: allocation error\n");
- exit(EXIT_FAILURE);
- }
- }
- if(flag==0)
- token = strtok(NULL, LSH_TOK_DELIM);
- else
- token = strtok(NULL, KSH_SPLIT_COMMAND);
- //printf("After:%s",token);
- }
- tokens[position] = NULL;
- return tokens;
- }
- int lsh_launch(char **args)
- {
- //printf("This is launch\n");
- pid_t pid, wpid;
- int status,bufsize=LSH_TOK_BUFSIZE;
- //printf("args:%s\n",args[0]);
- pid = fork();
- if (pid == 0) {
- // Child process
- //printf("This is child\n");
- if (execvp(args[0], args) == -1) {
- perror("KShell Error:");
- }
- exit(EXIT_FAILURE);
- } else if (pid < 0) {
- // Error forking
- perror("KShell Error");
- } else {
- //printf("This is parent\n");
- // Parent process
- do {
- wpid = waitpid(pid, &status, WUNTRACED);
- } while (!WIFEXITED(status) && !WIFSIGNALED(status));
- //exit loop if the child process terminated normally
- }
- return 1;
- }
- int lsh_execute(char **args)
- {
- //printf("\nThis is exec args:%s\n",args[0]);
- if (args[0] == NULL) {
- // An empty command was entered.
- return 1;
- }
- if(strcmp("quit",args[0])==0){
- //printf("fu\n");
- return 0;
- }
- return lsh_launch(args);
- }
- int ksh_run(char **commands)
- {
- int run=1,status;
- char **args;
- pid_t pid,wpid;
- for(int i=0;commands[i]!=NULL;i++)
- {
- if(strcmp("quit",commands[i])==0){
- run=0;
- }
- pid=fork();
- if(pid==0)
- {
- //printf("i:%d\ncommand:%s\n",i,commands[i]);
- args = lsh_split_line(commands[i],0);
- run=lsh_execute(args);
- exit(EXIT_FAILURE);
- printf("uff\n");
- }
- }
- do {
- wpid = waitpid(pid, &status, WUNTRACED);
- } while (!WIFEXITED(status) && !WIFSIGNALED(status));
- return run;
- }
- void lsh_loop(void)
- {
- char *line;
- char **commands;
- int status,flag,run=1;
- char home[]="HOME:";
- if(gethostname(hostname, sizeof(hostname)) == -1)
- perror("gethostname() error\n");
- char cwd[1000];
- getcwd(cwd, 1000);
- strcat(home,cwd);
- strcpy(homeDir,cwd);
- struct passwd *pass;
- pass = getpwuid(getuid());
- username = pass->pw_name;
- do {
- flag=1;
- printStylo(cwd);
- line = lsh_read_line();
- commands=lsh_split_line(line,1);
- run=ksh_run(commands);
- } while (run);
- }
- int main(int argc, char **argv)
- {
- //printf("This is main\n");
- lsh_loop();
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment