Thread: Seg-Fault. I found the location... help.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Seg-Fault. I found the location... help.

    Ok I found where the seg-fault is happening, but I dont know why it happens.

    Code:
    char * env;
    env = "/usr/local/bin:/bin:/usr/bin";
    ...
    
    int tryEnv(char * theFileName)
    {
        char * myEnv;
        myEnv = strtok(env, ":");
        char * myFile;
    
        while (1)
        {
            /* check if there is nothing else to extract */
             if(myEnv == NULL)
             {
                 printf("DONE\n");
                 break;
             }
            /* extract string from string sequence */
    
            strcpy(myFile, myEnv);
            strcat(myFile, "/");
            strcat(myFile, theFileName);
            int result;
            result = access (myFile, X_OK);//  TODO This causes Seg-Fault 
                 
            /* print string after tokenized, check if exists */
            //if(access(myFile,X_OK)) //  This i will use once the above works
            //{
                printf("%s\n", myFile);
            //}
    
            /* Finally get the next Env (if the othe failed)  */
            myEnv = strtok(NULL, ":");
    
        }
        return 0;
    }
    main(int argc, char **argv)
    {
        char *fileName;
        fileName = fgets (line, BUFFER_LENGTH, stdin); // i type the file name "ls"
        ret   =  tryEnv(fileName);
        ..
    }
    The above cause a segfault (I show at the "TODO" comment). But i think i am calling the system call "access" correctly, because it works in this program:

    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int result;
        char *filename1 = "/bin/ls";
        char *filename2 = "/usr/kerberos/bin/ls";
        char *filename3 = "/usr/local/bin/ls";
        result = access (filename1, X_OK); // works, returns 0
        printf("%d\n",result);
        result = access (filename2, X_OK); // works, returns -1
        printf("%d\n",result);
        result = access (filename3, X_OK); // works, returns -1
        printf("%d\n",result);
    
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You never allocate the memory for your char * in your function.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by Bladactania View Post
    You never allocate the memory for your char * in your function.
    Oh, ok, ( C programming is not my strong point).

    Question though, Is that what is causing the seg-fault?

    Because if i take out
    Code:
               result = access (myFile, X_OK);//  TODO This causes Seg-Fault
    then it works, with no segfault.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I'm not sure what access() does.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by Bladactania View Post
    I'm not sure what access() does.
    access is a system call that checks on files. it returns 0 if it worked, and -1 if it failed.


    Code:
    access(char* fileName, int mode); // looks for the file and runs a test.
    the mode "X_OK" checks if the file exists and is Executable.

    so when i have:

    Code:
      result = access (myFile, X_OK);// should return 0 if the file exists and is executable.

    I am thinking because i am using strtok, something is happening to "char* myFile" so that it cannot work propperly.

    Because i can get this to work:

    Code:
        char *filename1 = "/bin/ls";
        result = access (filename1, X_OK);
    Maybe just allocating memory would fix this, but it dosent make sense to me.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Seg Faults usually (maybe always?) occur when you are attempting to write to memory that you shouldn't be writing to which occures when you don't allocate memory properly (or at all).

    The error has nothing to do with myEnv, it has to do with myFile. I'm not sure you get the fault at the place you do but when you use strcpy and strcat, the first argument must have the right amount of memory allocated it PRIOR to the use of the function.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    yup, that was the problem.. (I told you i was bad at C... lol)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation / Seg Fault
    By Korhedron in forum C++ Programming
    Replies: 31
    Last Post: 09-06-2008, 11:47 AM
  2. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  3. unknown seg fault reason.
    By egoveneror2 in forum C Programming
    Replies: 16
    Last Post: 04-15-2008, 02:30 AM
  4. getc Seg Fault
    By Tyris in forum C Programming
    Replies: 3
    Last Post: 05-25-2005, 12:00 PM
  5. Simple seg Fault Problem
    By ccoder01 in forum C Programming
    Replies: 5
    Last Post: 05-02-2004, 10:28 PM