9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Dan Cross <cross@math.psu.edu>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] 'find'
Date: Mon, 30 Jun 2003 10:32:14 -0400	[thread overview]
Message-ID: <200306301432.h5UEWE707794@augusta.math.psu.edu> (raw)
In-Reply-To: Your message of "Sun, 29 Jun 2003 09:57:58 PDT." <3EFF1A96.8010201@acm.org>

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

> grep pattern `{du -a /}

Hmm, this is exactly what I wrote walk and sor for; check the
archives if you're interested.  Actually, I've updated them for
4th Edition, so I'll include them here (I have been meaning to
add something that ignores duplicates in walk, though).  The
archives still have a nice cookbook for how to use them.

	- Dan C.


[-- Attachment #2: Type: text/plain, Size: 193 bytes --]

#!/bin/rc
rfork e
fn runtests {
	file=$1; shift
	while (! ~ $#* 0 && ! eval $1 ''''^$file^'''')
		shift
	if (! ~ $#* 0)
		echo $file
}
while (file = `{read}) {
	runtests $file $*
}

[-- Attachment #3: Type: text/plain, Size: 2336 bytes --]

/*
 *  Walk a directory tree, in the style of du(1),
 *  but with some additional flourishes.
 *
 *  Dan Cross <cross@math.psu.edu>
 */

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

static int	mkdepth(int);
static char	*mkname(char *, int *, char *, char *);
static void	walk(char *, int, int);
static void	walkname(char *, int, int);

char	*fmt;

void
main(int argc, char *argv[])
{
	char	*dir;
	int	depth;
	Dir	*d;

	dir = ".";
	fmt = "%s\n";
	depth = -1;
	ARGBEGIN {
	case 'd':
		depth = atoi(ARGF());
		break;
	case 'q':
		quotefmtinstall();
		doquote = needsrcquote;
		fmt = "%q\n";
		break;
	}ARGEND
	if (argc == 0)
		walkname(".", depth, 1);
	else {
		for (dir = *argv; dir; dir = *++argv) {
			if ((d = dirstat(dir)) == nil) {
				fprint(2, "dirstat %s: %r\n", dir);
				continue;
			}
			walkname(dir, depth, d->mode & DMDIR);
			free(d);
		}
	}

	exits(0);
}

static void
walkname(char *dirname, int depth, int isdir)
{
	int	fd;

	if (strcmp(dirname, ".") != 0 && strcmp(dirname, "..") != 0)
		print(fmt, dirname);
	if (isdir) {
		fd = open(dirname, OREAD);
		if (fd < 0) {
			fprint(2, "open %s: %r\n", dirname);
			return;
		}
		walk(dirname, fd, depth);
		close(fd);
	}
}

static char *
mkname(char *name, int *l, char *basename, char *filename)
{
	char	*nname;
	int	t;

	t = strlen(basename) + 1 + strlen(filename) + 1;
	if (*l == 0 || name == nil) {
		*l = t;
		name = malloc(t);
		if (name == nil)
			sysfatal("malloc %d: %r\n", l);
	} else if (*l < t) {
		nname = realloc(name, t);
		if (nname == nil) {
			free(name);
			sysfatal("malloc %d: %r\n", l);
		}
		*l = t;
		name = nname;
	}
	snprint(name, t, "%s/%s", basename, filename);
	cleanname(name);

	return(name);
}

static int
mkdepth(int depth)
{

	return((depth == -1) ? depth : depth - 1);
}

static void
walk(char *dirname, int fd, int depth)
{
	Dir	*dir, *dp;
	char	*name, *nname;
	int	i, l, n, t;

	if (depth == 0)
		return;
	l = 0;
	name = nil;
	n = dirreadall(fd, &dir);
	for (dp = dir, i = 0; i < n; dp++, i++) {
		if (strcmp(dp->name, ".") == 0 || strcmp(dp->name, "..") == 0)
			continue;
		name = mkname(name, &l, dirname, dp->name);
		walkname(name, mkdepth(depth), dp->mode & DMDIR);
	}
	free(dir);
	if (name != nil)
		free(name);
}

      parent reply	other threads:[~2003-06-30 14:32 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-29 16:57 D. Brownlee
2003-06-29 16:59 ` Sape Mullender
2003-06-30 14:07   ` rog
2003-06-30 14:18     ` Scott Schwartz
2003-06-30 14:50       ` rog
2003-07-03  9:42         ` Douglas A. Gwyn
2003-07-03 10:03           ` Fco.J.Ballesteros, nemo
2003-07-03 15:14             ` rob pike, esq.
2003-07-03 12:57           ` matt
2003-07-03 16:59             ` Douglas A. Gwyn
2003-07-03 17:09               ` matt
2003-06-30 15:30       ` Dan Cross
2003-07-03 12:11     ` Kenji Arisawa
2003-06-29 18:11 ` Jack Johnson
2003-06-29 18:42   ` Dan Cross
2003-07-04 14:08   ` Ralph Corderoy
2003-07-05 18:38     ` Dan Cross
2003-07-06  3:11       ` boyd, rounin
2003-07-07  8:33         ` Douglas A. Gwyn
2003-07-07 10:49           ` boyd, rounin
2003-07-07 14:05             ` Douglas A. Gwyn
2003-07-07 15:46           ` rob pike, esq.
2003-07-07 19:18             ` Dan Cross
2003-07-07 19:26               ` David Presotto
2003-07-07 19:46                 ` Dan Cross
2003-07-07 19:08           ` Dan Cross
2003-07-07 19:21             ` Dan Cross
2003-07-08  8:31               ` Douglas A. Gwyn
2003-07-08 11:20                 ` Dan Cross
2003-07-06 16:12       ` Jack Johnson
2003-07-06 22:20       ` matt
2003-07-07 13:28       ` Ralph Corderoy
2003-07-07 19:13         ` Dan Cross
2003-06-30  7:47 ` Fco.J.Ballesteros, nemo
2003-06-30  7:55   ` boyd, rounin
2003-06-30 14:32 ` Dan Cross [this message]

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=200306301432.h5UEWE707794@augusta.math.psu.edu \
    --to=cross@math.psu.edu \
    --cc=9fans@cse.psu.edu \
    /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).