diff --git a/ChangeLog b/ChangeLog index 0794508..d36f03d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,12 @@ Change log for duff Started as of version 0.3.1. +2006-10-07 Camilla Berglund + + * duff.c (main): Always kill trailing slashes (and in a nicer way). + * duff.c (kill_trailing_slashes): Added function. + * duff.c (main) (read_path): Don't kill newlines for -0 option. + 2006-09-29 Camilla Berglund * duff.c (main) (usage): Added -0 option (credits to Clemens Lucas Fries). diff --git a/duff.c b/duff.c index 711f6ae..04015bc 100644 --- a/duff.c +++ b/duff.c @@ -180,6 +180,7 @@ static void bugs(void) static int read_path(char* path, size_t size) { int c, i = 0; + size_t length; if (null_terminate_flag) { @@ -197,11 +198,30 @@ static int read_path(char* path, size_t size) { if (!fgets(path, size, stdin)) return -1; + + /* Kill newline terminator, if present */ + length = strlen(path); + if ((length > 0) && (path[length - 1] == '\n')) + path[length - 1] = '\0'; } return 0; } +/* Kills trailing slashes in the specified path (except if it's /). + */ +static void kill_trailing_slashes(char* path) +{ + char* temp; + + while ((temp = strrchr(path, '/'))) + { + if (temp == path || *(temp + 1) != '\0') + break; + *temp = '\0'; + } +} + /* I don't know what this function does. * I just put it in because it looks cool. */ @@ -283,16 +303,10 @@ int main(int argc, char** argv) if (argc) { + /* Read file names from command line */ for (i = 0; i < argc; i++) { - /* Kill trailing slashes (except in "/") */ - while ((temp = strrchr(argv[i], '/'))) - { - if (temp == argv[i] || *(temp + 1) != '\0') - break; - *temp = '\0'; - } - + kill_trailing_slashes(argv[i]); process_path(argv[i], 0); } } @@ -301,9 +315,7 @@ int main(int argc, char** argv) /* Read file names from stdin */ while (read_path(path, sizeof(path)) == 0) { - if ((temp = strchr(path, '\n'))) - *temp = '\0'; - + kill_trailing_slashes(path); process_path(path, 0); } }