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 = &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.

How do I put the code in the text file into a nifty text box like I see around these forums?

You surround the formatted code with code tags.

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:

// 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 = &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:

char = &strsep(thePath, ':')

Now, since strsep returns a pointer to char, and you take the address of that pointer, you get this:

char = char**

See the problem? All in all, you're thinking too hard, and casting too much:

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;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.