commit 5db190002b9d2de4403d5f003141539f8b2f10a9
parent 1367143adc74745208d3696de347aaea3d98a1b7
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Mon, 28 Feb 2022 21:34:35 +0000
Dir: switch to new "API" format
Diffstat:
M | extra/Dir.c | | | 60 | +++++++++++++++--------------------------------------------- |
1 file changed, 15 insertions(+), 45 deletions(-)
diff --git a/extra/Dir.c b/extra/Dir.c
@@ -4,69 +4,39 @@
char *rroot = "/mnt/richterm";
-char *
-getnewid(void)
-{
- char *s, *id;
- int fd;
- long n;
- s = smprint("%s/new", rroot);
- fd = open(s, OREAD);
- free(s);
- if (fd < 0) sysfatal("getnewid: %r");
- id = mallocz(256, 1);
- n = read(fd, id, 256);
- if (n <= 0) sysfatal("getnewid: %r");
- return id;
-}
-
-int
-rprint(char *id, char *file, char *str)
-{
- int fd;
- long n;
- char *path;
- path = smprint("%s/%s/%s", rroot, id, file);
- fd = open(path, OWRITE);
- if (fd < 0) sysfatal("rprint: %r");
- n = write(fd, str, strlen(str));
- if (n < 0) sysfatal("rprint: %r");
- close(fd);
- return 0;
-}
-
void
main(int argc, char **argv)
{
- int fd;
+ int fd, tfd;
long i, n;
Dir *dp, *dbuf;
- char *path, buf[4096], *id, *spacer;
+ char *path, buf[4096];
+
path = getwd(buf, sizeof(buf));
+
if (argc == 2) path = argv[1];
+
fd = open(path, OREAD);
if (fd < 0) sysfatal("%r");
+
dp = dirfstat(fd);
if (dp == nil) sysfatal("%r");
- id = getnewid();
- rprint(id, "text", "../\n");
- rprint(id, "link", "..");
- free(id);
-
if (dp->mode & DMDIR) {
+
+ char *rpath = smprint("%s/text", rroot);
+ tfd = open(rpath , OWRITE | OTRUNC);
+ if (tfd < 0) sysfatal("can't open %s: %r", rpath);
+
dbuf = mallocz(DIRMAX, 1);
n = dirreadall(fd, &dbuf);
for (i = 0; i < n; i++) {
- char *label;
- spacer = "";
+ char *spacer = "";
if (dbuf[i].mode & DMEXEC) spacer = "*";
if (dbuf[i].mode & DMDIR) spacer = "/";
- label = smprint("%s%s\n", dbuf[i].name, spacer);
- id = getnewid();
- rprint(id, "text", label);
- rprint(id, "link", dbuf[i].name);
- free(id);
+
+ fprint(tfd, "l%s\n.%s%s\nn\n", dbuf[i].name, dbuf[i].name, spacer);
}
+ fprint(tfd, "l\n");
} else sysfatal("not a directory");
}