From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <73b6c42894bc770bc171ccc36e6bfe6a@vitanuova.com> To: 9fans@cse.psu.edu Subject: Re: [9fans] 'find' From: rog@vitanuova.com In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Date: Mon, 30 Jun 2003 15:07:59 +0100 Topicbox-Message-UUID: e179d988-eacb-11e9-9e20-41e7f4b1d025 > > grep pattern `{du -a /} > > > > leads to something like "grep: virtual memory allocation failure." > > Because the argument list to grep gets impossibly long. there's a little version of xargs that i tend to use in these cases, that i've attached, despite having posted it before as it hadn't been updated for 4e and changes to the libraries mean that it can be smaller than before. e.g. du -a / | sed 's/^[^ ]*[ ]//' | xargs grep pattern (note that awk '{print $1}' won't necessarily work instead of the above sed since filenames can contain spaces). #include #include #include #define NUMARGS 470 #define MAXSIZE 20000 void run(char **cmd) { char buf[256]; int npid, pid; pid = fork(); if (pid == -1) { perror("couldn't fork"); exits("no forkage"); } if (pid == 0) { if (cmd[0][0] != '/' && strncmp(cmd[0], "./", 2)) { snprint(buf, sizeof(buf), "/bin/%s", cmd[0]); exec(buf, cmd); } else { exec(cmd[0], cmd); } perror(cmd[0]); exits("no exec"); } while ((npid = waitpid()) != pid && npid != -1) ; } void main(int argc, char **argv) { char **cmd; int i, n, m, size, eof; Biobuf stdin; if (argc < 2) { fprint(2, "Usage: xargs cmd [args...]\n"); exits("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 (;;) { eof = 0; size = 0; for (m = 0; m < NUMARGS && size < MAXSIZE; m++) { if ((cmd[n + m] = Brdstr(&stdin, '\n', 1)) == 0) { eof = 1; break; } size += Blinelen(&stdin); } cmd[n + m] = 0; if (m > 0) run(cmd); for (i = 0; i < m; i++) free(cmd[n+i]); if (eof) break; } }