merging Emacs.app (NeXTstep port)
[bpt/emacs.git] / lib-src / mac-fix-env.m
1 /* mac-fix-env: A small utility to pick up the shell environment on MacOS X
2 and insert it into the file ~/.MacOSX/environment.plist
3 creating if necessary.
4 Copyright (C) 1989, 1993, 2005, 2008 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 usage:
24 Run from command line (in Terminal) once or whenever path changes:
25
26 /Applications/Emacs.app/Contents/MacOS/bin/mac-fix-env
27
28 (change initial part to where you installed Emacs).
29 */
30
31 #import <Foundation/Foundation.h>
32 #include <stdlib.h>
33
34 int main(int argc, char *argv[])
35 {
36 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
37 NSMutableDictionary *envPlist;
38 NSString *file = [[NSHomeDirectory()
39 stringByAppendingPathComponent:@".MacOSX"]
40 stringByAppendingPathComponent:@"environment.plist"];
41 NSString *path = [NSString stringWithCString: getenv("PATH")];
42
43 envPlist = [[NSDictionary dictionaryWithContentsOfFile: file] mutableCopy];
44 if (envPlist == nil)
45 {
46 // create
47 NSString *dir = [file stringByDeletingLastPathComponent];
48 envPlist = [NSMutableDictionary dictionaryWithCapacity: 5];
49
50 if ([[NSFileManager defaultManager] fileExistsAtPath: dir] == NO)
51 {
52 if ([[NSFileManager defaultManager] createDirectoryAtPath:dir
53 attributes:nil]==NO)
54 {
55 NSLog(@":\nCould not create directory at '%@'; aborting.",dir);
56 return 1;
57 }
58 }
59 }
60
61 [envPlist setObject: path forKey: @"PATH"];
62
63 if ([envPlist writeToFile: file atomically: YES] == NO)
64 {
65 NSLog(@":\nCould not write file at '%@'; aborting.", file);
66 return 1;
67 }
68
69 NSLog(@":\nWrote file to '%@'.\nPlease inspect it to make sure PATH is correct.", file);
70 return 0;
71 }