Hi
I need to expand some shell code so it would be able to do more than it is doing now. Currently it can do stuff like quit when q is pressed, display a prompt but not much more. I have to make the shell have capabilities like
cd (changing directories)
redirection of input and output, for example date > storeddate.txt
pipelines, for example ps -ax | grep programname
background processes, for example gcc -c verylargeprogram.c
etc..
this has to be done under a POSIX 1 compliant system.
i have been given the following code:
-----------------------------
-------------------------------------------------------------------Code:/* * simplesh.h */ #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/wait.h> #include <limits.h> #define DELIMITERSET " ><|&" #ifndef MAX_CANON #define MAX_CANON 256 #endif #define TRUE 1 #define FALSE 0 #define BLANK_STRING " " #define PROMPT_STRING "-> " #define QUIT_STRING "q" #define BACK_STRING "&" #define PIPE_STRING "|" #define NEWLINE_STRING "\n" #define IN_REDIRECT_SYMBOL '<' #define OUT_REDIRECT_SYMBOL '>' #define NULL_SYMBOL '\0' #define PIPE_SYMBOL '|' #define BACK_SYMBOL '&' #define NEWLINE_SYMBOL '\n' int makeargarray (char *s, char *delimiters, char ***argvp); int parsefile (char *inbuf, char delimiter, char **v); int redirect (char *infilename, char *outfilename); void executecmdline (char *cmd); int connectpipeline (char *cmd, int frontfd[], int backfd[]); ----------------------------------------------------------------------- /* makeargarray.c */ #include "simplesh.h" /* Make arg array (*arvp) for tokens in s which are separated by * delimiters. Return -1 on error, or the number of tokens otherwise. */ int makeargv (char *s, char *delimiters, char ***argvp) { char *t; char *snew; int numtokens; int i; /* snew is the start of string after skipping leading delimiters */ snew = s + strspn (s, delimiters); /* Create space for a copy of snew in t */ if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL) { *argvp = NULL; numtokens = -1; } else /* count the number of tokens in snew */ { strcpy (t, snew); if (strtok(t, delimiters) == NULL) numtokens = 0; else for (numtokens=1; strtok(NULL,delimiters) != NULL; numtokens++) ; /* create an argument array to contain pointers to tokens */ if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL) { free (t); numtokens = -1; } else /* insert pointers to tokens into the array */ { if (numtokens > 0) { strcpy (t, snew); **argvp = strtok (t, delimiters); for (i = 1; i < numtokens + 1; i++) *((*argvp) + i) = strtok (NULL, delimiters); } else { **argvp = NULL; free (t); } } } return numtokens; } -------------------------------------------------------------------------------- /* * simplesh2.c * * A simple shell - second attempt */ #include "simplesh.h" int main (void) { char inbuff[MAX_CANON+1]; pid_t child_pid; while (1) { fputs (PROMPT_STRING, stdout); if (fgets(inbuff, MAX_CANON, stdin) == NULL) break; if (*(inbuff + strlen(inbuff) - 1) == NEWLINE_SYMBOL) *(inbuff + strlen(inbuff) - 1) = 0; if (strcmp(inbuff, QUIT_STRING) == 0) break; else { if ((child_pid = fork()) == 0) { char **chargv; if (makeargv(inbuff, BLANK_STRING, &chargv) > 0) { if (execvp(chargv[0], chargv) == -1) { perror ("Invalid command"); exit (1); } } exit (1); } else if (child_pid > 0) wait (NULL); } } return 0; }
how do i do this? where do i find info on this sort of stuff?
thanx everyone



LinkBack URL
About LinkBacks



.