[hackers] [9base] added du from plan9port || Anselm R Garbe

From: <hg_AT_suckless.org>
Date: Sat, 8 Aug 2009 08:54:27 +0000 (UTC)

changeset: 35:02f8506674af
parent: 31:bd9949cc661c
user: Anselm R Garbe <anselm_AT_garbe.us>
date: Sat Aug 08 09:53:34 2009 +0100
files: Makefile du/Makefile du/du.c
description:
added du from plan9port

diff -r bd9949cc661c -r 02f8506674af Makefile
--- a/Makefile Sun Aug 02 09:31:02 2009 +0000
+++ b/Makefile Sat Aug 08 09:53:34 2009 +0100
@@ -3,7 +3,7 @@
 
 include config.mk
 
-SUBDIRS = lib9 yacc awk basename bc dc cat cleanname date echo grep ls \
+SUBDIRS = lib9 yacc awk basename bc dc du cat cleanname date echo grep ls \
                 rc read sed seq sleep sort tee test touch tr uniq
 
 all:
diff -r bd9949cc661c -r 02f8506674af du/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/du/Makefile Sat Aug 08 09:53:34 2009 +0100
@@ -0,0 +1,6 @@
+# du - du unix port from plan9
+# Depends on ../lib9
+
+TARG = du
+
+include ../std.mk
diff -r bd9949cc661c -r 02f8506674af du/du.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/du/du.c Sat Aug 08 09:53:34 2009 +0100
@@ -0,0 +1,194 @@
+#include <u.h>
+#include <libc.h>
+
+extern vlong du(char*, Dir*);
+extern vlong k(vlong);
+extern void err(char*);
+extern int warn(char*);
+extern int seen(Dir*);
+
+int aflag;
+int fflag;
+int nflag;
+int sflag;
+int tflag;
+int uflag;
+int qflag;
+char *fmt = "%llud\t%s\n";
+vlong blocksize = 1024LL;
+
+void
+main(int argc, char *argv[])
+{
+ int i;
+ char *s, *ss;
+
+ ARGBEGIN {
+ case 'a': /* all files */
+ aflag = 1;
+ break;
+ case 's': /* only top level */
+ sflag = 1;
+ break;
+ case 'f': /* ignore errors */
+ fflag = 1;
+ break;
+ case 'n': /* all files, number of bytes */
+ aflag = 1;
+ nflag = 1;
+ break;
+ case 't': /* return modified/accessed time */
+ tflag = 1;
+ break;
+ case 'u': /* accessed time */
+ uflag = 1;
+ break;
+ case 'q': /* qid */
+ fmt = "%.16llux\t%s\n";
+ qflag = 1;
+ break;
+ case 'b': /* block size */
+ s = ARGF();
+ if(s) {
+ blocksize = strtoul(s, &ss, 0);
+ if(s == ss)
+ blocksize = 1;
+ if(*ss == 'k')
+ blocksize *= 1024;
+ }
+ break;
+ } ARGEND
+ if(argc==0)
+ print(fmt, du(".", dirstat(".")), ".");
+ else
+ for(i=0; i<argc; i++)
+ print(fmt, du(argv[i], dirstat(argv[i])), argv[i]);
+ exits(0);
+}
+
+vlong
+du(char *name, Dir *dir)
+{
+ int fd, i, n;
+ Dir *buf, *d;
+ char file[256];
+ vlong nk, t;
+
+ if(dir == nil)
+ return warn(name);
+
+ fd = open(name, OREAD);
+ if(fd < 0)
+ return warn(name);
+
+ if((dir->qid.type&QTDIR) == 0)
+ nk = k(dir->length);
+ else{
+ nk = 0;
+ while((n=dirread(fd, &buf)) > 0) {
+ d = buf;
+ for(i=0; i<n; i++, d++) {
+ if((d->qid.type&QTDIR) == 0) {
+ t = k(d->length);
+ nk += t;
+ if(aflag) {
+ sprint(file, "%s/%s", name, d->name);
+ if(tflag) {
+ t = d->mtime;
+ if(uflag)
+ t = d->atime;
+ }
+ if(qflag)
+ t = d->qid.path;
+ print(fmt, t, file);
+ }
+ continue;
+ }
+ if(strcmp(d->name, ".") == 0 ||
+ strcmp(d->name, "..") == 0 ||
+ seen(d))
+ continue;
+ sprint(file, "%s/%s", name, d->name);
+ t = du(file, d);
+ nk += t;
+ if(tflag) {
+ t = d->mtime;
+ if(uflag)
+ t = d->atime;
+ }
+ if(qflag)
+ t = d->qid.path;
+ if(!sflag)
+ print(fmt, t, file);
+ }
+ free(buf);
+ }
+ if(n < 0)
+ warn(name);
+ }
+ close(fd);
+ if(tflag) {
+ if(uflag)
+ return dir->atime;
+ return dir->mtime;
+ }
+ if(qflag)
+ return dir->qid.path;
+ return nk;
+}
+
+#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; i<c->n; 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(c->cache == 0)
+ err("malloc failure");
+ }
+ c->cache[c->n++] = *dir;
+ return 0;
+}
+
+void
+err(char *s)
+{
+ fprint(2, "du: %s: %r\n", s);
+ exits(s);
+}
+
+int
+warn(char *s)
+{
+ if(fflag == 0)
+ fprint(2, "du: %s: %r\n", s);
+ return 0;
+}
+
+vlong
+k(vlong n)
+{
+ if(nflag)
+ return n;
+ n = (n+blocksize-1)/blocksize;
+ return n*blocksize/1024LL;
+}
Received on Sat Aug 08 2009 - 08:54:27 UTC

This archive was generated by hypermail 2.2.0 : Sun Aug 16 2009 - 14:18:57 UTC