diff options
author | Mystery Man <unknown@mandriva.org> | 2005-10-20 14:05:54 +0000 |
---|---|---|
committer | Mystery Man <unknown@mandriva.org> | 2005-10-20 14:05:54 +0000 |
commit | 9b6a0d83d135fad376b4eadcb2bd7bd8c8a07aa5 (patch) | |
tree | f3ef7ada2ce82dafb6d41cc087b0cc7fdfc40a46 /tools/ddcprobe/minifind.c | |
parent | 3e4ad5e1b687f262a1d7cc39a0b905a595ac7f95 (diff) | |
download | drakx-backup-do-not-use-10_34_11_100mdk.tar drakx-backup-do-not-use-10_34_11_100mdk.tar.gz drakx-backup-do-not-use-10_34_11_100mdk.tar.bz2 drakx-backup-do-not-use-10_34_11_100mdk.tar.xz drakx-backup-do-not-use-10_34_11_100mdk.zip |
This commit was manufactured by cvs2svn to create tagV10_34_11_100mdk
'V10_34_11_100mdk'.
Diffstat (limited to 'tools/ddcprobe/minifind.c')
-rw-r--r-- | tools/ddcprobe/minifind.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tools/ddcprobe/minifind.c b/tools/ddcprobe/minifind.c new file mode 100644 index 000000000..f483055b4 --- /dev/null +++ b/tools/ddcprobe/minifind.c @@ -0,0 +1,77 @@ +/* minifind.c -- simple find library + * + * Copyright (c) 2002 Terra Soft Solutions, Inc. + * Written by Dan Burcaw <dburcaw@terrasoftsolutions.com> + * + * This software may be freely redistributed under the terms of the GNU + * library public license. + * + * You should have received a copy of the GNU Library Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ + +#include "minifind.h" + +// insert a node at head of linked-list +void insert_node(struct pathNode *n, char *path) +{ + struct pathNode *new = (struct pathNode *) malloc(sizeof(struct pathNode)); + new->path = path; + new->next = n->next; + n->next = new; +} + +// return input strip less last character +char *stripLastChar(char *in) +{ + char *out = malloc(sizeof(char)*strlen(in)); + snprintf(out, strlen(in) - 1, "%s", in); + return out; +} + +// do the work +char *minifind(char *dir, char *search, struct findNode *list) +{ + char *d = NULL; + int n; + struct dirent **namelist; + struct stat buf; + + if (dir[strlen(dir)-1] == '/') + dir = stripLastChar(dir); + + // check is there is an exact filematch to dir + // when search is not specified + if (search == NULL) + { + if (lstat(dir, &buf) == 0) + insert_node(list->result, dir); + return 0; + } + + n = scandir(dir, &namelist, 0, alphasort); + if (n >= 0) + { + while (n--) + { + d = malloc(sizeof(char) * (strlen(dir) \ + + strlen(namelist[n]->d_name)+1)); + sprintf(d, "%s/%s", dir, namelist[n]->d_name); + if (strstr(namelist[n]->d_name, search)) + insert_node(list->result, d); + + if ((lstat(d, &buf) == 0) && S_ISDIR(buf.st_mode)) + { + if (strcmp(namelist[n]->d_name, ".") && + strcmp(namelist[n]->d_name, "..")) + d = minifind(d, search, list); + } + free(namelist[n]); + } + free(namelist); + return d; + } + return 0; +} |