[go: up one dir, main page]

Menu

[9c90d7]: / util / utnt.c  Maximize  Restore  History

Download this file

374 lines (334 with data), 13.6 kB

  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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* This file was written by Bill Cox, originally in 1991, and maintained since. It is hereby
* placed into the public domain.
*/
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <io.h>
#include <time.h>
#include <sys/stat.h>
#include "ddutil.h"
jmp_buf utJmpBuf[UT_MAX_SETJMP_DEPTH];
/*--------------------------------------------------------------------------------------------------
Decrement the setjmp stack, and longjmp.
--------------------------------------------------------------------------------------------------*/
void utLongjmp (void)
{
utSetjmpDepth--;
if (utSetjmpDepth < 0 || utSetjmpDepth > UT_MAX_SETJMP_DEPTH) {
utExit("utLongjmp: No utSetjmp for utLongjmp");
} else {
longjmp(utJmpBuf[utSetjmpDepth], 1);
}
}
/*--------------------------------------------------------------------------------------------------
Default Error notification routine in absence of user-speified.
--------------------------------------------------------------------------------------------------*/
void ErrorNotify (
char *string)
{
printf("%s\n", string);
fflush(stdout);
}
/*--------------------------------------------------------------------------------------------------
Allocate a block of memory. Don't fill with 0's.
--------------------------------------------------------------------------------------------------*/
void *mtCalloc (
uint32 numBlocks,
uint16 size)
{
return calloc((unsigned)numBlocks, (unsigned)size);
}
/*--------------------------------------------------------------------------------------------------
Allocate a block of memory. Don't fill with 0's.
--------------------------------------------------------------------------------------------------*/
void *mtMalloc (
uint32 numBlocks,
uint16 size)
{
return malloc(numBlocks*size);
}
/*--------------------------------------------------------------------------------------------------
Reallocate and expand memory.
--------------------------------------------------------------------------------------------------*/
void * mtRealloc (
void *mem,
uint32 numBlocks,
uint16 size)
{
return realloc((char *)mem, (size_t)(numBlocks * size));
}
/*--------------------------------------------------------------------------------------------------
Free a blo`ck of memory. Return TRUE if sucess.
--------------------------------------------------------------------------------------------------*/
bool mtFree (
void *p)
{
free(p);
return true;
}
/*--------------------------------------------------------------------------------------------------
FUNCTION: utGetProfileString
PURPOSE : Function to read a string from a .ini file
--------------------------------------------------------------------------------------------------*/
bool utGetProfileString(char *szToolName, char *szKeyName,
char *szDefault, char *szBuffer, int16 nSize)
{
bool ret;
ret = (bool) GetProfileString(szToolName, szKeyName,
szDefault, szBuffer, nSize);
return ret;
}
/*--------------------------------------------------------------------------------------------------
FUNCTION: utGetProfileString
PURPOSE : Function to write a string to win.ini file
--------------------------------------------------------------------------------------------------*/
bool utWriteProfileString(char *szSection, char *szKeyName,
char *szValue)
{
bool ret;
ret = (bool) WriteProfileString(szSection, szKeyName, szValue);
return ret;
}
/*--------------------------------------------------------------------------------------------------
FUNCTION: utGetPrivateProfileInt
PURPOSE : Function to read a Int from a .ini file
--------------------------------------------------------------------------------------------------*/
int32 utGetPrivateProfileInt(char *szToolName,
char *szKeyName, int32 nDefault, char *szIniFileName)
{
int32 val;
val = GetPrivateProfileInt(szToolName, szKeyName, nDefault, szIniFileName);
return val;
}
/*--------------------------------------------------------------------------------------------------
FUNCTION: utGetPrivateProfileString
PURPOSE : Function to read a string from a .ini file
--------------------------------------------------------------------------------------------------*/
void utGetPrivateProfileString(char *szToolName,
char *szKeyName, char *szDefault, char *szBuffer,
int16 nSize, char *szIniFileName)
{
GetPrivateProfileString(szToolName, szKeyName, szDefault,
szBuffer, nSize, szIniFileName);
}
/*--------------------------------------------------------------------------------------------------
FUNCTION: utWritePrivateProfileString
PURPOSE : Function to write a string to a .ini file
--------------------------------------------------------------------------------------------------*/
void utWritePrivateProfileString(char *szToolName,
char *szKeyName, char *szSetStr, char *szIniFileName)
{
WritePrivateProfileString(szToolName, szKeyName, szSetStr, szIniFileName);
}
/*--------------------------------------------------------------------------------------------------
Launches an application. Simple interface - no params except
command line & wkgDir. Command line can include args. Wkg dir
can be NULL.
--------------------------------------------------------------------------------------------------*/
bool utLaunchApp (
char* cmdLine,
char* wkgDir)
{
PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo;
memset((void *)(&startupInfo), 0, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
if (!CreateProcess(NULL, (char *)cmdLine, NULL, NULL, FALSE, 0, NULL, wkgDir,
&startupInfo, &processInfo)) {
utError("UT1000: Failed to launch program %s - Error %d", cmdLine,
GetLastError());
return false;
}
return true;
}
/*--------------------------------------------------------------------------------------------------
Terminate. Unix optionally writes a core file for debug, NT just exits.
--------------------------------------------------------------------------------------------------*/
void utAbnormalProgramTermination(void)
{
exit(-1);
}
/*--------------------------------------------------------------------------------------------------
FUNCTION: utGetModuleDirectory
PURPOSE : Function to get the path of the specified module (exe or dll)
NOTE: Module must be loaded already!
--------------------------------------------------------------------------------------------------*/
bool utGetModuleDirectory (
char *moduleName,
char *path,
int16 max_path)
{
HMODULE hModule;
hModule = GetModuleHandle(moduleName); /* get module instance */
if (!hModule) {
return false;
}
GetModuleFileName(hModule, path, max_path);
return true;
}
/*--------------------------------------------------------------------------------------------------
Set the environment variable.
--------------------------------------------------------------------------------------------------*/
void utSetEnvironmentVariable(
char *name,
char *value)
{
SetEnvironmentVariable(name, value);
}
/*--------------------------------------------------------------------------------------------------
Get the environment variable. Return NULL if it is not set.
--------------------------------------------------------------------------------------------------*/
char *utGetEnvironmentVariable(
char *name)
{
return getenv(name);
}
/*--------------------------------------------------------------------------------------------------
Match the string to a file and return the matched file name.
--------------------------------------------------------------------------------------------------*/
char *utGlob(
char *fileName)
{
if(utAccess(fileName, NULL)) {
return utCopyString(fileName);
}
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Find the current working directory name.
--------------------------------------------------------------------------------------------------*/
char *utGetcwd(void)
{
char buffer[UTSTRLEN];
_getcwd(buffer, UTSTRLEN);
return utCopyString(buffer);
}
/*--------------------------------------------------------------------------------------------------
Take the relative path, and return the full path.
--------------------------------------------------------------------------------------------------*/
char *utFullPath(
char *relativePath)
{
char fullPath[UTSTRLEN];
_fullpath(fullPath, relativePath, UTSTRLEN);
return utCopyString(fullPath);
}
/*--------------------------------------------------------------------------------------------------
Determine the access to a file
--------------------------------------------------------------------------------------------------*/
bool utAccess(
char *name,
char *mode)
{
if(mode == NULL) {
return _access(name, 0) == 0;
}
if(!strcmp(mode, "r")) {
return _access(name, 4) == 0;
}
if(!strcmp(mode, "w")) {
return _access(name, 2) == 0;
}
if(!strcmp(mode, "x")) {
return _access(name, 0) == 0;
}
utExit("Unknown mode passed to utAccess");
return false; /* Dummy return */
}
/*--------------------------------------------------------------------------------------------------
Return the path name which an executable resides.
--------------------------------------------------------------------------------------------------*/
char *utExecPath(
char *program)
{
char *path, *p;
char *fullPath;
char actualPath[1024];
char *fullPathProgram;
if(strchr(program, UTDIRSEP) != NULL) {
return utFullPath(program);
} else {
if(_getcwd(actualPath, 1024) != 0) {
fullPath = utMakeString(strlen(actualPath)+1+strlen(program)+1);
fullPath = utSprintf("%s%c%s", actualPath, UTDIRSEP, program);
fullPathProgram = utGlob(fullPath);
if(fullPathProgram != NULL) {
return fullPathProgram;
}
fullPathProgram = utGlob(utSprintf("%s.exe", fullPath));
if(fullPathProgram != NULL) {
return fullPathProgram;
}
} else {
path = getenv ("PATH");
if(path != NULL) {
p = path;
do {
path = p;
p = strchr(path, UTPATHSEP);
if(p == NULL) {
p = strchr(path, '\0');
}
if(p == path) {
fullPath = utCopyString(program);
} else {
fullPath = utMakeString((p - path) + 1);
strncpy(fullPath, path, p - path);
fullPath = utSprintf("%s%c%s", fullPath, UTDIRSEP, program);
}
fullPath = utGlob(fullPath);
if(fullPath != NULL) {
return fullPath;
}
fullPath = utGlob(utSprintf("%s.exe", fullPath));
if(fullPath != NULL) {
return fullPath;
}
} while(*p++ != '\0');
}
}
}
utError("could not find executable %s", program);
return NULL;
}
/*--------------------------------------------------------------------------------------------------
Find the size of the file.
--------------------------------------------------------------------------------------------------*/
uint64 utFindFileSize(
char *fileName)
{
struct stat statbuf;
if(stat(fileName, &statbuf)) {
return 0;
}
return statbuf.st_size;
}
/*--------------------------------------------------------------------------------------------------
Find the size of the file.
--------------------------------------------------------------------------------------------------*/
bool utDirectoryExists(
char *dirName)
{
struct stat statbuf;
if(stat(dirName, &statbuf)) {
return false;
}
return (statbuf.st_mode & S_IFDIR) != 0;
}
/*--------------------------------------------------------------------------------------------------
Truncate the file.
--------------------------------------------------------------------------------------------------*/
void utTruncateFile(
char *fileName,
uint64 length)
{
int file = open(fileName, 0);
if(file == -1) {
utWarning("utTruncateFile: could not open file %s", fileName);
return;
}
chsize(file, length);
close(file);
}