| | |
Problem with a snippet of code in a shell program
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 60
Reputation:
Solved Threads: 4
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.
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.
•
•
Join Date: Jul 2005
Posts: 60
Reputation:
Solved Threads: 4
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:
C Syntax (Toggle Plain Text)
// Prototype. This has been provided by my notes. int parsePath(char **); // Declaration of character array sent in and function call. MAX_PATHS // is equal to 64. The declaration is my own code, so I am not certain on it. char *pathv[MAX_PATHS]; parsePath(pathv); // Function itself int parsePath(char *dirs[]) { /* This fucntion reads the PATH variable for this environment, then builds an array, dirs[], of the directories in PATH. */ // The code until the comment below with END in it is from my notes char *pathEnvVar; // holds all the directories to check char *thePath; int i; for (i = 0; i < MAX_PATHS; i++) dirs[i] = NULL; pathEnvVar = (char *) getenv("PATH"); thePath = (char *) malloc(strlen(pathEnvVar) + 1); strcpy(thePath, pathEnvVar); // END /* Loop to parse thePath. Look for a ':' delimiter between each path name. */ // the rest of this function is all my code. i=0; printf("Got before the while\n"); // Here for trying to pin down the seg fault while ((*dirs[i] = &strsep(thePath, ':')) != NULL) // Here is the seg fault { printf("Got in the while\n"); *dirs[++i] = &((char *) malloc(MAX_PATH_LEN)); } printf("Got past the while\n"); return 1; }
>*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:
Now, since strsep returns a pointer to char, and you take the address of that pointer, you get this:
See the problem? All in all, you're thinking too hard, and casting too much:
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:
C Syntax (Toggle Plain Text)
char = &strsep(thePath, ':')
C Syntax (Toggle Plain Text)
char = char**
C Syntax (Toggle Plain Text)
int parsePath ( char *dirs[] ) { char *path = getenv ( "PATH" ); char *copy; char *tok; int n = 0; if ( path == NULL ) return 0; copy = malloc ( strlen ( path ) + 1 ); strcpy ( copy, path ); for ( tok = strtok ( copy, ":" ); tok != NULL; tok = strtok ( NULL, ":" ) ) { dirs[n] = malloc ( strlen ( tok ) + 1 ); if ( dirs[n] == NULL ) break; strcpy ( dirs[n++], tok ); } return n; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Is there any problem wid ths code? (PHP)
- shell issues (Visual Basic 4 / 5 / 6)
- help me--find command-shell program in unix (Shell Scripting)
- Problem with View More Messages title appearing (JavaScript / DHTML / AJAX)
Other Threads in the C Forum
- Previous Thread: Starting a Project
- Next Thread: binary search
| Thread Tools | Search this Thread |
#include * ansi api array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send sequential shape single socket socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi







