File: vos.cpp

package info (click to toggle)
libv1.22 1.22-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,380 kB
  • ctags: 9,765
  • sloc: cpp: 58,097; ansic: 6,814; makefile: 1,597; java: 515; sh: 24
file content (288 lines) | stat: -rw-r--r-- 8,067 bytes parent folder | download
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//===============================================================
//
// vos.cpp - Interface to OS dependent stuff (For Windows)
//
// Copyright (C) 1998  Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================
#include <v/vwin32.h>
#include <v/vos.h>
#include <string.h>
//#include <windows.h>

const int MaxCompNameLen = 256;

//=========================>>> vOS::vOS <<<==========================
  vOS::vOS()
  {
    // Constructor
    // This will be used to determing which OS we are running
    // under: e.g., NT vs 95
  }

//=========================>>> vOS::~vOS <<<==========================
  vOS::~vOS()
  {
    // Destructor
  }

//=========================>>> vOS::vDeleteFile <<<==========================
  int vOS::vDeleteFile(const char* filename)
  {
     return ::DeleteFile(filename) == TRUE;
  }

//=========================>>> vOS::vRenameFile <<<==========================
  int vOS::vRenameFile(const char* oldFilename, const char* newFilename)
  {
     return ::MoveFile((LPCTSTR)oldFilename,(LPCTSTR)newFilename) == TRUE;
  }

//=========================>>> vOS::vChDrive <<<==========================
  int vOS::vChDrive(int drive)
  {
    char temp [3] = "-:";
    temp[0] = drive + 'A';
    return ::SetCurrentDirectory(temp);
  }

//=========================>>> vOS::vGetUserName <<<==========================
  int vOS::vGetEnvVal(char* name, char* val, int maxlen)
  {
     return ::GetEnvironmentVariable(name,val,maxlen);
  }

//=========================>>> vOS::vGetUserName <<<==========================
  int vOS::vGetUserName( char* s, int len)
  {
    // Set s to user name

    char UserName[MaxCompNameLen+1];
    DWORD cch = sizeof UserName;

    if (::GetUserName(UserName, &cch))
      {
	if (strlen(UserName) > (unsigned int)len)
	    UserName[len-1] = 0;
	strcpy(s, UserName);
	return 1;
      }
    s[0] = 0;
    return 0;
  }


//=========================>>> vOS::vGetHostName <<<==========================
  void vOS::vGetHostName(char* s, int len)
  {
    // Get host name

    char HostName[MaxCompNameLen+1];
    DWORD cch = MaxCompNameLen;

    if (::GetComputerName(HostName, &cch))
      {
	strcpy(s,HostName);
      }
    else
	strcpy(s, "PC_Win32");
  }

//=========================>>> vOS::vGetPid <<<==========================
  long vOS::vGetPid()
  {
    // return process ID

    return (long) ::GetCurrentProcessId();
  }


//=========================>>> vOS::vGetCWD <<<==========================
  int vOS::vGetCWD(char* buf, int len)
  {
    // Get name of current directory

    int rv = ::GetCurrentDirectory(len, buf);

    return rv;
  }

//=========================>>> vOS::vChDir <<<==========================
  int vOS::vChDir(const char *path)
  {
    if (path[0] == 0)		/* just checking... */
	return 0;

    if (*path == 0)		/* drive name only */
	return 0;

    return ::SetCurrentDirectory(path);	       /* let the normal chdir() do the rest */
  }

//=========================>>> vOS::vSleep <<<==========================
  void vOS::vSleep(long MSec)
  {
    ::Sleep(MSec);
  }

//=========================>>> vOS::vRunProcess <<<==========================
  int vOS::vRunProcess(const char* cmd, const char* StdOut, const char* StdErr,
      const int Wait, const int minimize)
  {
    STARTUPINFO		si;
    PROCESS_INFORMATION pi;
    DWORD		ret = 0;

    si.cb = sizeof(si);
    si.lpReserved = 0;
    si.lpDesktop = 0;
    si.lpTitle = 0;
    si.dwFlags = 0;
    si.cbReserved2 = 0;
    si.lpReserved2 = 0;
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;
    if (minimize)
        si.wShowWindow = SW_MINIMIZE;
    else
        si.wShowWindow = SW_SHOWNORMAL;

    HANDLE hNewStderr = INVALID_HANDLE_VALUE;
    HANDLE hNewStdout = INVALID_HANDLE_VALUE;


    if (StdErr && *StdErr)	// they specified a stderr out
      {
        hNewStderr = ::CreateFile(
          StdErr,			// address of name of the file
    	  GENERIC_WRITE	,		// access (read-write) mode
    	  (DWORD) 0,			// share mode
          (LPSECURITY_ATTRIBUTES) 0,	// address of security descriptor
    	  CREATE_ALWAYS	,		// how to create
    	  FILE_ATTRIBUTE_NORMAL,	// file attributes
          (HANDLE) 0 		// handle of file with attributes to copy
          );
      }

    if (StdOut && *StdOut)	// they specified a stderr out
      {
        hNewStdout = ::CreateFile(
          StdOut,			// address of name of the file
    	  GENERIC_WRITE	,		// access (read-write) mode
    	  (DWORD) 0,			// share mode
          (LPSECURITY_ATTRIBUTES) 0,	// address of security descriptor
    	  CREATE_ALWAYS	,		// how to create
    	  FILE_ATTRIBUTE_NORMAL,	// file attributes
          (HANDLE) 0 		// handle of file with attributes to copy
          );
      }

    HANDLE myself = ::GetCurrentProcess();	//my own process handle
    HANDLE dupStdout = INVALID_HANDLE_VALUE;
    HANDLE dupStderr = INVALID_HANDLE_VALUE;


    si.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);

    if (hNewStdout != INVALID_HANDLE_VALUE)	// specified out file
      {
        ::DuplicateHandle(myself, hNewStdout ,
           myself,&dupStdout,0, TRUE, DUPLICATE_SAME_ACCESS);
        si.hStdOutput = dupStdout;
      }
    else
      {
         si.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
      }

    if (hNewStderr != INVALID_HANDLE_VALUE)	// specified err file
      {
        ::DuplicateHandle(myself, hNewStderr ,
           myself,&dupStderr,0, TRUE, DUPLICATE_SAME_ACCESS);
        si.hStdError = dupStderr;
      }
    else
      {
         si.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
      }

    OSVERSIONINFO osver;
    osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    ::GetVersionEx(&osver);
    if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
      {
	// Now, run the command
	if (!::CreateProcess (0,		/* Executable name */
	    (char *)cmd,		/* Command to execute */
	0,				/* Process security attributes */
	0,				/* Thread security attributes */
	TRUE,				/* Inherit handles */
// four different alternatives for creation flags...
//	CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
//	CREATE_NEW_CONSOLE,
//	DETACHED_PROCESS,
	0,				/* Creation flags */
	0,				/* Environment */
//	::GetEnvironmentStrings(),	/* Environment */
	0,				/* Current directory */
	&si,			/* Startup information */
	&pi))			/* Process information */
	  {
	    if (dupStdout != INVALID_HANDLE_VALUE)
		::CloseHandle(dupStdout);
	    if (dupStderr != INVALID_HANDLE_VALUE)
		::CloseHandle(dupStderr);
	    return 99;
	  }
      }
    else
      {
	// Now, run the command
	if (!::CreateProcess (0,		/* Executable name */
	    (char *)cmd,		/* Command to execute */
	0,				/* Process security attributes */
	0,				/* Thread security attributes */
	TRUE,			/* Inherit handles */
	/* 0 */CREATE_NEW_CONSOLE,				/* Creation flags */
	0,				/* Environment */
	0,				/* Current directory */
	&si,			/* Startup information */
	&pi))			/* Process information */
	  {
	    if (dupStdout != INVALID_HANDLE_VALUE)
		::CloseHandle(dupStdout);
	    if (dupStderr != INVALID_HANDLE_VALUE)
		::CloseHandle(dupStderr);
	    return 99;
	  }
      }

    if (dupStdout != INVALID_HANDLE_VALUE)
        ::CloseHandle(dupStdout);
    if (dupStderr != INVALID_HANDLE_VALUE)
        ::CloseHandle(dupStderr);

    if (!Wait)
        return 0;


    /* Wait for the command to terminate before continuing */
    ::WaitForSingleObject(pi.hProcess, INFINITE);

    /* Get the command exit code */
    ::GetExitCodeProcess(pi.hProcess, &ret);

    /* Close the handles to the subprocess, so that it goes away */
    ::CloseHandle(pi.hThread);
    ::CloseHandle(pi.hProcess);
    if (hNewStdout != INVALID_HANDLE_VALUE)
        ::CloseHandle(hNewStdout);
    if (hNewStderr != INVALID_HANDLE_VALUE)
        ::CloseHandle(hNewStderr);

    return ret;
  }