merge ROS Shell without integrated explorer part into trunk
[reactos.git] / posix / lib / psxdll / sys / utsname / uname.c
1 /* $Id: uname.c,v 1.4 2002/10/29 04:45:46 rex Exp $
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS POSIX+ Subsystem
6 * FILE: lib/psxdll/sys/utsname/uname.c
7 * PURPOSE: Get name of current system
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 19/12/2001: Created
11 */
12
13 #include <ddk/ntddk.h>
14 #include <ntdll/rtl.h>
15 #include <string.h>
16 #include <sys/utsname.h>
17 #include <psx/stdlib.h>
18 #include <psx/debug.h>
19 #include <psx/errno.h>
20
21 int uname(struct utsname *name)
22 {
23 NTSTATUS nErrCode;
24 OBJECT_ATTRIBUTES oaKeyAttribs;
25 UNICODE_STRING wstrKeyPath;
26 UNICODE_STRING wstrValueName;
27 UNICODE_STRING wstrValueData;
28 ANSI_STRING strValueData;
29 PKEY_VALUE_PARTIAL_INFORMATION pkvpiKeyValue;
30 ULONG nKeyValueSize;
31 HANDLE hKey;
32
33 /* system name and version info are fixed strings, at the moment */ /* FIXME? */
34 strncpy(name->sysname, "ReactOS" , 255);
35 strncpy(name->release, "0.0" , 255);
36 strncpy(name->version, "pre-alpha", 255);
37
38 /* hardware identifier */
39 /* FIXME: this should definitely be determined programmatically */
40 strncpy(name->machine, "i386" , 255);
41
42 /* we use the active computer's name as the node name */
43 /* TODO: POSIX-style registry functions */
44
45 /* initialize the registry key path */
46 RtlInitUnicodeString(
47 &wstrKeyPath,
48 L"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName"
49 );
50
51 /* initialize object attributes */
52 oaKeyAttribs.Length = sizeof(OBJECT_ATTRIBUTES);
53 oaKeyAttribs.RootDirectory = NULL;
54 oaKeyAttribs.ObjectName = &wstrKeyPath;
55 oaKeyAttribs.Attributes = OBJ_CASE_INSENSITIVE /* | OBJ_OPENLINK | OBJ_OPENIF */ /* FIXME? */;
56 oaKeyAttribs.SecurityDescriptor = NULL;
57 oaKeyAttribs.SecurityQualityOfService = NULL;
58
59 /* open the key object */
60 nErrCode = NtOpenKey
61 (
62 &hKey,
63 KEY_QUERY_VALUE,
64 &oaKeyAttribs
65 );
66
67 /* failure */
68 if(!NT_SUCCESS(nErrCode))
69 {
70
71 ERR("NtOpenKey() failed with status 0x%08X", nErrCode);
72 errno = __status_to_errno(nErrCode);
73 return (-1);
74 }
75
76 /* initialize the registry value name */
77 RtlInitUnicodeString(&wstrValueName, L"ComputerName");
78
79 /* fake query - null buffer and zero length to pre-fetch the appropriate buffer size */
80 nErrCode = NtQueryValueKey
81 (
82 hKey,
83 &wstrValueName,
84 KeyValuePartialInformation,
85 NULL,
86 0,
87 &nKeyValueSize
88 );
89
90 /* success */
91 if(nErrCode == (NTSTATUS)STATUS_BUFFER_TOO_SMALL)
92 {
93
94 /* allocate the appropriate buffer size */
95 if(nKeyValueSize < sizeof(KEY_VALUE_PARTIAL_INFORMATION)) /* just to be sure */
96 nKeyValueSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION);
97
98 pkvpiKeyValue = __malloc(nKeyValueSize);
99
100 }
101 /* failure */
102 else
103 {
104
105 ERR("NtQueryValueKey() failed with status 0x%08X", nErrCode);
106 NtClose(hKey);
107 errno = __status_to_errno(nErrCode);
108 return (-1);
109 }
110
111 /* query the value */
112 nErrCode = NtQueryValueKey
113 (
114 hKey,
115 &wstrValueName,
116 KeyValuePartialInformation,
117 pkvpiKeyValue,
118 nKeyValueSize,
119 &nKeyValueSize
120 );
121
122 /* close the key handle (not needed anymore) */
123 NtClose(hKey);
124
125 /* failure */
126 if(!NT_SUCCESS(nErrCode))
127 {
128 ERR("NtQueryValueKey() failed with status 0x%08X", nErrCode);
129 __free(pkvpiKeyValue);
130 errno = __status_to_errno(nErrCode);
131 return (-1);
132 }
133
134 /* make wstrValueData refer to the Data field of the key value information */
135 wstrValueData.Length = pkvpiKeyValue->DataLength;
136 wstrValueData.MaximumLength = wstrValueData.Length;
137 wstrValueData.Buffer = (PWCHAR)&(pkvpiKeyValue->Data[0]);
138
139 /* make strValueData refer to the nodename buffer */
140 strValueData.Length = 0;
141 strValueData.MaximumLength = 254;
142 strValueData.Buffer = name->nodename;
143
144 RtlUnicodeStringToAnsiString
145 (
146 &strValueData,
147 &wstrValueData,
148 FALSE
149 );
150
151 /* free the key value buffer */
152 __free(pkvpiKeyValue);
153
154 /* null-terminate the returned string */
155 name->nodename[strValueData.Length] = '0';
156
157 INFO
158 (
159 " \
160 name->sysname = \"%s\"\n\
161 tname->nodename = \"%s\"\n\
162 tname->release = \"%s\"\n\
163 tname->version = \"%s\"\n\
164 tname->machine = \"%s\"",
165 name->sysname,
166 name->nodename,
167 name->release,
168 name->version,
169 name->machine
170 );
171
172 /* success */
173 return (0);
174
175 }
176
177 /* EOF */
178