[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.4

Writing simple shell in C, compiles but won't run :(

Asked by bvjens31 in C Programming Language, Linux Programming, Unix Systems Programming

Tags: C, Segmentation fault (core dumped)

I am writing a simple shell program for unix that 1) takes a command from a user via standard input, 2) parses the command into a data structure, 3) parses the PATH environment, 4) looks up the command within the PATH, and 5) forks a child process to execute the command while the parent waits to terminate.

I had the first half of it working great. I could enter user input, and it would parse it correctly and keep on 'a runnin. But when I implemented the path stuff, it no longer worked how I wanted it to. I do not see where I messed up, and my debug prints inside of main() are useless because NONE of them get printed :(. Can anyone see if I messed something up within *lookupPath() or parsePath()? Or anywhere else? Thanks!!
(I tried to comment as much and as clear as possible to make navigation and understanding easy)
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:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define LINE_LEN      80
#define MAX_ARGS      64
#define MAX_ARG_LEN   16
#define MAX_PATHS     64
#define MAX_PATH_LEN  96
#define DELIM         " *.,\t\n"
 
#ifndef NULL
#define NULL ...
#endif
 
struct command_t {
  char *name;
  int  argc;
  char *argv[MAX_ARGS]; };
 
char *lookupPath(char **, char **);
int  parseCommand(char *, struct command_t *);
int  parsePath(char **);
void printPrompt();
void readCommand(char *);
 
/******************************************************************/
 
void printPrompt() {
  /* This code simply prints the shell prompt to standard out */
  char prompt[] = "jSh > ";
  printf("%s",prompt);
}
 
/******************************************************************/
 
void readCommand(char *buffer) {
  /* This code reads the entire command line into the buffer */
  fgets(buffer,LINE_LEN,stdin);
}
 
/******************************************************************/
 
int parsePath(char *dirs[]) {
  /* This function reads the PATH variable for the environment and
     then builds an array of all the directories in PATH */
  char *pathEnvVar;
  char *thePath;
  char *tmp;
  int i;
  int k;
 
  for(i=0; i<MAX_PATHS; i++)
    dirs[i] = NULL;
  pathEnvVar = (char *) getenv("PATH");
  if (pathEnvVar == NULL)
    return 0;
 
  thePath = (char *) malloc(strlen(pathEnvVar) + 1);
  strcpy(thePath, pathEnvVar);
 
  /* Parse thePath and construct dirs array while parsing*/
  for(tmp=strtok(thePath,":"); tmp!=NULL; strtok(NULL,":")) {
    dirs[k] = malloc(strlen(tmp)+1);
 
    /* Need to break out if k is NULL */
    if (dirs[k] == NULL)
      break;
 
    strcpy(dirs[k],tmp);
    k = k+1;
  }
 
  /* Return array size */
  return k;
}
 
/******************************************************************/
 
char *lookupPath(char **argv, char **dirs) {
  /* This function searches for program names within dirs */
  char *tmp;
  char *result;
  int i;
 
  /* Base case, see if file name is absolute path name */
  if(*argv[0] == '/') {
    result = argv[0];
    return result;
  }
 
  /* Look in PATH directories using access() to check for file */
  for(i=0;i<MAX_PATHS;i++) {
    if(dirs[i] == NULL)
      continue;
    /* Construct string for full PATH name */
    tmp = (char *) malloc(MAX_PATH_LEN*20);
    strcpy(tmp,dirs[i]);
    strncat(tmp,"/",MAX_PATH_LEN);
    strncat(tmp,argv[0],MAX_PATH_LEN);
 
    /* Check to see if file is in PATH and is executable */
    if(access(dirs[i],X_OK) == 0) {
      result = tmp;
      return result;
    }
  }
  
  /* Account for file not being found in any directories */
  fprintf(stderr,"%s: command not found\n",argv[0]);
  return NULL;
}
 
/******************************************************************/
 
int parseCommand(char *cLine, struct command_t *cmd) {
  /* parses a command from standard input and creates a command struct */
  int argc;
  int count;
  char **clPtr;
  clPtr = &cLine;
  argc  = 0;
 
  /* Need to fill up argv[] with all of the tokens */
  cmd->argv[argc] = (char *) malloc(MAX_ARG_LEN);
  while((cmd->argv[argc] = strsep(clPtr,DELIM)) != NULL) {
    cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN);
  }
 
  /* need to decrement argc to reflect actual # of command args */
  cmd->argc = argc-1;
 
  /* create memory for name of command to be called, and store it in name */
  cmd->name = (char *) malloc(sizeof(cmd->argv[0]));
  strcpy(cmd->name,cmd->argv[0]);
 
  /* test for exit condition */
  if(strncasecmp(cmd->name,"exit",4)==0) {
    exit(0);
  }
 
  /* for testing purposes */
  printf("The command \"%s\" has %d argument(s)\n", cmd->name,cmd->argc);
  printf("argv0 = %s\n",cmd->argv[0]);
  for(count=0;count<argc;++count) {
    printf("argv[%d] = %s\n",count,cmd->argv[count]);
  }
 
  return 1;
}
 
/******************************************************************/
 
int main() {
  /* TEST */
  printf("inside main");
 
  char *pathv[MAX_PATHS];
  char commandLine[LINE_LEN];
  struct command_t command;
  int chPID;
  int status;
  pid_t childPID;
 
  /* TEST */
  printf("almost to parsePath()");
 
  /* Get all the PATH directories */
  parsePath(pathv);
 
  /* TEST */
  printf("got to parsePath()");
 
  /* loop to accept lines from standard input and parse them */
  while(1) {
    printPrompt();
    readCommand(commandLine);
    parseCommand(commandLine, &command);
    command.argv[command.argc] = NULL;
 
    /* Get the full pathname for the file */
    command.name = lookupPath(command.argv, pathv);
    if(command.name == NULL) {
      fprintf(stderr,"command \"%s\" unknown",command.name);
      continue;
    }
 
  /* Create child and execute the command */
    if((chPID = fork()) == 0) {
      printf("Child executing: %s\n", command.name);
      execvp(command.name, command.argv);
    }
 
  /* Wait for the child to terminate */
    printf("Parent waiting for child to terminate\n");
    childPID = wait(&status);
    /* Free dynamic storage in command structure */
    free(command.name);
  }
 
/* Shell termination */
  return 0;
}
[+][-]02/11/08 03:55 PM, ID: 20871493Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/11/08 04:32 PM, ID: 20871675Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/11/08 05:00 PM, ID: 20871815Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/11/08 05:18 PM, ID: 20871893Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/11/08 05:23 PM, ID: 20871914Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/11/08 05:26 PM, ID: 20871925Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/11/08 05:39 PM, ID: 20871962Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: C Programming Language, Linux Programming, Unix Systems Programming
Tags: C, Segmentation fault (core dumped)
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 3
Solution Grade: A
 
[+][-]02/11/08 05:41 PM, ID: 20871976Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/11/08 05:46 PM, ID: 20871995Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/11/08 08:04 PM, ID: 20872444Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/11/08 10:46 PM, ID: 20872885Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]02/12/08 01:37 AM, ID: 20873462Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/12/08 01:56 AM, ID: 20873551Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/12/08 02:42 AM, ID: 20873754Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/12/08 03:52 AM, ID: 20874021Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/23/09 08:33 AM, ID: 25645406Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]10/23/09 11:16 PM, ID: 25651036Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/24/09 01:42 AM, ID: 25651309Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/31/09 04:12 AM, ID: 25709214Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 / EE_QW_2_20070628