9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: rog@vitanuova.com
To: 9fans@cse.psu.edu
Subject: [9fans] ls -R
Date: Wed,  8 Aug 2001 11:45:13 +0100	[thread overview]
Message-ID: <20010808103450.57264199E3@mail.cse.psu.edu> (raw)

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

sorry, boyd's response wasn't enormously helpful.

as far as i know the only plan 9 utility around that traverses the
file tree is du(1).

personally, i have a little rc script in my bin called find
(attached), which does almost all of what i used to use find for under
unix, i.e.  find files with a particular name.

e.g.
	find . '\.c$'
to find all c source files.

i've also got a little version of xargs (also attached) that reads
filenames from stdin and occasionally executes a command with those as
arguments, which is useful when grepping in hierarchies where the list
of files will be larger than execv will tolerate (plus you start
getting results sooner).  luckily newlines are illegal in plan 9 names
AFAIK so it's not vulnerable to the same problems as the unix xargs.

so ls -lR becomes:

	find . . | xargs ls -l

  cheers,
    rog.


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

#!/bin/rc
if (~ $#* 1 0) {
	echo 'usage: find <dir>... <re>' >[1=2]
	exit usage
}
a=()
while (! ~ $#* 1) {
	a=($a $1)
	shift
}
du -a $a | sed 's/^[^ 	]*[ 	]//' | grep $1

[-- Attachment #3: xargs.c --]
[-- Type: text/plain, Size: 1647 bytes --]

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

char *makeline(void);

#define NUMARGS 470
#define MAXSIZE 5000

void run(char **cmd);
Biobuf stdin;
int main(int argc, char **argv)
{
	char **cmd;
	int i, n, m, size;
	if (argc < 2) {
		fprint(2, "Usage: xargs [cmd args...]\n");
		exits("bad usage");
	}
	if (Binit(&stdin, 0, OREAD) == -1) {
		fprint(2, "couldn't init stdin\n");
		exits("error");
	}
	cmd = malloc((argc - 1 + NUMARGS + 1) * sizeof(char *));
	n = 0;
	for (i = 1; i < argc; i++) {
		cmd[n++] = argv[i];
	}

	for (;;) {
		int eof = 0;;
		size = 0;
		for (m = 0; m < NUMARGS && size < MAXSIZE; m++) {
			if ((cmd[n + m] = makeline()) == 0) {
				eof = 1;
				break;
			}
			size += strlen(cmd[n + m]);
		}
		cmd[n + m] = 0;
		if (m > 0)
			run(cmd);
		for (i = 0; i < m; i++) {
			free(cmd[n+i]);
		}
		if (eof)
			break;
	}
	return 0;
}

int pwait(void)
{
	Waitmsg msg;
	if (wait(&msg) == -1)
		return -1;
	return atoi(msg.pid);
}

void run(char **cmd)
{
	char buf[256];
	int npid, pid = fork();
	if (pid == -1) {
		perror("couldn't fork");
		exits("no forkage");
	}
	if (pid == 0) {
		if (cmd[0][0] != '/' && strncmp(cmd[0], "./", 2)) {
			sprint(buf, "/bin/%s", cmd[0]);
			exec(buf, cmd);
		} else {
			exec(cmd[0], cmd);
		}
		perror(cmd[0]);
		exits("no exec");
	}
	while ((npid = pwait()) != pid && npid != -1)
		;
}

char *makeline(void)
{
	char *ln, *ret;
	int len;
	if ((ln = Brdline(&stdin, '\n')) == 0) {
		return 0;
	}
	len = Blinelen(&stdin);
	ret = malloc(len + 1);
	memcpy(ret, ln, len);
	ret[len-1] = '\0';
	return ret;
}

             reply	other threads:[~2001-08-08 10:45 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-08 10:45 rog [this message]
2001-08-08 17:08 ` [9fans] Sam question gene garbutt
2001-08-14  9:44   ` Douglas A. Gwyn
2001-08-14 17:02     ` [9fans] h is for help in tp Rick Hohensee
2001-08-16 21:07     ` [9fans] Sam question Boyd Roberts
2001-08-17  8:50       ` Douglas A. Gwyn
2001-08-17 10:27         ` Re[2]: " Matt
2001-08-17 12:14         ` Boyd Roberts
2001-08-17 12:35           ` Re[2]: " Matt
2001-08-20  8:56           ` Douglas A. Gwyn
2001-08-20 11:11             ` Boyd Roberts
2001-08-20 16:25             ` Sam Ducksworth
2001-08-20 15:11               ` Boyd Roberts
2001-08-21  8:37                 ` Ozan Yigit
2001-08-21  9:31                   ` Boyd Roberts
2001-08-22  2:52                     ` Donald Brownlee
2001-08-17 10:22       ` Re[2]: " Matt

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=20010808103450.57264199E3@mail.cse.psu.edu \
    --to=rog@vitanuova.com \
    --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).