a find for plan9, a tad faster than du -a... well, somewhat faster actually. much faster than linux' find :) Notes: Sat Oct 16 00:14:44 EDT 2004 rsc except it's not find. it's more like ls -R. see lsr2.c for a similar attempt, home grown. why not just make du faster? i can't see why the system calls required would be different. then everyone wins. russ Reference: /n/sources/patch/sorry/find Date: Sat Oct 16 06:16:31 CES 2004 Reviewed-by: rsc --- /sys/src/cmd/find.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/find.c Sat Oct 16 06:16:31 2004 @@ -0,0 +1,133 @@ +#include +#include +#include + +void find(char*, Dir*); +void err(char*); +void warn(char*); +int seen(Dir*); + +char *fmt = "%q\n"; +int fflag; + +void +main(int argc, char *argv[]) +{ + int i; + + doquote = needsrcquote; + quotefmtinstall(); + + ARGBEGIN { + case 'f': /* ignore errors */ + fflag = 1; + break; + } ARGEND + if(argc==0) + find(".", dirstat(".")); + else + for(i=0; i 0) { + d = buf; + for(i=0; iqid.type&QTDIR) == 0) { + file = s_copy(name); + if(file == nil) + err("s_copy"); + s_append(file, "/"); + s_append(file, d->name); + print(fmt, s_to_c(file)); + s_free(file); + continue; + } + if(strcmp(d->name, ".") == 0 || + strcmp(d->name, "..") == 0 || + seen(d)) + continue; + file = s_copy(name); + s_append(file, "/"); + s_append(file, d->name); + find(s_to_c(file), d); + if(file == nil) + err("s_copy"); + print(fmt, s_to_c(file)); + s_free(file); + } + free(buf); + } + if(n < 0) + warn(name); + close(fd); +} + +#define NCACHE 128 /* must be power of two */ +typedef struct Cache Cache; +struct Cache +{ + Dir* cache; + int n; + int max; +} cache[NCACHE]; + +int +seen(Dir *dir) +{ + Dir *dp; + int i; + Cache *c; + + c = &cache[dir->qid.path&(NCACHE-1)]; + dp = c->cache; + for(i=0; in; i++, dp++) + if(dir->qid.path == dp->qid.path && + dir->type == dp->type && + dir->dev == dp->dev) + return 1; + if(c->n == c->max){ + c->cache = realloc(c->cache, (c->max+=20)*sizeof(Dir)); + if(cache == 0) + err("malloc failure"); + } + c->cache[c->n++] = *dir; + return 0; +} + +void +err(char *s) +{ + fprint(2, "find: %s: %r\n", s); + exits(s); +} + +void +warn(char *s) +{ + if(fflag == 0) + fprint(2, "find: %s: %r\n", s); + return; +} --- /sys/man/1/find Thu Jan 1 00:00:00 1970 +++ /sys/man/1/find Sat Oct 16 06:16:31 2004 @@ -0,0 +1,27 @@ +.TH FIND 1 +.SH NAME +find \- find files and directories on the system. +.SH SYNOPSIS +.B find +[ +.B -f +] +[ +.B dir1 dir2 ... +] +.SH DESCRIPTION +.I Find +lists recursively all files under the directories supplied, or the current directory. +.PP +.B -f +causes +.I find +to skip reporting any warnings and errors. +.SH SOURCE +.B /sys/src/cmd/find.c +.SH BUGS +No options familiar from lunix' find, such as +.B -type +and +.B -name, +available.