Skip to content

Commit be3c61e

Browse files
authored
Merge pull request #294 from DilumAluthge-forks/dpa/tmpdir
Allow user to override the temporary directory by setting the `TMPDIR` environment variable
2 parents 85196da + a926ea5 commit be3c61e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

Shared/NSFileManager+TempFiles.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ - (NSString *)createTempFileNamed:(NSString *)fileName withContents:(NSString *)
4141
// http://cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html
4242

4343
NSString *tmpFileNameTemplate = fileName ? fileName : @"tmp_file_platypus_macos.XXXXXX";
44+
// Allow user to override the temporary directory by setting the TMPDIR environment variable
4445
NSString *tmpDir = NSTemporaryDirectory();
45-
if (!tmpDir) {
46-
NSLog(@"NSTemporaryDirectory() returned nil");
47-
return nil;
46+
char *tmpdir_cstring = getenv("TMPDIR");
47+
if (tmpdir_cstring != NULL && strlen(tmpdir_cstring) > 0) {
48+
tmpDir = [NSString stringWithUTF8String:tmpdir_cstring];
49+
} else {
50+
if (!tmpDir) {
51+
NSLog(@"NSTemporaryDirectory() returned nil");
52+
return nil;
53+
}
4854
}
49-
55+
5056
NSString *tempFileTemplate = [tmpDir stringByAppendingPathComponent:tmpFileNameTemplate];
5157
const char *tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation];
5258
char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1);

Shared/PlatypusAppSpec.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,17 @@ - (BOOL)create {
260260
// .app bundle
261261
// Get temporary directory, make sure it's kosher. Apparently NSTemporaryDirectory() can return nil
262262
// See http://www.cocoadev.com/index.pl?NSTemporaryDirectory
263+
// Allow user to override the temporary directory by setting the TMPDIR environment variable
263264
NSString *tmpPath = NSTemporaryDirectory();
264-
if (tmpPath == nil) {
265+
char *tmpdir_cstring = getenv("TMPDIR");
266+
if (tmpdir_cstring != NULL && strlen(tmpdir_cstring) > 0) {
267+
tmpPath = [NSString stringWithUTF8String:tmpdir_cstring];
268+
} else {
269+
if (tmpPath == nil) {
265270
tmpPath = @"/tmp/"; // Fallback, just in case
271+
}
266272
}
267-
273+
268274
// Make sure we can write to temp path
269275
if ([FILEMGR isWritableFileAtPath:tmpPath] == NO) {
270276
_error = [NSString stringWithFormat:@"Could not write to the temp directory '%@'.", tmpPath];

0 commit comments

Comments
 (0)