Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSProcessInfo.m
blob582d63f706ec61b480dadb0475665817ec8476a7
1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8 #import <Foundation/NSProcessInfo.h>
9 #import <Foundation/NSArray.h>
10 #import <Foundation/NSDictionary.h>
11 #import <Foundation/NSPathUtilities.h>
12 #import <Foundation/NSStringFormatter.h>
13 #import <Foundation/NSRaise.h>
14 #import <Foundation/NSString_cString.h>
15 #import <Foundation/NSThread-Private.h>
16 #import <Foundation/NSPlatform.h>
17 #ifdef __WINDOWS__
18 #import <Foundation/NSPlatform_win32.h>
19 #endif
20 #import <objc/runtime.h>
22 @implementation NSProcessInfo
24 int                 NSProcessInfoArgc=0;
25 const char * const *NSProcessInfoArgv=NULL;
27 -(NSInteger)incrementCounter {
28    NSInteger result;
30    [_counterLock lock];
31    _counter++;
32    result=_counter;
33    [_counterLock unlock];
35    return result;
38 +(NSProcessInfo *)processInfo {
39    return NSThreadSharedInstance(@"NSProcessInfo");
42 -init {
43    _environment=nil;
44    _arguments=nil;
45    _hostName=nil;
46    _processName=nil;
47    _counter=0;
48    _counterLock=[NSLock new];
49    return self;
52 -(NSUInteger)processorCount {
53    NSUnimplementedMethod();
54    return 0;
57 -(NSUInteger)activeProcessorCount {
58    NSUnimplementedMethod();
59    return 0;
62 -(uint64_t)physicalMemory {
63    NSUnimplementedMethod();
64    return 0;
67 -(NSUInteger)operatingSystem {
68    NSUnimplementedMethod();
69    return 0;
72 -(NSString *)operatingSystemName {
73    NSUnimplementedMethod();
74    return 0;
77 -(NSString *)operatingSystemVersionString {
78 #ifdef __WINDOWS__
79         OSVERSIONINFOEX osVersion;
80         int systemVersion;
81         NSString* versionString;
82         NSString* servicePack;
84         osVersion.dwOSVersionInfoSize=sizeof(osVersion);
85         GetVersionEx((OSVERSIONINFO *)&osVersion);
87         // Switches aren't float-friendly, so let's get our major/minor version in
88         // some kind of combination that'll be easier for it to handle.  dwMajorVersion
89         // can live in the 10s digit, with dwMinorVersion living in the 1s digit.
90         // We'll also want to negate in the case we're not an NT_WORKSTATION productType - 
91         // this only matters for Vista and up
92         systemVersion =  osVersion.dwMajorVersion * 10 + osVersion.dwMinorVersion;
93         if(systemVersion >= 60 && osVersion.wProductType != VER_NT_WORKSTATION) {
94                 systemVersion = -systemVersion;
95         }
96         
97         if (osVersion.szCSDVersion != '\0') {
98                 servicePack = [NSString stringWithCString:osVersion.szCSDVersion];
99         } else {
100                 servicePack = @"";
101         }
102                 
103         switch (systemVersion) {
104                 case 62:
105                         return [NSString stringWithFormat: @"Windows 8 %@", servicePack];
106                         break;
107                 case -62:
108                         return [NSString stringWithFormat: @"Windows Server 2012 %@", servicePack];
109                         break;
110                 case 61:
111                         return [NSString stringWithFormat: @"Windows 7 %@", servicePack];
112                         break;
113                 case -61:
114                         return [NSString stringWithFormat: @"Windows Server 2008 R2 %@", servicePack];
115                         break;
116                 case 60:
117                         return [NSString stringWithFormat: @"Windows Vista %@", servicePack];
118                         break;
119                 case -60:
120                         return [NSString stringWithFormat: @"Windows Server 2003 R2 %@", servicePack];
121                         break;
122                 case 52:
123                         return [NSString stringWithFormat: @"Windows XP Professional x64 Edition %@", servicePack];
124                         break;
125                 case 51:
126                         return [NSString stringWithFormat: @"Windows XP %@", servicePack];
127                         break;
128                 case 50:
129                         return [NSString stringWithFormat: @"Windows 2000 %@", servicePack];
130                         break;
131                 default:
132                         return [NSString stringWithFormat: @"%d.%d %d %d", osVersion.dwMajorVersion, osVersion.dwMinorVersion, osVersion.wServicePackMajor, osVersion.wServicePackMinor ];
133                         break;
134         }
135 #else
136     NSUnimplementedMethod();
137     return 0;
138 #endif
141 -(NSString *)hostName {
142    if(_hostName==nil){
143     _hostName=[[[NSPlatform currentPlatform] hostName] retain];
145     if(_hostName==nil)
146      _hostName=@"HOSTNAME";
147    }
149    return _hostName;
152 -(NSString *)processName {
153    if(_processName==nil){
154     NSArray *arguments=[self arguments];
156     if([arguments count]>0)
157      _processName=[[[[[self arguments] objectAtIndex:0]
158        lastPathComponent] stringByDeletingPathExtension] retain];
160     if(_processName==nil){
161      _processName=@"";
162     }
163    }
165    return _processName;
168 -(void)setProcessName:(NSString *)name {
169    [_processName release];
170    _processName=[name copy];
173 -(int)processIdentifier {
174    return NSPlatformProcessID();
177 -(NSArray *)arguments {
178    if(_arguments==nil){
179     _arguments=[[[NSPlatform currentPlatform] arguments] retain];
180    }
182    return _arguments;
185 -(NSDictionary *)environment {
186    if(_environment==nil)
187     _environment=[[[NSPlatform currentPlatform] environment] retain];
189    return _environment;
192 -(NSString *)globallyUniqueString {
193    return NSStringWithFormat(@"%@_%d_%d_%d_%d",[self hostName],
194      [self processIdentifier],0,0,[self incrementCounter]);
197 @end
199 FOUNDATION_EXPORT void __NSInitializeProcess(int argc,const char *argv[]) {
200    NSProcessInfoArgc=argc;
201    NSProcessInfoArgv=argv;
202 #if !defined(GCC_RUNTIME_3)
203 #if !defined(APPLE_RUNTIME_4)
204     OBJCInitializeProcess();
205 #endif
206 #ifdef __APPLE__
207     // init NSConstantString reference-tag (see http://lists.apple.com/archives/objc-language/2006/Jan/msg00013.html)
208     // only Darwin ppc!?
209     Class cls = objc_lookUpClass("NSConstantString");
210 //    memcpy(&_NSConstantStringClassReference, cls, sizeof(_NSConstantStringClassReference));
211     cls = objc_lookUpClass("NSDarwinString");
213 #if __LP64__
214     extern int __CFConstantStringClassReference[24];
215 #else
216     extern int __CFConstantStringClassReference[12];
217 #endif
219     memcpy(&__CFConstantStringClassReference, cls, sizeof(__CFConstantStringClassReference));
220     
221     // Override the compiler version of the class
222     //objc_addClass(&_NSConstantStringClassReference);
223 #endif
224 #endif