LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2010, 09:23 AM   #1
schurt
Member
 
Registered: Nov 2003
Location: raleigh, nc
Distribution: fedora, arch
Posts: 78

Rep: Reputation: 15
C: problem with strdup against array


Does anyone see a problem with my code? attempting to do a strdup is failing with a segmentation fault within libc

plugintools.h
Code:
/* Provide function for adding files to be copied/linked
   into report
*/
typedef struct file_list {
  int count;
  char **filepath;
} file_list;

// max path length for any file/dir listing
#define MAXPATHLEN 1024
file_list *
add_copy_spec(const char *path);
plugintools.c
Code:
/* add file specification (dir, file, shell glob)
 */
file_list *
add_copy_spec(const char *path)
{
    int err, i;
    char *abspath[MAXPATHLEN];
    glob_t globbuf;

    struct file_list *fl = (file_list *)malloc(sizeof(file_list));
    
    err = glob(path, GLOB_DOOFFS | GLOB_APPEND, NULL, &globbuf);
    if (err) {
        fl->count = 0;
        fl->filepath = NULL;
        return fl;
    }
    
    fl->count = globbuf.gl_pathc;
    fl->filepath = (char **)malloc(sizeof(char *) * fl->count);
    // loop through our file list, append to struct array
    printf("\nfilepath loop\n");
    for (i = 0; i < globbuf.gl_pathc; i++) {
        printf("%s\n", globbuf.gl_pathv[i]);
        fl->filepath[i] = strdup(globbuf.gl_pathv[i]);

/*
        if((realpath(globbuf.gl_pathv[i], abspath)) != NULL) {
            //fl->filepath[i] = strdup(globbuf.gl_pathv[i]);
          } else {
            continue;
        }
*/
    }
    globfree(&globbuf);
    return fl;
}
 
Old 03-12-2010, 06:37 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Your glob flags GLOB_DOOFFS and GLOB_APPEND seem to be used incorrectly, DOOFFS shouldn't be used with an uninitalized glob_t since it uses the glob_t gl_offs information. GLOB_APPEND shouldn't be used in the first call to glob and since your glob_t var goes away every time you call add_copy_spec you probably shouldn't have that flag in there at all.

Either one of those used that way could make your return state undefined for your globbuf info
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
C Array Problem bnixon10 Programming 4 03-19-2008 09:28 PM
strdup help studentlb Programming 2 11-23-2006 08:34 AM
help with strdup knobby67 Programming 2 10-22-2006 12:23 PM
array problem pranavojha Programming 4 03-21-2006 01:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration