annotate lib/dirtree.c @ 823:0429050a224b

uuencode and uudecode by Erich Plondke.
author Rob Landley <rob@landley.net>
date Mon, 18 Mar 2013 09:11:21 -0500
parents 90e98cdb1b7c
children b4faf2ae39e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* dirtree.c - Functions for dealing with directory trees.
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2007 Rob Landley <rob@landley.net>
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 */
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
5
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 #include "toys.h"
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
8 static int notdotdot(char *name)
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
9 {
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
10 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 0;
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
11
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
12 return 1;
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
13 }
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
14
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
15 // Default callback, filters out "." and "..".
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
16
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
17 int dirtree_notdotdot(struct dirtree *catch)
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
18 {
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
19 // Should we skip "." and ".."?
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
20 return notdotdot(catch->name) ? DIRTREE_SAVE|DIRTREE_RECURSE : 0;
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
21 }
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
22
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
23 // Create a dirtree node from a path, with stat and symlink info.
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
24 // (This doesn't open directory filehandles yet so as not to exhaust the
779
90e98cdb1b7c Make dirtree_handle_callback() start with dirtree_ like the rest of the dirtree functions.
Rob Landley <rob@landley.net>
parents: 739
diff changeset
25 // filehandle space on large trees, dirtree_handle_callback() does that.)
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
27 struct dirtree *dirtree_add_node(struct dirtree *parent, char *name,
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
28 int symfollow)
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
30 struct dirtree *dt = NULL;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
31 struct stat st;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
32 char buf[4096];
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
33 int len = 0, linklen = 0;
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 143
diff changeset
34
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
35 if (name) {
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
36 // open code this because haven't got node to call dirtree_parentfd() on yet
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
37 int fd = parent ? parent->data : AT_FDCWD;
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
38
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
39 if (fstatat(fd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW)) goto error;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
40 if (S_ISLNK(st.st_mode)) {
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
41 if (0>(linklen = readlinkat(fd, name, buf, 4095))) goto error;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
42 buf[linklen++]=0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
43 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
44 len = strlen(name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
45 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
46 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
47 dt->parent = parent;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
48 if (name) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
49 memcpy(&(dt->st), &st, sizeof(struct stat));
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
50 strcpy(dt->name, name);
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
52 if (linklen) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
53 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
54 dt->data = --linklen;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
55 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
56 }
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
58 return dt;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
59
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
60 error:
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
61 if (notdotdot(name)) {
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
62 char *path = parent ? dirtree_path(parent, 0) : "";
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
63 perror_msg("%s%s%s",path, parent ? "/" : "", name);
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
64 }
739
451d7e91232e Complicate the rm -i behavior to do what posix specifies.
Rob Landley <rob@landley.net>
parents: 735
diff changeset
65 if (parent) parent->symlink = (char *)1;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
66 free(dt);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
67 return 0;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
68 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
69
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
70 // Return path to this node, assembled recursively.
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
71
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
72 char *dirtree_path(struct dirtree *node, int *plen)
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
73 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
74 char *path;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
75 int len;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
76
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
77 if (!node || !node->name) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
78 path = xmalloc(*plen);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
79 *plen = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
80 return path;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
81 }
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
82
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
83 len = (plen ? *plen : 0)+strlen(node->name)+1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
84 path = dirtree_path(node->parent, &len);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
85 if (len && path[len-1] != '/') path[len++]='/';
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
86 len = (stpcpy(path+len, node->name) - path);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
87 if (plen) *plen = len;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
88
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
89 return path;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
90 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
91
601
a6a541b7fc34 Add dirtree_parentfd()
Rob Landley <rob@landley.net>
parents: 593
diff changeset
92 int dirtree_parentfd(struct dirtree *node)
a6a541b7fc34 Add dirtree_parentfd()
Rob Landley <rob@landley.net>
parents: 593
diff changeset
93 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
94 return node->parent ? node->parent->data : AT_FDCWD;
601
a6a541b7fc34 Add dirtree_parentfd()
Rob Landley <rob@landley.net>
parents: 593
diff changeset
95 }
a6a541b7fc34 Add dirtree_parentfd()
Rob Landley <rob@landley.net>
parents: 593
diff changeset
96
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
97 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 //
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
99 // By default, allocates a tree of struct dirtree, not following symlinks
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
100 // If callback==NULL, or callback always returns 0, allocate tree of struct
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
101 // dirtree and return root of tree. Otherwise call callback(node) on each
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
102 // hit, free structures after use, and return NULL.
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
103 //
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
104
779
90e98cdb1b7c Make dirtree_handle_callback() start with dirtree_ like the rest of the dirtree functions.
Rob Landley <rob@landley.net>
parents: 739
diff changeset
105 struct dirtree *dirtree_handle_callback(struct dirtree *new,
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
106 int (*callback)(struct dirtree *node))
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
108 int flags, dir = S_ISDIR(new->st.st_mode);
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
109
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
110 if (!callback) callback = dirtree_notdotdot;
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
111
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
112 flags = callback(new);
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
113
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
114 if (dir) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
115 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
116 new->data = openat(dirtree_parentfd(new), new->name, 0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
117 dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
118 if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
119 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
120 }
582
b88bc7dcdb48 Update chgrp so -R works, tweaking DIRTREE_COMEAGAIN design along the way.
Rob Landley <rob@landley.net>
parents: 580
diff changeset
121
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
122 // If this had children, it was callback's job to free them already.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
123 if (!(flags & DIRTREE_SAVE)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
124 free(new);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
125 new = NULL;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
126 }
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
127
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
128 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 }
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
130
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
131 // Recursively read/process children of directory node (with dirfd in data),
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
132 // filtering through callback().
143
9cbb323f297f Break out dirtree.c and let it call a function instead of returning the data.
Rob Landley <rob@landley.net>
parents:
diff changeset
133
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
134 void dirtree_recurse(struct dirtree *node,
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
135 int (*callback)(struct dirtree *node), int symfollow)
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
136 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
137 struct dirtree *new, **ddt = &(node->child);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
138 struct dirent *entry;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
139 DIR *dir;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
140
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
141 if (!(dir = fdopendir(node->data))) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
142 char *path = dirtree_path(node, 0);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
143 perror_msg("No %s", path);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
144 free(path);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
145 close(node->data);
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
146
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
147 return;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
148 }
569
2e0367cb9585 More work on ls. Now ls -lR sort of works-ish.
Rob Landley <rob@landley.net>
parents: 565
diff changeset
149
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
150 // according to the fddir() man page, the filehandle in the DIR * can still
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
151 // be externally used by things that don't lseek() it.
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
152
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
153 // The extra parentheses are to shut the stupid compiler up.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
154 while ((entry = readdir(dir))) {
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
155 if (!(new = dirtree_add_node(node, entry->d_name, symfollow)))
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
156 continue;
779
90e98cdb1b7c Make dirtree_handle_callback() start with dirtree_ like the rest of the dirtree functions.
Rob Landley <rob@landley.net>
parents: 739
diff changeset
157 new = dirtree_handle_callback(new, callback);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
158 if (new == DIRTREE_ABORTVAL) break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
159 if (new) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
160 *ddt = new;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
161 ddt = &((*ddt)->next);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
162 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
163 }
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
164
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
165 // This closes filehandle as well, so note it
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
166 closedir(dir);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 629
diff changeset
167 node->data = -1;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
168 }
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
169
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
170 // Create dirtree from path, using callback to filter nodes.
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
171 // If callback == NULL allocate a tree of struct dirtree nodes and return
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
172 // pointer to root node.
607
8bee9c27c219 Unify chown and chgrp, add support for -hHLP flags.
Rob Landley <rob@landley.net>
parents: 601
diff changeset
173 // symfollow is just for the top of tree, callback return code controls children
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
174
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
175 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
176 {
735
3aaba60133c8 Have dirtree_add_node() set parent so error message can provide full path.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
177 struct dirtree *root = dirtree_add_node(0, path, 0);
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
178
779
90e98cdb1b7c Make dirtree_handle_callback() start with dirtree_ like the rest of the dirtree functions.
Rob Landley <rob@landley.net>
parents: 739
diff changeset
179 return root ? dirtree_handle_callback(root, callback) : DIRTREE_ABORTVAL;
565
44abf4d901f3 Rewrite dirtree so we don't need readdir, scandir, and fts.h. Rewrite ls (from scratch) to use new dirtree infrastructure. (This breaks everything else that currently uses dirtree.)
Rob Landley <rob@landley.net>
parents: 320
diff changeset
180 }