diff --git a/ltree.c b/ltree.c index 87dbded..70724aa 100644 [/color]--- a/ltree.c +++ b/ltree.c @@ -25,6 +25,7 @@ // These are set as result from command line parsing. static int opt_prefix_fd = AT_FDCWD; static int opt_print_separator = '\n'; +static int opt_dir_separator = '/'; typedef struct archive ArchiveRead; typedef struct archive_entry ArchiveEntry; @@ -80,12 +81,18 @@ static int entry_print (struct archive_entry *e, void *d) { (void) d; +// int res = fputs (archive_entry_pathname_utf8 (e), stdout); + int res = fputs (archive_entry_pathname (e), stdout); - if (fputs (archive_entry_pathname (e), stdout) == EOF || - putchar (opt_print_separator) == EOF) + if ((archive_entry_filetype(e) == AE_IFDIR) && res != EOF) { + res = putchar (opt_dir_separator); + } + + if (res == EOF || putchar (opt_print_separator) == EOF) { return errno; } + return ENTRY_OK; } @@ -226,12 +233,13 @@ main (int argc, char **argv) enum action opt_action = ACTION_UNKNOWN; _Bool opt_verbose = false; char *opt_prefix = NULL; + FILE *fd = stdin; ptr_auto(ArchiveRead) a = NULL; handle_auto(DirFd) prefix_fd = AT_FDCWD; int opt; - while ((opt = getopt (argc, argv, "lCR0vp:h")) != -1) { + while ((opt = getopt (argc, argv, "lCR0vpf:h")) != -1) { switch (opt) { case '0': opt_print_separator = '\0'; @@ -260,8 +268,17 @@ main (int argc, char **argv) opt_prefix = optarg; break; + case 'f': + fd = fopen(optarg, "r"); + if (fd == NULL) { + fprintf (stderr, "Error opening '%s' file\n", optarg); + return EXIT_FAILURE; + } + break; + case 'h': fprintf (stderr, "Usage: %s [-lCR] [-0v] [-p path] < mtree\n", argv[0]); + fprintf (stderr, "or %s [-lCR] [-0v] [-f file] [-p path]\n", argv[0]); return EXIT_SUCCESS; default: @@ -290,7 +307,7 @@ main (int argc, char **argv) a = archive_read_new (); archive_read_support_format_mtree (a); - if (archive_read_open_FILE (a, stdin) != ARCHIVE_OK) + if (archive_read_open_FILE (a, fd) != ARCHIVE_OK) goto beach; int exit_code = EXIT_SUCCESS; @@ -330,14 +347,25 @@ main (int argc, char **argv) } } end: + if (fd != stdin) + fclose(fd); + return exit_code; beach: fprintf (stderr, "%s: error: %s\n", argv[0], archive_error_string (a)); + + if (fd != stdin) + fclose(fd); + return EXIT_FAILURE; getopt_action_err: fprintf (stderr, "%s: Option '-%c' invalid in '%s' mode.\n", argv[0], opt, s_entry_actions[opt_action].name); + + if (fd != stdin) + fclose(fd); + return EXIT_FAILURE; [color=#000000] } |