- if (len) {
- if (oldelem) {
- pathbuf += sprintf(pathbuf, "%s/", oldelem->name);
- }
-
- /* (len == 0) if the original path was "drivers/char/"
- * and we have run already two rounds, having elem
- * pointing at the drivers/char directory.
- */
- elem = elem->item.tree->entries;
- while (elem) {
- if ((strlen(elem->name) == len) &&
- !strncmp(elem->name, path, len)) {
- /* found */
- break;
- }
- elem = elem->next;
- }
- if (!elem)
- return NULL;
-
- oldelem = elem;
- }
- path = next;
- }
-
- return elem;
-}
-
-static const char *entry_type(struct tree_entry_list *e)
-{
- return (e->directory ? "tree" : "blob");
-}
-
-static const char *entry_hex(struct tree_entry_list *e)
-{
- return sha1_to_hex(e->directory
- ? e->item.tree->object.sha1
- : e->item.blob->object.sha1);
-}
-
-/* forward declaration for mutually recursive routines */
-static int show_entry(struct tree_entry_list *, int, char *pathbuf);
-
-static int show_children(struct tree_entry_list *e, int level, char *pathbuf)
-{
- int oldlen = strlen(pathbuf);
-
- if (e != &root_entry)
- sprintf(pathbuf + oldlen, "%s/", e->name);
-
- if (prepare_children(e))
- die("internal error: ls-tree show_children called with non tree");
- e = e->item.tree->entries;
- while (e) {
- show_entry(e, level, pathbuf);
- e = e->next;
- }
-
- pathbuf[oldlen] = '\0';
-
- return 0;
-}
-
-static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)
-{
- int err = 0;
-
- if (e != &root_entry) {
- printf("%06o %s %s %s%s", e->mode, entry_type(e),
- entry_hex(e), pathbuf, e->name);
- putchar(line_termination);
- }
-
- if (e->directory) {
- /* If this is a directory, we have the following cases:
- * (1) This is the top-level request (explicit path from the
- * command line, or "root" if there is no command line).
- * a. Without any flag. We show direct children. We do not
- * recurse into them.
- * b. With -r. We do recurse into children.
- * c. With -d. We do not recurse into children.
- * (2) We came here because our caller is either (1-a) or
- * (1-b).
- * a. Without any flag. We do not show our children (which
- * are grandchildren for the original request).
- * b. With -r. We continue to recurse into our children.
- * c. With -d. We should not have come here to begin with.
- */
- if (level == 0 && !(ls_options & LS_TREE_ONLY))
- /* case (1)-a and (1)-b */
- err = err | show_children(e, level+1, pathbuf);
- else if (level && ls_options & LS_RECURSIVE)
- /* case (2)-b */
- err = err | show_children(e, level+1, pathbuf);
- }
- return err;
-}
-
-static int list_one(const char *path)
-{
- int err = 0;
- char pathbuf[MAXPATHLEN + 1];
- struct tree_entry_list *e = find_entry(path, pathbuf);
- if (!e) {
- /* traditionally ls-tree does not complain about
- * missing path. We may change this later to match
- * what "/bin/ls -a" does, which is to complain.
- */
- return err;