#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11

    getopt ,command line options


    If i dont give any command line option it gives segmentation fault.. If i put one or more option it works..I have read about getopt in manual pages and some books but couldnt find my mistake at the below code
    i couldnt understand where am i making wrong..

    Code:
    ................
    #include "deploy.h"
    #include "main.h"
    
    
    int main(int argc, char **argv) {
    
            extern char *optarg;
            extern int optind;
    
            char *options = "s:t:d:e:vh";
            int i,o,err = 0;
    
            while ((o = getopt(argc,argv,options)) != -1) {
    
                    switch(o) {
                            case 's':
                                    if(NULL == (soudir = (char *)calloc(DSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(soudir, optarg, DSIZE);
                                    break;
                            case 't':
                                    if(NULL == (mytime = (char *)calloc(TSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(mytime, optarg, TSIZE);
                                    break;
                            case 'd':
                                    if(NULL == (destdir = (char *)calloc(DSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(destdir, optarg, DSIZE);
                                    break;
                            case 'e':
                                    if (NULL == (exclfile = (char *)calloc(EFSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(exclfile,optarg,EFSIZE);
                                    break;
                            case 'h':
                                    usage();
                                    exit(0);
                                    break;
                            case 'v':
                                    verbose = 1;
                                    break;
                            default:
                                    usage();
                                    err = 1;
                            }
            }
    
    
            for (i = optind; i < argc; i++) {
                    printf ("Non-option argument %s\n", argv[i]);
                    usage();
                    exit(0);
            }
    
            open_mydir(soudir,atoi(mytime),destdir);
    
            return 0;
    }
    
    
    void usage() {
    
            fprintf(stderr, "usage: jdeploy [-hv] [-s SourcePath] [-t date] [-d DestPath] [-e filename]\n\n");
            fprintf(stderr, "-h       : this (help) message\n");
            fprintf(stderr, "-v       : verbose output\n");
            fprintf(stderr, "-s       : Source Directory path to be copied\n");
            fprintf(stderr, "-t       : After which modification Time (YYYYMMDD)\n");
            fprintf(stderr, "-d       : Destination Directory path where the files will be copied (if not given, look /work/deploy)\n");
            fprintf(stderr, "-e       : File that contains Full path of SubDirectories to be Excluded\n\n");
            fprintf(stderr, "example: jdeploy -v -s /home/ppp0/ProjectMayhem -t 20030312 -d /work/prj2003312 -e file\n");
    }
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    well where is the error occurring? run it in gdb and find out what function it's faulting on. im not sure if this really matters, but when getopt() finds an argument it doesnt recognize, it returns '?'. maybe change default to that? im guessing the fault occurs when you try this:
    Code:
    open_mydir(soudir,atoi(mytime),destdir);
    when no options are passed i'm assuming u dont want to make that call.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222
    Good points, Infamous. err gets set in the default case, but is never tested after that. And if any of the three parameters -- soudir, mytime, or destdir -- don't get set to a valid value, then there's the segmentation fault.

    ppp0, you need to test that you have received values for those parameters before you consider calling that function. Or else have the function test them before trying to use them.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11
    Two mistakes i made is

    1. not to test err..

    2.soudir mytime and destdir must not be NULL value i have to test them before i send this variables to the function open_mydir..
    when i sent NULL values to open_mydir() , this function also doesnt test these variables.. So the segmentation fault occurs when i call open_mydir()..

    Code:
    ...................
    #include "deploy.h"
    #include "main.h"
    
    
    int main(int argc, char **argv) {
    
            extern char *optarg;
            extern int optind;
    
            char *options = "s:t:d:e:vh";
            int i,o,err = 0;
                      
                      //check err here
            while (err != 1 && (o = getopt(argc,argv,options)) != -1) {
    
                    switch(o) {
                            case 's':
                                    if(NULL == (soudir = (char *)calloc(DSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(soudir, optarg, DSIZE);
                                    break;
                            case 't':
                                    if(NULL == (mytime = (char *)calloc(TSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(mytime, optarg, TSIZE);
                                    break;
                            case 'd':
                                    if(NULL == (destdir = (char *)calloc(DSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(destdir, optarg, DSIZE);
                                    break;
                            case 'e':
                                    if (NULL == (exclfile = (char *)calloc(EFSIZE, sizeof(char))))
                                            fprintf(stderr,"Unable to allocate memory\n");
                                    strncpy(exclfile,optarg,EFSIZE);
                                    break;
                            case 'h':
                                    usage();
                                    exit(0);
                                    break;
                            case 'v':
                                    verbose = 1;
                                    break;
                            default:
                                    usage();
                                    err = 1;
                                    break;
                            }
            }
    
            if(err)             //test err also here 
                    exit(1);
    
            //and here i test if user enter the NULL values
            if(soudir == 0 || mytime == 0 || destdir == 0) {
    
                    usage();
                    exit(-1);
            }
    
            //true values sent to the below function cause i tested these values above
            open_mydir(soudir,atoi(mytime),destdir);
    
    
            return 0;
    }
    Thanks all for yor help...

    and one more question..

    I dont know how to use gdb. i am googling internet for gdb but Do you know any good resource on net about gdb..
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

IMN logo majestic logo threadwatch logo seochat tools logo