Article View: comp.unix.programmer
Article #3427Re: readir() II
From: frank
Date: Mon, 08 Feb 2010 01:57
Date: Mon, 08 Feb 2010 01:57
151 lines
5034 bytes
5034 bytes
Scott Lurndal wrote:
>> [lots of code, so big snips]
> Rather than a linked list, how about using the standard tsearch function?
Thanks, Scott, I have a mixed bag of success. Certainly there weren't
to be so many variables unused:
dan@dan-desktop:~/source42$ gcc -g -D_GNU_SOURCE -Wall -Wextra b1.c -o out
b1.c:35: warning: unused parameter �depth�
b1.c:66: warning: unused parameter �sp�
b1.c:67: warning: unused parameter �typeflag�
b1.c:67: warning: unused parameter �ftwbuf�
b1.c:92: warning: unused parameter �argc�
b1.c:92: warning: unused parameter �envp�
dan@dan-desktop:~/source42$ ./out
Segmentation fault
dan@dan-desktop:~/source42$ ./out /home/
^C
dan@dan-desktop:~/source42$ ./out /home/dan/source42/
/home/dan/source42
/home/dan/source42/b1.c
/home/dan/source42/b1.c~
/home/dan/source42/out
dan@dan-desktop:~/source42$ cat b1.c
#include <errno.h>
#include <ftw.h>
#include <memory.h>
#include <search.h>
#include <string.h>
#include <stdio.h>
void *root = NULL;
const char *myname;
/**
* tsearch(3) comparison function. The keys are nul-terminated
* arrays of type char.
*
* @param a The key being searched for
* @param b A key in the tree to compare against.
* @returns -1 if a collates before b
* 0 if a and b collate identically.
* +1 if a collates after b
*/
int
path_compare(const void *a, const void *b)
{
return strcmp((const char *)a, (const char *)b);
}
/**
* Print a tree node. The key for each node is a pathname.
*
* @param np A pointer to the node.
* @param which Point in tree traversal
* @param depth Depth of the tree.
*/
void
path_print(const void *np, const VISIT which, const int depth)
{
struct _node {
const char *key;
} *kp = (struct _node *)np;
switch (which) {
case preorder:
case endorder:
break;
case postorder:
case leaf:
fprintf(stdout, "%s\n", kp->key);
break;
}
}
/**
* nftw(3) callback function. Insert the provided path
* into the tree. Use the appropriate return value to
* avoid descending into subdirectories if desired.
*
* @param path The pathname for a directory entry.
* @param sp The corresponding struct stat
* @param typeflag Metadata
* @param ftwbuf Basename offset and path depth
* @returns FTW_STOP To stop walking the filesystem tree
* FTW_CONTINUE Continue walking the tree
* FTW_SKIP_SUBTREE Don't walk into this directory
*/
int
ftw_callback(const char *path, const struct stat *sp,
int typeflag, struct FTW* ftwbuf)
{
void *key = strdup(path);
if (key == NULL) {
fprintf(stderr, "%s: Unable to allocate memory for tree node: %m\n",
myname);
return FTW_STOP;
}
key = tsearch(key, &root, path_compare);
if (key == NULL) {
fprintf(stderr, "%s: Unable to allocate memory for tree node: %m\n",
myname);
return FTW_STOP;
}
return FTW_CONTINUE;
}
/**
* Walk a file system directory and insert the entries into a red-black
* tree.
*/
int
main(int argc, char **argv, char **envp)
{
int diag;
myname = argv[0];
diag = nftw(argv[1], ftw_callback, 20, 0);
if (diag == -1) {
fprintf(stderr, "%s: Unable to walk filesystem tree: %m\n",
argv[0]);
return 1;
}
twalk(root, path_print);
return 0;
}
/* vim: sw=4 sts=4 sta ts=8:
*/
// gcc -g -D_GNU_SOURCE -Wall -Wextra b1.c -o out
// -D_XOPEN_SOURCE=500
dan@dan-desktop:~/source42$
Where do I find out more about the -D* compiler switches? Apparently
I'm asking a question specifically about glibc.
I spent an hour reading about compiler switches in prel today, and I
think it perfectly reasonable for anyone in the contemporary open-source
movement to be almost constantly confused by them.
Cheers,
--
frank
Technical Details
Message-ID:
<7t99bfF750U1@mid.individual.net>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mailReferences:
<7sa7rrFg81U1@mid.individual.net> <hjpmhk$j4a$1@reader1.panix.com> <7suj6qF4qbU1@mid.individual.net> <hke881$7lt$00$1@news.t-online.com> <7t1qmfFcm2U1@mid.individual.net> <hO0bn.611$JQ6.244@news.usenetserver.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 1200bcf240,1d887f475f1c894c X-Google-Attributes: gid1200bcf240,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,CP1252 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: frank <frank@example.invalid> Newsgroups: comp.unix.programmer,gnu.gcc.help Subject: Re: readir() II Date: Sun, 07 Feb 2010 18:57:02 -0700 Lines: 151 Message-ID: <7t99bfF750U1@mid.individual.net> References: <7sa7rrFg81U1@mid.individual.net> <hjpmhk$j4a$1@reader1.panix.com> <7suj6qF4qbU1@mid.individual.net> <hke881$7lt$00$1@news.t-online.com> <7t1qmfFcm2U1@mid.individual.net> <hO0bn.611$JQ6.244@news.usenetserver.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net glWrXW++49pr/GwV15P4gQ/QSvMkYXTZH7ycrBwBKEvPBouhUd Cancel-Lock: sha1:32UmFjlInUNkeGnx50l+MeOkll4= User-Agent: Thunderbird 2.0.0.23 (X11/20090817) In-Reply-To: <hO0bn.611$JQ6.244@news.usenetserver.com>