Thread: How to properly use a C function?

  1. #1
    Registered User
    Join Date
    Sep 2007

    How to properly use a C function?

    Below I posted a small piece of my code. I'm not familiar with using this C function that's highlighted in red and my program generates the following when executed:

    "10 [main] a.out 4488 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)"

    As of right now i'm not passing the command to the function, only the arguments after it. Any help would be great. Thanks in advance.

    char *string = "ls *.c";
    char *token;
    int count = 0;
    boolean FIRSTGLOB = TRUE;  //ignore boolean, already been type def'ed
    int status = 0;
    glob_t globbuf;
    token = strtok( input, " " );
    command = token; 
    while( token != NULL ) //process arguments after command ( i.e. "*.c" )
    		token = strtok( NULL, " " );
    		arguments[ count ] = token;
    //in this case count will = 1, and the only argument was "*.c", so arguments[] only contains
    // "*.c"  (should it also contain "ls"?)
    for( i=0; i<count; i++ ) //call the glob function for the number of wildcard arguments found
    	if( !FIRSTGLOB ) //more than one wildcard argument was found
    		glob( arguments[i], GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf );
    	else //only a single wildcard argument
    	       glob( arguments[i], GLOB_DOOFFS, NULL, &globbuf );
    	       FIRSTGLOB = FALSE; //no longer the first "glob", could be more wildcards
       status = execvp( cmd, &globbuf.gl_pathv[0] );

  2. #2
    Join Date
    Dec 2007
    This works:
    #include <stdio.h>
    #include <glob.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_ARGS 20
    int main()
        char *cmd = strdup("ls *.php *.sh *.yo");
        char *arguments[MAX_ARGS] = { 0 };
        char *token = NULL; 
        char *command = strtok(cmd, " ");
        int count = 0;
        while (NULL != (token = strtok(NULL, " ")) && count < MAX_ARGS)
            arguments[count++] = token;
        glob_t globbuf = { 0 };
        int i = 0;
        int rc = 0;
        for (; i < count ; ++i)
            if (0 != (rc = glob(arguments[i], GLOB_DOOFFS | GLOB_APPEND, 
                                NULL, &globbuf)))
                if (GLOB_NOMATCH == rc)
                fprintf(stderr, "glob failed: &#37;d\n", rc);
                return -1;
        for (i = 0; i < globbuf.gl_pathc; ++i)
            printf("Argument %d: %s\n", i, globbuf.gl_pathv[i]);
        return 0;
    Quote Originally Posted by Output
    neon:~ me$ ./globtest
    Argument 0: qs.php
    Argument 1: sbtest.php
    Argument 2: test.php
    Argument 3:
    Argument 4:
    Argument 5:
    neon:~ me$
    Last edited by rags_to_riches; 05-29-2008 at 04:06 PM. Reason: Missing newline

  3. #3
    Registered User
    Join Date
    Sep 2007
    I'm not sure how to apply that to what i'm trying to do. I'm trying to allow make the command "ls *.c" display all the C files in the current directory. Looking at your code doesn't really help me understand what I did wrong or how to fix it. But don't worry about it, I figured out what I was doing wrong. I now have a general solution to allow all and any types of globbing via input. Thanks anyways.
    Last edited by John_L; 05-29-2008 at 04:28 PM.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    First and foremost, string literals should be const char, not char.
    const char *string = "ls *.c";
    Second, I don't see where you define arguments. Most likely, your "arguments" variable isn't big enough to hold the data, but of course I can't be sure.

    You are overwriting data you shouldn't somewhere. That's why you get an error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    One difference I see is that your globbuf isn't initialized. I don't KNOW if that's of any importance tho'.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

Tags for this Thread