Problem with a snippet of code in a shell program

Thread Solved

Join Date: Jul 2005
Posts: 60
Reputation: hinde is an unknown quantity at this point 
Solved Threads: 4
hinde hinde is offline Offline
Junior Poster in Training

Problem with a snippet of code in a shell program

 
0
  #1
Jul 8th, 2005
I am pretty much done a shell program I have to write for my operating systems class. The only problem is that I can't seem to figure out how to make this function work. It is supposed to separate out a list of tryable paths that is from the getenv("PATH") function and stick each path into an array of character arrays, but on the line of code indicated below, I get a segmentation fault. Anyway the code is below in a text file. I will post everything from the prototype, to the function call, to the function itself. I'm grateful to everybody who can look at it. Once I am done with the program I would be happy to e-mail the source to anyone who would like to take a look at it. Although aggravating, probably one of the cooler programming projects I have been assigned.

Okay so here is the line that I am getting the seg fault on:
while ((*dirs[i] = &strsep(thePath, ':')) != NULL)
Here I am trying to put the first path in the thePath array in the array dirs which will hold all of them seperately.

I have been awake since yesterday at 1, and I am getting really frustrated with this seg fault as well as my professor who has yet to respond to my e-mail asking the same thing I am asking you guys (sent it yesterday at noon).

Thanks in advance.
Attached Files
File Type: txt code piece.txt (1.3 KB, 18 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 60
Reputation: hinde is an unknown quantity at this point 
Solved Threads: 4
hinde hinde is offline Offline
Junior Poster in Training

Re: Problem with a snippet of code in a shell program

 
0
  #2
Jul 8th, 2005
How do I put the code in the text file into a nifty text box like I see around these forums?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,600
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with a snippet of code in a shell program

 
0
  #3
Jul 8th, 2005
You surround the formatted code with code tags.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 60
Reputation: hinde is an unknown quantity at this point 
Solved Threads: 4
hinde hinde is offline Offline
Junior Poster in Training

Re: Problem with a snippet of code in a shell program

 
0
  #4
Jul 8th, 2005
Awesome. Just so you guys know I am gathering the relevant parts from throughout my program. It is not bunched up like this in my source of course. Here is the code in the file:
  1. // Prototype. This has been provided by my notes.
  2. int parsePath(char **);
  3.  
  4. // Declaration of character array sent in and function call. MAX_PATHS
  5. // is equal to 64. The declaration is my own code, so I am not certain on it.
  6. char *pathv[MAX_PATHS];
  7. parsePath(pathv);
  8.  
  9. // Function itself
  10. int parsePath(char *dirs[])
  11. {
  12. /*
  13. This fucntion reads the PATH variable for this environment, then builds an
  14. array, dirs[], of the directories in PATH.
  15. */
  16. // The code until the comment below with END in it is from my notes
  17. char *pathEnvVar; // holds all the directories to check
  18. char *thePath;
  19.  
  20. int i;
  21.  
  22. for (i = 0; i < MAX_PATHS; i++)
  23. dirs[i] = NULL;
  24. pathEnvVar = (char *) getenv("PATH");
  25. thePath = (char *) malloc(strlen(pathEnvVar) + 1);
  26. strcpy(thePath, pathEnvVar);
  27.  
  28. // END
  29. /*
  30. Loop to parse thePath. Look for a ':' delimiter between each path name.
  31. */
  32.  
  33. // the rest of this function is all my code.
  34. i=0;
  35. printf("Got before the while\n"); // Here for trying to pin down the seg fault
  36. while ((*dirs[i] = &strsep(thePath, ':')) != NULL) // Here is the seg fault
  37. {
  38. printf("Got in the while\n");
  39. *dirs[++i] = &((char *) malloc(MAX_PATH_LEN));
  40. }
  41.  
  42. printf("Got past the while\n");
  43. return 1;
  44. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,600
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 712
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with a snippet of code in a shell program

 
0
  #5
Jul 8th, 2005
>*dirs[i] = &strsep(thePath, ':')
Let's look at it like this because it's easier to pick out the errors. dirs is an array of pointer to char. When you subscript dirs, you get a pointer to char, but when you dereference that, you get a single char. So we have this so far as a type map:
  1. char = &strsep(thePath, ':')
Now, since strsep returns a pointer to char, and you take the address of that pointer, you get this:
  1. char = char**
See the problem? All in all, you're thinking too hard, and casting too much:
  1. int parsePath ( char *dirs[] )
  2. {
  3. char *path = getenv ( "PATH" );
  4. char *copy;
  5. char *tok;
  6. int n = 0;
  7.  
  8. if ( path == NULL )
  9. return 0;
  10.  
  11. copy = malloc ( strlen ( path ) + 1 );
  12. strcpy ( copy, path );
  13.  
  14. for ( tok = strtok ( copy, ":" ); tok != NULL; tok = strtok ( NULL, ":" ) ) {
  15. dirs[n] = malloc ( strlen ( tok ) + 1 );
  16.  
  17. if ( dirs[n] == NULL )
  18. break;
  19.  
  20. strcpy ( dirs[n++], tok );
  21. }
  22.  
  23. return n;
  24. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 60
Reputation: hinde is an unknown quantity at this point 
Solved Threads: 4
hinde hinde is offline Offline
Junior Poster in Training

Re: Problem with a snippet of code in a shell program

 
0
  #6
Jul 8th, 2005
Well that worked like a charm. You helped me immensely. Now I just gotta debug one more function and I am home free for the summer.

If you are ever in Millersville PA drop me an im cause I owe you a beer.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC