[go: up one dir, main page]

Menu

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

Download this file

259 lines (231 with data), 8.3 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
/*
* This file was written by Bill Cox, originally in 1991, and maintained since. It is hereby
* placed into the public domain.
*/
/*============================================================================
Module : Datadraw
Purpose: Includes UNIX dependent functions.
============================================================================*/
#include <sys/stat.h>
#include <sys/types.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <dirent.h>
#include <unistd.h>
#include "ddutil.h"
jmp_buf utJmpBuf[UT_MAX_SETJMP_DEPTH];
/*--------------------------------------------------------------------------------------------------
Generate a real randome number string.
--------------------------------------------------------------------------------------------------*/
uint8 *utRealRandom(
uint32 length)
{
uint8 *randBuff = utNewBufA(uint8, length);
uint32 xByte;
uint32 seed = (uint32)time(NULL);
utInitSeed(seed);
for(xByte = 0; xByte < length; xByte++) {
randBuff[xByte] = utRandN(0x100);
}
return randBuff;
}
/*--------------------------------------------------------------------------------------------------
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);
}
}
/*--------------------------------------------------------------------------------------------------
Take the relative path, and return the full path.
--------------------------------------------------------------------------------------------------*/
char *utFullPath(
char *relativePath)
{
char *fileName = relativePath;
char *dirName = utGetcwd();
if(*relativePath == UTDIRSEP || (*relativePath != '\0' && relativePath[1] == ':')) {
return utCopyString(relativePath);
}
if(fileName == NULL) {
return utSprintf("%s%c%s", dirName, UTDIRSEP, relativePath);
}
while(!strncmp(fileName, ".." UTDIRSEP_STRING, 3)) {
fileName = fileName + 3;
dirName = utDirName(dirName);
if(dirName == NULL) {
return utSprintf("%s%c%s", dirName, UTDIRSEP, relativePath);
}
}
return utSprintf("%s%c%s", dirName, UTDIRSEP, fileName);
}
/*--------------------------------------------------------------------------------------------------
Allocate a block of memory. Don't fill with 0's.
--------------------------------------------------------------------------------------------------*/
void *mtCalloc(
uint32 numBlocks,
uint16 size)
{
return calloc(numBlocks, 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 block of memory. Return TRUE if sucess.
--------------------------------------------------------------------------------------------------*/
bool mtFree(
void *p)
{
free((char *)p);
return true;
}
/*--------------------------------------------------------------------------------------------------
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)
{
return (bool) system(cmdLine);
}
/*--------------------------------------------------------------------------------------------------
Find the current working directory name.
--------------------------------------------------------------------------------------------------*/
char *utGetcwd(void)
{
char buffer[UTSTRLEN];
getcwd(buffer, UTSTRLEN);
return utConvertDirSepChars(buffer);
}
/*--------------------------------------------------------------------------------------------------
Change to the directory.
--------------------------------------------------------------------------------------------------*/
bool utChdir(
char *dirName)
{
if(!chdir(dirName)) {
return false;
}
return true;
}
/*--------------------------------------------------------------------------------------------------
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, R_OK) == 0;
}
if(!strcmp(mode, "w")) {
return access(name, W_OK) == 0;
}
if(!strcmp(mode, "x")) {
return access(name, X_OK) == 0;
}
utExit("Unknown mode passed to utAccess");
return false; /* Dummy return */
}
/*--------------------------------------------------------------------------------------------------
Return the path name which an executable resides.
--------------------------------------------------------------------------------------------------*/
char *utExecPath(
char *program)
{
return program; /* On Windows, we have the real path */
}
/*--------------------------------------------------------------------------------------------------
Set the environment variable.
--------------------------------------------------------------------------------------------------*/
void utSetEnvironmentVariable(
char *name,
char *value)
{
putenv(utSprintf("%s=%s", name, value));
}
/*--------------------------------------------------------------------------------------------------
Get the environment variable. Return NULL if it is not set.
--------------------------------------------------------------------------------------------------*/
char *utGetEnvironmentVariable(
char *name)
{
return getenv(name);
}
/*--------------------------------------------------------------------------------------------------
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 S_ISDIR(statbuf.st_mode);
}
/*--------------------------------------------------------------------------------------------------
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);
}
/*--------------------------------------------------------------------------------------------------
Delete the file.
--------------------------------------------------------------------------------------------------*/
bool utDeleteFile(
char *fileName)
{
return !unlink(fileName);
}