Thread: Passing pathname argument in glob() function

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    12

    Passing pathname argument in glob() function

    Is it possible to pass a pathname to glob()?

    I have sample code that I've tried to modify to accept a pathname and use this to search the path given. The code below only searches the present working directory.

    Can anyone suggest a good glob() tutorial or article to reference?

    Code:
    int	main(int argc, char *argv[])
    {
    	glob_t	paths;
    	int		csource;
    	char	**p;
    	
    	printf("argv[0] = %s\n", argv[0]);
    	printf("argv[1] = %s\n", argv[1]);  /* Argument I want to use as pathname */
    	
    	/* Find all ".c" files in given directory*/
    	csource = glob("*.c", 0, NULL, &paths);
    	
    	if (csource == 0)
    	{
    		for (p=paths.gl_pathv; *p != NULL; ++p)
    			puts(*p);
    		globfree(&paths);
    	}

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    What you'd want to do is glue the pathname together with the pattern using something like snprintf(). glob("/some/path/*.c", ..) would be how the actual call would look.

    As for tutorials, sorry, I don't know of any, apart from whatever examples your man pages might contain.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    12
    Thanks cas.

    I did try using snprintf, but I'm not able to get the glob command to work with my string created with snprintf. My output is:

    # ./globprog /home/myuser
    1: This is argv[0] = ./globprog
    2: This is argv[1] = /home/myuser
    3: PATH = /home/myuser
    This is the fullpath: "/home/myuser*.c"
    Glob error. globresult = 16384

    My code is below:

    Code:
    int	main(int argc, char *argv[])
    {
    	glob_t	paths;
    	int	globresult;
    	char	fullpath[MAX_LEN+1],
    		**p;
    			
    	printf("1: This is argv[0] = %s\n", argv[0]);
    	printf("2: This is argv[1] = %s\n", argv[1]);
    	printf("3: PATH = %s\n", argv[1]);
    	
    	snprintf(fullpath, MAX_LEN, "\"%s/*.c\"", argv[1]);
    	printf("This is the fullpath: %s\n", fullpath); 
    	
    	/* Find all ".c" files in specified directory and store in paths structure*/
    	globresult = glob(fullpath, 0, NULL, &paths);

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Don't put quotes around the path. glob() will look for a path that starts with a quote if you do that, and of course no such path will exist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM