What is wrong with my shell program

It should be able to run processes in the foreground and background. 2)List all processes 3)kill a process. The string manipulations definitely seem shady. My pointer concepts aren't really clear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <ctype.h>

//This method looks up the input command against the lookup for 
//validity purposes
bool FunctionLookUp(char *argv, char *Lookup[])
{
	bool match = false;
	for(int i = 0; i < 10; i++){
		if(*Lookup[i] == *argv){
			match = true;
			break;}
	}
	return match;
}
void  parse(char *line, char **argv)
{
     while (*line != '\0') {       /* if not the end of line ....... */ 
          while (*line == ' ' || *line == '\t' || *line == '\n')
               *line++ = '\0';     /* replace white spaces with 0    */
          *argv++ = line;          /* save the argument position     */
          while (*line != '\0' && *line != ' ' && 
                 *line != '\t' && *line != '\n') 
               line++;             /* skip the argument until ...    */
     }
     *argv = '\0';                 /* mark the end of argument list  */
}

bool CheckForwardSlash(char temp[], int size)
{
      bool present = false
      for(int i = 0; i < size; i++){
          if(temp[i] == '/'){  
           present = true;
           break;
           }
      }
      return present;
}
void  execute(char **argv)
{
     pid_t  pid;
     int    status;
     
     if ((pid = fork()) < 0) {     /* fork a child process           */
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process:         */
          if (execvp(*argv, argv) < 0) {     /* execute the command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                                  /* for the parent:      */
          while (wait(&status) != pid)       /* wait for completion  */
               ;
     }
}

  
void  main(void)
{
     char  line[1024];                         // The input line                 
     char  *argv[3];                           // An array of pointers to the tokens (the tokens were parsed 
											   // from the input line)
     bool check1, check2 = false;

	 //A look-up table that contains some of the valid commands
	 char *LookUp[11] = {"emacs", "kill", "bye", "jobs", "fg", "chmod", "cd", "help", "cat", "cp"};
     
     while (1) {                              // repeating....         
          printf("tish >> ");                 // the prompt             
          gets(line);                         // read in the command line     
          printf("\n");
          parse(line, argv);                  // parse the line  
       
          if (strcmp(argv[0], "bye") == 0)    // exit if the user enters bye
               {exit(0);}  

					  //Input Validation//
					  ///////////////////

          //If the input is just a white space character, continue with the next iteration of the loop
          if (isspace(argv[0]))
           continue;    //If it is some sort of a white-space character, 
						//skip the current iteration of the while loop
	  
			
          //Call a function that checks for the validity of the input command
		  check2 = FunctionLookUp(argv, LookUp);
		  if(check2 == false){
			  fprintf("Invalid Command\n");
			  continue;
		  }

          //Test argv[0] for invalid internal commands. Check for Letters and Negative numbers.
          if(strcmp(argv[0], "kill") == 0){
           if(isaplha(argv[1]) || atoi(argv[1]) < 0){
           fprintf("Invald PID entered");
           fprintf("Enter a positive numeric number");
           continue;
           }
           }
           
                                                     
        
         int size = sizeof(argv[1]) + 1;
         char temp[size];
		 strcpy(temp,agrv[1]);
         check1 = CheckForwardSlash(temp, size);
		 //If true is returned by the CheckForwardSlash method, skip the rest of the while loop
         if(check1 == true){                
         printf("Invalid file format\n");
         printf("Avoid Forward Slashes in your file name\n");
         continue;
         }  

					  //Input Validation Ends//
        ///////////////*********************//////////////////

		//Signals to catch the Ctrl-C and Ctrl/ combination
        signal(SIGINT, SIG_IGN);       	        //The instructions said to ignore the SIGINT signal
        signal(SIGTERM, SIG_DFL);               //SIGTERM signal must be caught. 
        execute(argv);                          //Finally, execute the command
     }
}
Last edited on
1
2
3
4
5
bool FunctionLookUp(char *argv, char *Lookup[]) {
...
  if(*Lookup[i] == *argv){
...
}


Are you trying to compare a string contents against another string contents ? Above if statement is not doing what you want. If you want to do that, you can use Standard C++ string class.



I gotcha. Besides the string manipulations, where am I off? Do you think the signal calls at the end are right
Actually to debug if your signal calls is correct is to comment out all the code and hard-code the parameters to see if they work before un-comment the rest.

e.g

signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_DFL);
//assign some hard-coded values for argv which can be execvp inside execute function
execute(argv);
Topic archived. No new replies allowed.