([NSProcessInfo -hostName]): Remove unnecessary inefficiency: use

stack to hold temporary string, not malloc/free; just return the
string, not a copy.  Conform to GNU coding standards in use of braces.
([NSProcessInfo -processName]): Likewise.  Just return the string, not
a copy.
([NSProcessInfo -environment]): Likewise.
([NSProcessInfo -arguments]): Likewise.
(_gnu_arguments, _gnu_environment): Make them constant classes, not
mutable.
(_gnu_process_args): Alloc _gnu_processName NSString, don't get an
autoreleased string and then retain it.  Initialize _gnu_arguments as
a constant NSArray, not NSMutableArray.  Initialize _gnu_environment
as a constant NSDictionary, not NSMutableDictionary.  Conform to the
GNU coding standards in use of braces.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@656 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Andrew McCallum 1995-11-03 16:12:07 +00:00
parent 65a5bbc051
commit 101037f18a

View file

@ -138,7 +138,7 @@ static NSString* _gnu_hostName = nil;
static NSString* _gnu_processName = nil; static NSString* _gnu_processName = nil;
// Array of NSStrings (argv[1] .. argv[argc-1]) // Array of NSStrings (argv[1] .. argv[argc-1])
static NSMutableArray* _gnu_arguments = nil; static NSArray* _gnu_arguments = nil;
// Dictionary of environment vars and their values // Dictionary of environment vars and their values
static NSMutableDictionary* _gnu_environment = nil; static NSMutableDictionary* _gnu_environment = nil;
@ -153,29 +153,40 @@ _gnu_process_args(int argc, char *argv[], char *env[])
int i; int i;
/* Getting the process name */ /* Getting the process name */
_gnu_processName = [NSString stringWithCString:argv[0]]; _gnu_processName = [[NSString alloc] initWithCString:argv[0]];
[_gnu_processName retain];
/* Copy the argument list */ /* Copy the argument list */
_gnu_arguments = [[NSMutableArray arrayWithCapacity:0] retain]; {
for (i = 1; i < argc; i++) { id obj_argv[argc];
[_gnu_arguments addObject:[NSString stringWithCString:argv[i]]]; for (i = 1; i < argc; i++)
obj_argv[i-1] = [NSString stringWithCString:argv[i]];
_gnu_arguments = [[NSArray alloc] initWithObjects:obj_argv count:argc-1];
} }
/* Copy the evironment list */ /* Copy the evironment list */
_gnu_environment = [[NSMutableDictionary dictionaryWithCapacity:0] retain]; {
char *cp;
NSMutableArray *keys = [NSMutableArray new];
NSMutableArray *values = [NSMutableArray new];
i = 0; i = 0;
while (env[i]) { while (env[i])
char* cp; {
cp = strchr(env[i],'='); cp = strchr(env[i],'=');
/* Temporary set *cp to \000 ... for copying purpose */ /* Temporary set *cp to \0 for copying purposes */
*cp = '\000'; *cp = '\0';
[_gnu_environment setObject:[NSString stringWithCString:(cp+1)] [keys addObject: [NSString stringWithCString:env[i]]];
forKey:[NSString stringWithCString:env[i]]]; [values addObject: [NSString stringWithCString:cp+1]];
/* Return the original value of environ[i] */ /* Return the original value of environ[i] */
*cp = '='; *cp = '=';
i++; i++;
} }
_gnu_environment = [[NSDictionary alloc] initWithObjects:values
forKeys:keys];
/* Do this explicitly, because we probably don't have
a NSAutoreleasePool initialized yet. */
[keys release];
[values release];
}
} }
/* Place the _gnu_process_args function in the _libc_subinit section so /* Place the _gnu_process_args function in the _libc_subinit section so
@ -297,31 +308,29 @@ int main(int argc, char *argv[], char *env[])
*************************************************************************/ *************************************************************************/
- (NSArray *)arguments - (NSArray *)arguments
{ {
return [[_gnu_arguments copyWithZone:[self zone]] autorelease]; return _gnu_arguments;
} }
- (NSDictionary *)environment - (NSDictionary *)environment
{ {
return [[_gnu_environment copyWithZone:[self zone]] autorelease]; return _gnu_environment;
} }
- (NSString *)hostName - (NSString *)hostName
{ {
if (!_gnu_hostName) { if (!_gnu_hostName)
char *hn = NSZoneMalloc([self zone], _GNU_MAX_HOST_NAMELEN); {
char hn[_GNU_MAX_HOST_NAMELEN];
gethostname(hn, _GNU_MAX_HOST_NAMELEN); gethostname(hn, _GNU_MAX_HOST_NAMELEN);
_gnu_hostName = [NSString stringWithCString:hn]; _gnu_hostName = [[NSString alloc] initWithCString:hn];
[_gnu_hostName retain];
NSZoneFree([self zone], hn);
} }
return _gnu_hostName;
return [[_gnu_hostName copyWithZone:[self zone]] autorelease];
} }
- (NSString *)processName - (NSString *)processName
{ {
return [[_gnu_processName copyWithZone:[self zone]] autorelease]; return _gnu_processName;
} }
- (NSString *)globallyUniqueString - (NSString *)globallyUniqueString