944,068 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3072
  • C RSS
Jul 8th, 2005
0

Problem with a snippet of code in a shell program

Expand Post »
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, 42 views)
Similar Threads
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
hinde is offline Offline
60 posts
since Jul 2005
Jul 8th, 2005
0

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

How do I put the code in the text file into a nifty text box like I see around these forums?
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
hinde is offline Offline
60 posts
since Jul 2005
Jul 8th, 2005
0

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

You surround the formatted code with code tags.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 8th, 2005
0

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

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. }
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
hinde is offline Offline
60 posts
since Jul 2005
Jul 8th, 2005
0

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

>*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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 8th, 2005
0

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

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.
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
hinde is offline Offline
60 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Starting a Project
Next Thread in C Forum Timeline: binary search





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC