File: win32.c

package info (click to toggle)
rat 4.2.22-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,717
  • sloc: ansic: 36,542; tcl: 2,740; sh: 2,675; makefile: 295
file content (252 lines) | stat: -rw-r--r-- 6,530 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright (c) 1996 The Regents of the University of California.
 * All rights reserved.
 *
 * This module contributed by John Brezak <brezak@apollo.hp.com>.
 * January 31, 1996
 *
 * Additional contributions Orion Hodson (UCL) 1996-98.
 */
 
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] = 
	"$Id: win32.c,v 1.40 2000/02/15 17:27:07 ucaccsp Exp $";
#endif /* HIDE_SOURCE_STRINGS */

#ifndef lint
static char rcsid[] =
    "@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/rat/win32.c,v 1.40 2000/02/15 17:27:07 ucaccsp Exp $ (LBL)";
#endif

#include "config_win32.h"
#include "debug.h"
#include "util.h"

int
uname(struct utsname *ub)
{
        SYSTEM_INFO   sysinfo;
        OSVERSIONINFO osinfo;
        
        GetSystemInfo(&sysinfo);
        osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
        GetVersionEx(&osinfo);
        
        strcpy(ub->sysname, "Windows");
        strcpy(ub->nodename, "Unknown"); /* could use gethostbyname */
        
        /* ub->release and ub->machine*/
        if (osinfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
                sprintf(ub->release, "NT %d.%d", osinfo.dwMajorVersion, osinfo.dwMinorVersion);
                switch (sysinfo.wProcessorArchitecture) {
                case PROCESSOR_ARCHITECTURE_INTEL:
                        sprintf(ub->machine, "i%d86", sysinfo.wProcessorLevel);
                        break;
                case PROCESSOR_ARCHITECTURE_MIPS :
                        (void)strncpy(ub->machine, "mips", SYS_NMLN);
                        break;
                case PROCESSOR_ARCHITECTURE_ALPHA:
                        sprintf(ub->machine, "alpha-%d", sysinfo.wProcessorLevel);
                        break;
                case PROCESSOR_ARCHITECTURE_PPC:
                        (void)strncpy(ub->machine, "ppc", SYS_NMLN);
                        break;
                default:
                        (void)strncpy(ub->machine, "unknown", SYS_NMLN);
                        break;
                }
        } else if (osinfo.dwPlatformId == VER_PLATFORM_WIN32s) {
                strcpy(ub->release, "3.1"); 
                strcpy(ub->machine, "i386"); 
        } else if (osinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
                if (osinfo.dwMinorVersion == 0) {
                        strcpy(ub->release, "95");
                } else {
                        strcpy(ub->release, "98");
                }
                switch(sysinfo.dwProcessorType) {
                case PROCESSOR_INTEL_386:     
                        strcpy(ub->machine, "i386"); 
                        break;
                case PROCESSOR_INTEL_486:     
                        strcpy(ub->machine, "i486"); 
                        break;  
                case PROCESSOR_INTEL_PENTIUM: 
                        strcpy(ub->machine, "i586"); 
                        break;
                }
        } else {
                sprintf(ub->release, "Unknown %d.%d", osinfo.dwMajorVersion, osinfo.dwMinorVersion);
                sprintf(ub->machine, "Unknown");
        }

        /* Append service pack info onto end of release */
        if (osinfo.szCSDVersion != NULL) {
                DWORD avail = SYS_NMLN - strlen(ub->release);
                strncat(ub->release, osinfo.szCSDVersion, avail);
        }

        /* ub->version */
        sprintf(ub->version, "%lu", osinfo.dwBuildNumber);
                                                
        return TRUE;
}

uid_t
getuid(void) 
{ 
    return 0;
    
}

gid_t
getgid(void)
{
    return 0;
}

unsigned long
gethostid(void)
{
    char   hostname[WSADESCRIPTION_LEN];
    struct hostent *he;

    if ((gethostname(hostname, WSADESCRIPTION_LEN) == 0) &&
        (he = gethostbyname(hostname)) != NULL) {
            return *((unsigned long*)he->h_addr_list[0]);        
    }

    /*XXX*/
    return 0;
}

int
nice(int pri)
{
    return 0;
}

extern char *__progname;

void
ShowMessage(int level, char *msg)
{
    MessageBeep(level);
    MessageBox(NULL, msg, __progname,
	       level | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
}

static char szTemp[256];

int
printf(const char *fmt, ...)
{
	int retval;
	
	va_list ap;
	va_start(ap, fmt);
	retval = _vsnprintf(szTemp, 256, fmt, ap);
	OutputDebugString(szTemp);
	va_end(ap);

	return retval;
}

int
fprintf(FILE *f, const char *fmt, ...)
{
	int 	retval;
	va_list	ap;

	va_start(ap, fmt);
	if (f == stderr) {
		retval = _vsnprintf(szTemp, 256, fmt, ap);
		OutputDebugString(szTemp);
	} else {
		retval = vfprintf(f, fmt, ap);
	}
	va_end(ap);
	return retval;
}

void
perror(const char *msg)
{
    DWORD cMsgLen;
    CHAR *msgBuf;
    DWORD dwError = GetLastError();
    
    cMsgLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
			    FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL,
			    dwError,
			    MAKELANGID(0, SUBLANG_ENGLISH_US),
			    (LPTSTR) &msgBuf, 512,
			    NULL);
    if (!cMsgLen)
	fprintf(stderr, "%s%sError code %lu\n",
		msg?msg:"", msg?": ":"", dwError);
    else {
	fprintf(stderr, "%s%s%s\n", msg?msg:"", msg?": ":"", msgBuf);
	LocalFree((HLOCAL)msgBuf);
    }
}

extern int main(int argc, const char *argv[]);
extern int __argc;
extern char **__argv;

static char argv0[255];		/* Buffer used to hold argv0. */

HINSTANCE hAppInstance;
HINSTANCE hAppPrevInstance;

char *__progname = "main";

#define WS_VERSION_ONE MAKEWORD(1,1)
#define WS_VERSION_TWO MAKEWORD(2,2)

int APIENTRY
WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine,
    int nCmdShow)
{
    char *p;
    WSADATA WSAdata;
    int r;
    if (WSAStartup(WS_VERSION_TWO, &WSAdata) != 0 &&
        WSAStartup(WS_VERSION_ONE, &WSAdata) != 0) {
    	MessageBox(NULL, "Windows Socket initialization failed. TCP/IP stack\nis not installed or is damaged.", "Network Error", MB_OK | MB_ICONERROR);
        exit(-1);
    }

    debug_msg("WSAStartup OK: %sz\nStatus:%s\n", WSAdata.szDescription, WSAdata.szSystemStatus);

    hAppInstance     = hInstance;
    hAppPrevInstance = hPrevInstance; 

    SetMessageQueue(64);

    if (GetModuleFileName(NULL, argv0, 255) == 0) {
	    MessageBox(NULL, "GetModuleFileName failed\n", "Random Error", MB_OK | MB_ICONERROR);
	    exit(-1);
    }
    p = argv0;
    __progname = strrchr(p, '/');
    if (__progname != NULL) {
	__progname++;
    } else {
	__progname = strrchr(p, '\\');
	if (__progname != NULL) {
	    __progname++;
	} else {
	    __progname = p;
	}
    }
    
    r = main(__argc, (const char**)__argv);

    WSACleanup();
    return r;
}