From a942cadb8404b91212a5f5a394a239fa7c72504f Mon Sep 17 00:00:00 2001 From: Garrett Davidson Date: Sun, 15 May 2016 14:51:12 -0700 Subject: [PATCH 1/2] Added support for reading files from stdin --- Makefile | 2 +- trash.m | 122 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 105 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 5bcdc4e..07e35ae 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ trash: $(SOURCE_FILES) @echo @echo ---- Compiling: @echo ====================================== - $(CC) -O2 -Wall -Wextra -force_cpusubtype_ALL -mmacosx-version-min=10.5 -arch i386 -framework AppKit -framework ScriptingBridge -o $@ $(SOURCE_FILES) + $(CC) -g -O2 -Wall -Wextra -force_cpusubtype_ALL -mmacosx-version-min=10.5 -arch i386 -framework AppKit -framework ScriptingBridge -o $@ $(SOURCE_FILES) analyze: @echo diff --git a/trash.m b/trash.m index 0006dc4..a1a584e 100644 --- a/trash.m +++ b/trash.m @@ -40,6 +40,7 @@ of this software and associated documentation files (the "Software"), to deal // (Apple reserves OSStatus values outside the range 1000-9999 inclusive) #define kHGAppleScriptError 9999 #define kHGNotAllFilesTrashedError 9998 +#define MAX_BUF 100 static const int VERSION_MAJOR = 0; static const int VERSION_MINOR = 8; @@ -434,7 +435,19 @@ static void printUsage() @"\n", versionNumberStr()); } - +void stripBackslashes(char *s) { + char *a, *b; + a = b = s; + + while (*a) { + if ((*a) != '\\') { + *b = *a; + b++; + } + a++; + } + *b = '\0'; +} int main(int argc, char *argv[]) { @@ -443,11 +456,92 @@ int main(int argc, char *argv[]) int exitValue = 0; myBasename = basename(argv[0]); - if (argc == 1) - { + + char** files; + if (isatty(fileno(stdin))) { + + if (argc == 1) { printUsage(); return 0; + } + + files = argv; + + } else { + char buffer[MAX_BUF]; + size_t contentSize = 1; + char *input = malloc(sizeof(char) * MAX_BUF); + + if(input == NULL) { + perror("Failed to allocate input buffer"); + exit(1); + } + + input[0] = '\0'; + + while(fgets(buffer, MAX_BUF, stdin)) { + char *prev = input; + + contentSize += strlen(buffer); + input = realloc(input, contentSize); + + if (input == NULL) { + perror("Failed to reallocate buffer"); + free(prev); + exit(2); + } + + strcat(input, buffer); + } + + if (ferror(stdin)) { + free(input); + perror("Error reading from stdin."); + exit(3); + } + + input[strlen(input) - 1] = '\0'; + + int argCount = 1; + int i = 0; + while (input[i] != '\0') { + if (input[i] == ' ') { + if (i == 0 || input[i-1] != '\\') { + argCount++; + } + } + + i++; + } + + files = (char**) malloc((argCount + 1) * sizeof(char*)); + + files[1] = input; + + i = 2; + while (argCount - i + 1) { + char* arg = strstr(input, " "); + while (arg[-1] == '\\') { + + arg = strstr(arg+1, " "); + } + *arg = '\0'; + arg += 1; + + files[i] = arg; + i++; + input = arg; + } + + + for (i = 1; i < argCount + 1; i++) { + stripBackslashes(files[i]); + } + argc += argCount; + optind = 1; } + + BOOL arg_list = NO; BOOL arg_empty = NO; @@ -461,7 +555,7 @@ int main(int argc, char *argv[]) ; int opt; - while ((opt = getopt(argc, argv, optstring)) != EOF) + while ((opt = getopt(argc, files, optstring)) != EOF) { switch (opt) { @@ -513,21 +607,21 @@ int main(int argc, char *argv[]) // after the first restricted item are not trashed at all NSMutableArray *nonRestrictedPathsForFinder = [NSMutableArray arrayWithCapacity:argc]; NSMutableArray *restrictedPathsForFinder = [NSMutableArray arrayWithCapacity:argc]; - + int i; for (i = optind; i < argc; i++) { // Note: don't standardize the path! we don't want to expand leaf symlinks. - NSString *path = [[NSString stringWithUTF8String:argv[i]] stringByExpandingTildeInPath]; + NSString *path = [[NSString stringWithUTF8String:files[i]] stringByExpandingTildeInPath]; if (path == nil) { - PrintfErr(@"trash: %s: invalid path\n", argv[i]); + PrintfErr(@"trash: %s: invalid path\n", files[i]); continue; } if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { - PrintfErr(@"trash: %s: path does not exist\n", argv[i]); + PrintfErr(@"trash: %s: path does not exist\n", files[i]); exitValue = 1; continue; } @@ -550,7 +644,7 @@ int main(int argc, char *argv[]) { PrintfErr( @"trash: %s: cannot get file privileges (%i: %@)\n", - argv[i], + files[i], getCatalogStatus, osStatusToErrorString(getCatalogStatus) ); @@ -576,7 +670,7 @@ int main(int argc, char *argv[]) exitValue = 1; PrintfErr( @"trash: %s: can not move to trash (%i: %@)\n", - argv[i], + files[i], status, osStatusToErrorString(status) ); @@ -618,11 +712,3 @@ int main(int argc, char *argv[]) [autoReleasePool release]; return exitValue; } - - - - - - - - From 8a4b05af65a0956a302a0e040e661c2d68796031 Mon Sep 17 00:00:00 2001 From: Garrett Davidson Date: Sun, 15 May 2016 14:55:12 -0700 Subject: [PATCH 2/2] Cleaned up --- Makefile | 2 +- trash.m | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 07e35ae..5bcdc4e 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ trash: $(SOURCE_FILES) @echo @echo ---- Compiling: @echo ====================================== - $(CC) -g -O2 -Wall -Wextra -force_cpusubtype_ALL -mmacosx-version-min=10.5 -arch i386 -framework AppKit -framework ScriptingBridge -o $@ $(SOURCE_FILES) + $(CC) -O2 -Wall -Wextra -force_cpusubtype_ALL -mmacosx-version-min=10.5 -arch i386 -framework AppKit -framework ScriptingBridge -o $@ $(SOURCE_FILES) analyze: @echo diff --git a/trash.m b/trash.m index a1a584e..604a577 100644 --- a/trash.m +++ b/trash.m @@ -472,7 +472,7 @@ int main(int argc, char *argv[]) size_t contentSize = 1; char *input = malloc(sizeof(char) * MAX_BUF); - if(input == NULL) { + if (input == NULL) { perror("Failed to allocate input buffer"); exit(1); } @@ -480,19 +480,19 @@ int main(int argc, char *argv[]) input[0] = '\0'; while(fgets(buffer, MAX_BUF, stdin)) { - char *prev = input; + char *prev = input; - contentSize += strlen(buffer); - input = realloc(input, contentSize); + contentSize += strlen(buffer); + input = realloc(input, contentSize); - if (input == NULL) { - perror("Failed to reallocate buffer"); - free(prev); - exit(2); - } - - strcat(input, buffer); + if (input == NULL) { + perror("Failed to reallocate buffer"); + free(prev); + exit(2); } + + strcat(input, buffer); + } if (ferror(stdin)) { free(input); @@ -537,6 +537,7 @@ int main(int argc, char *argv[]) for (i = 1; i < argCount + 1; i++) { stripBackslashes(files[i]); } + argc += argCount; optind = 1; } @@ -607,7 +608,7 @@ int main(int argc, char *argv[]) // after the first restricted item are not trashed at all NSMutableArray *nonRestrictedPathsForFinder = [NSMutableArray arrayWithCapacity:argc]; NSMutableArray *restrictedPathsForFinder = [NSMutableArray arrayWithCapacity:argc]; - + int i; for (i = optind; i < argc; i++) {