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>
18 #import <Foundation/NSPlatform_win32.h>
20 #import <objc/runtime.h>
22 @implementation NSProcessInfo
24 int NSProcessInfoArgc=0;
25 const char * const *NSProcessInfoArgv=NULL;
27 -(NSInteger)incrementCounter {
33 [_counterLock unlock];
38 +(NSProcessInfo *)processInfo {
39 return NSThreadSharedInstance(@"NSProcessInfo");
48 _counterLock=[NSLock new];
52 -(NSUInteger)processorCount {
53 NSUnimplementedMethod();
57 -(NSUInteger)activeProcessorCount {
58 NSUnimplementedMethod();
62 -(uint64_t)physicalMemory {
63 NSUnimplementedMethod();
67 -(NSUInteger)operatingSystem {
68 NSUnimplementedMethod();
72 -(NSString *)operatingSystemName {
73 NSUnimplementedMethod();
77 -(NSString *)operatingSystemVersionString {
79 OSVERSIONINFOEX osVersion;
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;
97 if (osVersion.szCSDVersion != '\0') {
98 servicePack = [NSString stringWithCString:osVersion.szCSDVersion];
103 switch (systemVersion) {
105 return [NSString stringWithFormat: @"Windows 8 %@", servicePack];
108 return [NSString stringWithFormat: @"Windows Server 2012 %@", servicePack];
111 return [NSString stringWithFormat: @"Windows 7 %@", servicePack];
114 return [NSString stringWithFormat: @"Windows Server 2008 R2 %@", servicePack];
117 return [NSString stringWithFormat: @"Windows Vista %@", servicePack];
120 return [NSString stringWithFormat: @"Windows Server 2003 R2 %@", servicePack];
123 return [NSString stringWithFormat: @"Windows XP Professional x64 Edition %@", servicePack];
126 return [NSString stringWithFormat: @"Windows XP %@", servicePack];
129 return [NSString stringWithFormat: @"Windows 2000 %@", servicePack];
132 return [NSString stringWithFormat: @"%d.%d %d %d", osVersion.dwMajorVersion, osVersion.dwMinorVersion, osVersion.wServicePackMajor, osVersion.wServicePackMinor ];
136 NSUnimplementedMethod();
141 -(NSString *)hostName {
143 _hostName=[[[NSPlatform currentPlatform] hostName] retain];
146 _hostName=@"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){
168 -(void)setProcessName:(NSString *)name {
169 [_processName release];
170 _processName=[name copy];
173 -(int)processIdentifier {
174 return NSPlatformProcessID();
177 -(NSArray *)arguments {
179 _arguments=[[[NSPlatform currentPlatform] arguments] retain];
185 -(NSDictionary *)environment {
186 if(_environment==nil)
187 _environment=[[[NSPlatform currentPlatform] environment] retain];
192 -(NSString *)globallyUniqueString {
193 return NSStringWithFormat(@"%@_%d_%d_%d_%d",[self hostName],
194 [self processIdentifier],0,0,[self incrementCounter]);
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();
207 // init NSConstantString reference-tag (see http://lists.apple.com/archives/objc-language/2006/Jan/msg00013.html)
209 Class cls = objc_lookUpClass("NSConstantString");
210 // memcpy(&_NSConstantStringClassReference, cls, sizeof(_NSConstantStringClassReference));
211 cls = objc_lookUpClass("NSDarwinString");
214 extern int __CFConstantStringClassReference[24];
216 extern int __CFConstantStringClassReference[12];
219 memcpy(&__CFConstantStringClassReference, cls, sizeof(__CFConstantStringClassReference));
221 // Override the compiler version of the class
222 //objc_addClass(&_NSConstantStringClassReference);