[C] Segmentation fault

Discussion in 'OT Technology' started by Rick, Feb 1, 2006.

  1. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    Can't seem to figure out why I get a segmentation fault in the following C code. It will print the first printf statement, but then give me a segmentation fault whether argv[1] is NULL or not.

    Code:
    else if (strcasecmp(argv[0], "chdir") == 0)  /* chdir is translated to the cd command */
            {
              printf("%s %s\n", argv[0], argv[1]);
              if (strcmp(argv[1], NULL) == 0)
                {
                  fprintf(stderr, "Error: Enter directory to change to.\n");
                }
              else
                {
                  printf("here?\n");
    
                  dir = strtok(argv[j], " ");
    
                  printf("%s\n", dir);
    
                  while (argv[j] != NULL)
           {
                      j++;
                      tmp = strtok(NULL, " \"");
                      strcat(dir, " ");
                      strcat(dir, tmp);
    
                      printf("%s\n", dir);
                    }
    
                  printf("Changing directory to %s\n", dir);
    
                  chdir(dir);
                }
            }
    
     
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    attach a debugger and tell us what line it fails at
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    if (strcmp(argv[1], NULL) == 0)
     
  4. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    post all of it
     

Share This Page