Question:
Segmentation fault on C?
CookiJar
2013-01-24 08:59:52 UTC
When i run it, it give me segmentation fault. Can anyone please help me fix this. Try so many ways.



#include
#include
#include
#include
#include

#define MAX_ARGS 64
#define LINE_LEN 80
#define MAX_ARG_LEN 16
#define MAX_PATHS 64
#define MAX_PATH_LEN 96
#define WHITESPACE " .,\n\t"

#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 *);

int parsePath(char* dirs[]) {
int i;
int j = 1;
char *parsePathEnv;
char *thePath;
for(i=0; i dirs[i] = NULL;
parsePathEnv = (char *) getenv("PATH");
thePath = (char*) malloc(strlen(parsePathEnv) + 1);
strcpy(thePath, parsePathEnv);
dirs[j] = strtok(thePath, ":");
//j = 1;
while(1)
{
dirs[j] = strtok(NULL, ":");
if(dirs[j] == NULL)
break;
j++;
}
}

char *lookupPath(char **argv, char **dirs) {
char *result;
char pName[MAX_PATH_LEN];
int i;
if(*argv[0] == '/')
{
if(access(argv[0], X_OK) == 0)
{
result = (char *) malloc(strlen(argv[0]) + 1);
strcpy(result, argv[0]);
}
else
{
printf("\nError!");
result = NULL;
}
}
else
{
for(i=0; i {
strcpy(pName, dirs[i]);
strcat(pName, "/");
strcat(pName, argv[0]);
if(access(pName, X_OK) == 0)
{
result = (char *) malloc(strlen(pName) + 1);
strcpy(result, pName);
}
else
{
printf("\nError!");
result = NULL;
}
}

}
return result;
}

void printPrompt() {
char promptString[] = "osProj1# ";
printf("%s", promptString);
}

void readCommand(char *buffer) {
gets(buffer);
}

int parseCommand(char *cLine, struct command_t *cmd) {
int argc;
char **clPtr;
argc = 0;
cmd->argv[argc] = (char *) malloc(MAX_ARG_LEN);
while((cmd->argv[argc] = strsep(clPtr, WHITESPACE)) != NULL) {
cmd->argv[++argc] = (char *) malloc(MAX_ARG_LEN);
}
cmd->argc = argc - 1;
cmd->name = (char *) malloc(sizeof(cmd->argv[0]));
strcpy(cmd->name, cmd->argv[0]);
return 1;
}

int main() {
int status, pid;
char *parsePathEnv;
char *pathv[MAX_PATH_LEN];
char commandLine[LINE_LEN];
struct command_t command;
parsePathEnv = (char *) getenv("PATH");
printf("%s", parsePathEnv);
parsePath(pathv);
while(1) {
printPrompt();
readCommand(commandLine);
parseCommand(commandLine, &command);
command.argv[command.argc] = NULL;
command.name = lookupPath(command.argv, pathv);
if(command.name == NULL) {
printf("\nInvalid command");
continue;
}
if((pid = fork()) == 0) {
execv(command.name, command.argv);
}
wait(&status);
}
return 0;
}
Four answers:
cja
2013-01-25 09:02:11 UTC
You're about 90% of the way there; with a few corrections, I got your program running (I found strsep here: http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/libkern/strsep.c).



Here's some sample output:



osProj1# ls -l /usr/bin/gcc.exe

-rwx------+ 1 Administrators Domain Users 93717 Jun 7 2005 /usr/bin/gcc.exe

osProj1#



Here's what parseCommand should look like:



void parseCommand(char *cLine, struct command_t *cmd) {

  int argc = 0;

  char *clPtr = cLine;

 

  cmd->argv[argc] = (char *)malloc(MAX_ARG_LEN);

  while ((cmd->argv[argc] = strsep(&clPtr, WHITESPACE)) != NULL) {

    cmd->argv[++argc] = (char *)malloc(MAX_ARG_LEN);

  }

  cmd->argc = argc - 1;

  cmd->name = (char *)malloc(sizeof(cmd->argv[0]));

  strcpy(cmd->name, cmd->argv[0]);

}



You're passing the uninitialized clPtr to strsep, probably the cause of your segmentation fault. Notice that it needs to be a char*, and you pass the address of that to strsep. Also, no need to have a return type of int, I made it void.



Your parsePath function is not tokenizing correctly. Here's a fix:



void parsePath(char* dirs[]) {

  int i,j = 0;

  char *parsePathEnv, *thePath, *p;



  for (i=0; i
  parsePathEnv = (char *)getenv("PATH");

  thePath = (char*) malloc(strlen(parsePathEnv) + 1);

  strcpy(thePath, parsePathEnv);

  for (p = strtok(thePath, ":"); (p != NULL) && (j < MAX_ARGS); p = strtok(NULL,":")) {

    dirs[j++] = p;

  }

}



In lookupPath, I think you need to break out of the loop if (access(pName, X_OK) == 0), if that means you've found the executable you're looking for.



Start with those fixes, and you should be able to get this thing working.
Laurence I
2013-01-24 09:05:46 UTC
an EXE has a memory model small medium large

a pointer eg Char * could be small medium or large or other eg Char Far *

when doing Malloc your Char * needs to be the right sort

and your memory model needs to be enough to hold all your data in



you may be referencing Segments whilst only implementing a SMALL

memory model that doesnt cater for SEGMENTS in the same way.

you should be able to DEBUG it using the LST and MAP files to

find the culprit lines of code.



perhaps a Malloc(some whopping big number) is the cause?

i suggest you replace all Mallocs with something that tests what the malloc parameter is first as you may have dropped a clanger with the paramters value
?
2016-12-04 01:17:41 UTC
A "Segmentation Fault" constantly potential one component: you're getting access to a memory area outdoors the boundary of your technique area. often, meaning your array index factors outdoors the array. examine your 'r' and 'c' and 'i' and 'j' values to determine that they don't seem to be unfavorable or too huge to your array length. via the way, did you allocate any area for "vec"? i did not spot any vec = new vector (yadda yadda); everywhere.
Unca Alby
2013-01-24 21:10:13 UTC
You may need to get a debugger such as "gdb".



I'd like to try compiling it for you, but my system doesn't have a "strsep" command. I've never heard of it before.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...