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 PARALLEL_DELIM "&"
- 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 s=' ';
- if(strstr(cwd,homeDir)!=NULL)
- {
- removeSubstring(cwd,homeDir);
- s='~';
- }
- fprintf(stdout, "\033[33;1m%s@%s:\033[0m\e[32m%c%s$\033[0m", username, hostname,s, cwd);
- }
- char *lsh_read_line(void)
- {
- 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, char * delim)
- {
- int bufsize = LSH_TOK_BUFSIZE, position = 0;
- char **tokens = malloc(bufsize * sizeof(char*));
- char *token;
- if (!tokens) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
- token = strtok(line, delim);
- while (token != NULL) {
- tokens[position] = token;
- position++;
- if (position >= bufsize) {
- bufsize += LSH_TOK_BUFSIZE;
- tokens = realloc(tokens, bufsize * sizeof(char*));
- if (!tokens) {
- fprintf(stderr, "lsh: allocation error\n");
- exit(EXIT_FAILURE);
- }
- }token = strtok(NULL, LSH_TOK_DELIM);
- }
- tokens[position] = NULL;
- return tokens;
- }
- int lsh_launch(char **args)
- {
- pid_t pid, wpid;
- int status;
- pid = fork();
- if (pid == 0) {
- // Child process
- if (execvp(args[0], args) == -1) {
- perror("lsh");
- }
- exit(EXIT_FAILURE);
- } else if (pid < 0) {
- // Error forking
- perror("lsh");
- } else {
- // Parent process
- do {
- printf("parent process\n");
- wpid = waitpid(pid, &status, WUNTRACED);
- } while (!WIFEXITED(status) && !WIFSIGNALED(status));
- //exit loop if the child process terminated normally
- }
- printf("parent process exited\n");
- //printf("%s process of pid:%d is exited with status:%d",args[0],pid,status);
- return 1;
- }
- /*
- Function Declarations for builtin shell commands:
- */
- int lsh_cd(char **args);
- int lsh_help(char **args);
- int lsh_exit(char **args);
- int pinfo(char **args);
- /*
- List of builtin commands, followed by their corresponding functions.
- */
- char *builtin_str[] = {
- "cd",
- "help",
- "exit",
- "pinfo"
- };
- int (*builtin_func[]) (char **) = {
- &lsh_cd,&lsh_help,
- &lsh_exit,&pinfo
- };
- int lsh_num_builtins() {
- return sizeof(builtin_str) / sizeof(char *);
- }
- /*
- Builtin function implementations.
- */
- int lsh_cd(char **args)
- {
- if (args[1] == NULL) {
- fprintf(stderr, "lsh: expected argument to \"cd\"\n");
- } else {
- if (chdir(args[1]) != 0) {
- perror("lsh");
- }
- }
- return 1;
- }
- int lsh_help(char **args)
- {
- int i;
- printf("Stephen Brennan's LSH\n");
- printf("Type program names and arguments, and hit enter.\n");
- printf("The following are built in:\n");
- for (i = 0; i < lsh_num_builtins(); i++) {
- printf(" %s\n", builtin_str[i]);
- }
- printf("Use the man command for information on other programs.\n");
- return 1;
- }
- int lsh_exit(char **args)
- {
- return 0;
- }
- int lsh_execute(char **args)
- {
- int i;
- if (args[0] == NULL) {
- // An empty command was entered.
- return 1;}
- for (i = 0; i < lsh_num_builtins(); i++) {
- if (strcmp(args[0], builtin_str[i]) == 0) {
- return (*builtin_func[i])(args);
- }
- }
- 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("Child processs\n");
- //printf("i:%d\ncommand:%s\n",i,commands[i]);
- args = lsh_split_line(commands[i],LSH_TOK_DELIM);
- run=lsh_execute(args);
- exit(EXIT_FAILURE);
- printf("uff\n");
- }
- }
- //printf("parent processs\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;
- getcwd(cwd, 1000);
- printStylo(cwd);
- line = lsh_read_line();
- if(strstr(line,"&")!=NULL)
- {
- commands = lsh_split_line(line,PARALLEL_DELIM);
- run=ksh_run(commands);
- }
- else{
- commands=lsh_split_line(line,LSH_TOK_DELIM);
- run=lsh_execute(commands);
- }
- //printf("done running\n");
- free(line);
- free(commands);
- } while (run);
- }
- int main(int argc, char **argv)
- {
- // Load config files, if any.
- // Run command loop.
- lsh_loop();
- // Perform any shutdown/cleanup.
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment