9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: Re: [9fans] du and find
Date: Mon, 28 Dec 2009 18:25:29 -0500	[thread overview]
Message-ID: <ad3e3bdd465ea8f35c7aa5f1d7b9c149@ladd.quanstro.net> (raw)
In-Reply-To: <<20091228230510.GA25423@machine>>

[-- Attachment #1: Type: text/plain, Size: 260 bytes --]

i agree that du -a has a few holes.  too bad whitespace
is allowed in file names.  i use the attached find.c.
it's also available as contrib quanstro/find.  by default
the output is quoted so that it can be reparsed properly
with rc or gettokens.

- erik

[-- Attachment #2: find.c --]
[-- Type: text/plain, Size: 2491 bytes --]

#include <u.h>
#include <libc.h>
#include <bio.h>

char 	*defargv[] = {".", 0};
char	*fmt = "%q\n";
int	flag[256];
uint	dev ;
uint	type;
Biobuf	out;

void
warn(char *s)
{
	if(flag['f'] == 0)
		fprint(2, "find: %s: %r\n", s);
}

void
usage(void)
{
	fprint(2, "usage: find [-1dfq] [path ...]\n");
	exits("usage");
}

/*  if you think this scales you'd be wrong.  this is is 1/128th of a linear search.  */

enum{
	Ncache		= 128,		/* must be power of two */
	Cachebits	= Ncache-1,
};

typedef struct{
	vlong	qpath;
	uint	dev;
	uchar	type;
} Fsig;

typedef	struct	Cache	Cache;
struct Cache{
	Fsig	*cache;
	int	n;
	int	nalloc;
} cache[Ncache];

void
clearcache(void)
{
	int i;

	for(i = 0; i < nelem(cache); i++)
		free(cache[i].cache);
	memset(cache, 0, nelem(cache)*sizeof cache[0]);
}

int
seen(Dir *dir)
{
	Fsig 	*f;
	Cache 	*c;
	int	i;

	c = &cache[dir->qid.path&Cachebits];
	f = c->cache;
	for(i = 0; i < c->n; i++)
		if(dir->qid.path == f[i].qpath
			&& dir->type == f[i].type
			&& dir->dev == f[i].dev)
			return 1;
	if(i == c->nalloc){
		c->nalloc += 20;
		f = c->cache = realloc(c->cache, c->nalloc*sizeof *f);
	}
	f[c->n].qpath = dir->qid.path;
	f[c->n].type = dir->type;
	f[c->n].dev = dir->dev;
	c->n++;
	return 0;
}

int
dskip(Dir *d)
{
	if(flag['1']){
		if(dev == 0 && type == 0){
			dev = d->dev;
			type = d->type;
		}
		if(d->dev != dev || d->type != type)
			return 0;
	}
	return 1;
}

int
skip(Dir *d)
{
	if(strcmp(d->name, ".") == 0|| strcmp(d->name, "..") == 0 || seen(d))
		return 1;
	return 0;
}

void
find(char *name)
{
	int fd, n;
	Dir *buf, *p, *e;
	char file[256];

	if((fd = open(name, OREAD)) < 0) {
		warn(name);
		return;
	}
	Bprint(&out, fmt, name);
	for(; (n = dirread(fd, &buf)) > 0; free(buf))
		for(p = buf, e = p+n; p < e; p++){
			snprint(file, sizeof file, "%s/%s", name, p->name);
			if((p->qid.type&QTDIR) == 0 || !dskip(p)){
				if(!flag['d'])
					Bprint(&out, fmt, file);
			}else if(!skip(p))
				find(file);
		}
	close(fd);
}

void
main(int argc, char *argv[])
{
	doquote = needsrcquote;
	quotefmtinstall();

	ARGBEGIN{
	case 'd':
	case 'f':
	case '1':
		flag[ARGC()] = 1;
		break;
	case 'q':
		fmt = "%s\n";
		break;
	default:
		usage();
	}ARGEND

	Binit(&out, 1, OWRITE);
	if(argc == 0)
		argv = defargv;
	for(; *argv; argv++){
		find(*argv);
		clearcache();
	}
	Bterm(&out);
	exits(0);
}


       reply	other threads:[~2009-12-28 23:25 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <<20091228230510.GA25423@machine>
2009-12-28 23:25 ` erik quanstrom [this message]
2009-12-28 23:31   ` Don Bailey
2009-12-28 23:50     ` Lyndon Nerenberg (VE6BBM/VE7TFX)
     [not found] <<df49a7371001021043p2a990207od65457a068b7828@mail.gmail.com>
2010-01-02 19:47 ` erik quanstrom
2010-01-02 23:21   ` Bakul Shah
2010-01-03  1:49     ` erik quanstrom
2010-01-03  2:31       ` Bakul Shah
2010-01-03  2:40         ` erik quanstrom
2010-01-06 20:44           ` Akshat Kumar
     [not found] <<20100102052943.GA9871@machine>
2010-01-02 17:05 ` erik quanstrom
2010-01-02 18:18   ` anonymous
     [not found] <<df49a7371001011744o6687fd59l451d690ea56edea5@mail.gmail.com>
2010-01-02  2:02 ` erik quanstrom
2010-01-02  5:29   ` anonymous
2010-01-02 18:43   ` roger peppe
2010-01-03  2:28     ` Anthony Sorace
     [not found] <<df49a7371001011234r3aaaf961n8ce253a6681e74b1@mail.gmail.com>
2010-01-02  0:51 ` erik quanstrom
2010-01-02  1:44   ` roger peppe
     [not found] <<af7cc2a5be1668a4f2cb708f5bd96f67@yyc.orthanc.ca>
2009-12-29  1:22 ` erik quanstrom
2009-12-29  0:41 erik quanstrom
2009-12-29  1:03 ` Lyndon Nerenberg (VE6BBM/VE7TFX)
2010-01-01 20:34 ` roger peppe
     [not found] <<68eb39920912281531jd0e4661j56adfc589a370dfc@mail.gmail.com>
2009-12-28 23:35 ` erik quanstrom
2009-12-28 23:39   ` Don Bailey
2009-12-29  1:00     ` anonymous
2009-12-29  1:13       ` Don Bailey
  -- strict thread matches above, loose matches on Subject: below --
2009-12-28 23:05 anonymous
2009-12-28 23:09 ` lucio
2009-12-28 23:14 ` Steve Simon
2009-12-29 17:59 ` Tim Newsham
2009-12-29 18:28   ` Don Bailey
2009-12-29 20:16   ` Rob Pike
2009-12-30  7:44     ` anonymous
2010-05-03 12:13   ` Mathieu Lonjaret
2010-05-03 12:18     ` Akshat Kumar
2010-05-03 12:26       ` Mathieu Lonjaret
2010-05-03 12:49         ` tlaronde
2010-05-03 13:10         ` Ethan Grammatikidis
2010-05-03 13:41           ` Steve Simon
2010-05-03 15:18             ` Ethan Grammatikidis
2010-05-03 15:29               ` jake
2010-05-03 15:46                 ` Ethan Grammatikidis
2010-05-03 15:37               ` Steve Simon
2010-05-03 13:17       ` Rudolf Sykora
2010-05-03 14:53         ` erik quanstrom
2010-05-03 18:34           ` Jorden M
2010-05-04 10:01             ` Ethan Grammatikidis
2010-05-04 10:29               ` Robert Raschke
2010-05-04 15:38               ` Jorden M
2010-05-04 16:56                 ` Gabriel Díaz
2010-05-04 18:39                   ` Karljurgen Feuerherm
2010-05-03 14:03     ` erik quanstrom

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ad3e3bdd465ea8f35c7aa5f1d7b9c149@ladd.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).