00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 # include <sys/types.h>
00045 # include <sys/stat.h>
00046
00047 # include <errno.h>
00048
00049
00050 # include "lclintMacros.nf"
00051 # include "basic.h"
00052 # include "osd.h"
00053 # include "portab.h"
00054
00055
00056
00057 extern int stat (const char *, struct stat *);
00058
00059
00060 static bool osd_executableFileExists (char *);
00061
00062 static bool nextdir (char **p_current_dir, char **p_dir,
00063 size_t *p_len);
00064
00065 extern char *LSLRootName (char *filespec)
00066 {
00067 char *result, *startName, *tail;
00068 size_t nameLength;
00069
00070 tail = strrchr (filespec, CONNECTCHAR);
00071 startName = (tail == NULL ? filespec : &tail[1]);
00072 tail = strrchr (startName, '.');
00073 nameLength = (tail == NULL ? strlen (startName)
00074 : size_fromInt (tail - startName));
00075 result = dmalloc (nameLength + 1);
00076 strncpy (result, startName, nameLength);
00077 result[(int) nameLength] = '\0';
00078 return result;
00079 }
00080
00081 extern char *
00082 osd_getEnvironment (char *env, char *def)
00083 {
00084 char *ret = osd_getEnvironmentVariable (env);
00085
00086 if (ret == NULL)
00087 {
00088 return def;
00089 }
00090 else
00091 {
00092 return ret;
00093 }
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 extern char *
00132 osd_getHomeDir ()
00133 {
00134
00135 return (osd_getEnvironmentVariable ("HOME"));
00136 }
00137
00138 filestatus osd_findOnLarchPath (char *file, char **returnPath)
00139 {
00140 return (osd_getPath (cstring_toCharsSafe (context_getLarchPath ()), file, returnPath));
00141 }
00142
00143 extern filestatus
00144 osd_getPath (char *path, char *file, char **returnPath)
00145 {
00146 char *fullPath;
00147 char *dirPtr;
00148 size_t dirLen;
00149 char aPath[MAXPATHLEN];
00150 filestatus rVal = OSD_FILENOTFOUND;
00151
00152 fullPath = path;
00153
00154 if (fullPath == NULL ||
00155 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
00156
00157
00158 *fullPath == '\0' ||
00159 (*file == CONNECTCHAR || (file[0] != '\0' && file[1] == ':')))
00160 # else
00161 (*file == CONNECTCHAR))
00162 # endif
00163 {
00164
00165
00166 strcpy (&aPath[0], file);
00167
00168 if (osd_fileExists (&aPath[0]))
00169 {
00170 rVal = OSD_FILEFOUND;
00171 *returnPath = dmalloc (strlen (&aPath[0]) + 1);
00172 strcpy (*returnPath, &aPath[0]);
00173 }
00174 }
00175 else
00176 {
00177
00178
00179
00180 while (nextdir (&fullPath, &dirPtr, &dirLen) &&
00181 rVal == OSD_FILENOTFOUND)
00182 {
00183 if ((dirLen + strlen (file) + 2) <= MAXPATHLEN)
00184 {
00185
00186 strncpy (&aPath[0], dirPtr, dirLen);
00187 strcpy (&aPath[0] + dirLen, "");
00188 strcat (&aPath[0], CONNECTSTR);
00189 strcat (&aPath[0], file);
00190
00191 if (osd_fileExists (&aPath[0]))
00192 {
00193 rVal = OSD_FILEFOUND;
00194 *returnPath = (char *) dmalloc (strlen (&aPath[0]) + 1);
00195 strcpy (*returnPath, &aPath[0]);
00196 }
00197 }
00198 else
00199 {
00200 rVal = OSD_PATHTOOLONG;
00201 }
00202 }
00203 }
00204
00205 return rVal;
00206 }
00207
00208 extern filestatus
00209 osd_getExePath (char *path, char *file, char **returnPath)
00210 {
00211 char *fullPath;
00212 char *dirPtr;
00213 size_t dirLen;
00214 char aPath[MAXPATHLEN];
00215 filestatus rVal = OSD_FILENOTFOUND;
00216
00217 fullPath = osd_getEnvironmentVariable (path);
00218
00219 if (fullPath == NULL)
00220 {
00221
00222
00223 strcpy (&aPath[0], file);
00224
00225 if (osd_fileExists (&aPath[0]))
00226 {
00227 rVal = OSD_FILEFOUND;
00228 *returnPath = dmalloc (strlen (&aPath[0]) + 1);
00229 strcpy (*returnPath, &aPath[0]);
00230 }
00231 }
00232 else
00233 {
00234
00235
00236
00237
00238
00239 while (nextdir (&fullPath, &dirPtr, &dirLen) &&
00240 rVal == OSD_FILENOTFOUND)
00241 {
00242 if ((dirLen + strlen (file) + 2) <= MAXPATHLEN)
00243 {
00244
00245 strncpy (&aPath[0], dirPtr, dirLen);
00246 strcpy (&aPath[0] + dirLen, "");
00247 strcat (&aPath[0], CONNECTSTR);
00248 strcat (&aPath[0], file);
00249
00250 if (osd_executableFileExists (&aPath[0]))
00251 {
00252 rVal = OSD_FILEFOUND;
00253 *returnPath = dmalloc (strlen (&aPath[0]) + 1);
00254 strcpy (*returnPath, &aPath[0]);
00255 }
00256 }
00257 else
00258 {
00259 rVal = OSD_PATHTOOLONG;
00260 }
00261 }
00262 }
00263
00264 return rVal;
00265 }
00266
00267 bool
00268 osd_fileExists (char *filespec)
00269 {
00270 # ifdef UNIX
00271 struct stat buf;
00272 return (stat (filespec, &buf) == 0);
00273 # else
00274 # if defined (WIN32) || defined (OS2)
00275 FILE *test = fopen (filespec, "r");
00276 if (test != NULL)
00277 {
00278 (void) fclose (test);
00279 return TRUE;
00280 }
00281 else
00282 {
00283 return FALSE;
00284 }
00285 # else
00286 return FALSE;
00287 # endif
00288 # endif
00289 }
00290
00291 bool
00292 osd_executableFileExists ( char *filespec)
00293 {
00294 # ifdef UNIX
00295 struct stat buf;
00296 # if defined(__IBMC__) && defined(OS2)
00297 # define S_IFMT (unsigned short)0xFFFF
00298 # endif
00299 if (stat (filespec, &buf) == 0)
00300 {
00301
00302
00303 if ((buf.st_mode & S_IFMT) != S_IFDIR )
00304 {
00305
00306 # if defined(__IBMC__) && defined(OS2)
00307 int com_or_exe_pos = strlen( filespec) - 4;
00308 return stricmp( &filespec[com_or_exe_pos], ".exe") == 0
00309 || stricmp( &filespec[com_or_exe_pos], ".com") == 0
00310 || stricmp( &filespec[com_or_exe_pos], ".bat") == 0
00311 || stricmp( &filespec[com_or_exe_pos], ".cmd") == 0;
00312 # else
00313 return (((buf.st_mode & S_IXUSR)
00314 # if !defined(MSDOS) && !defined(OS2)
00315 | (buf.st_mode & S_IXGRP) |
00316 (buf.st_mode & S_IXOTH)
00317 # endif
00318 ) != 0);
00319 # endif
00320 }
00321 }
00322
00323 # endif
00324 return (FALSE);
00325
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368 static bool
00369 nextdir (d_char *current_dir, d_char *dir, size_t *len)
00370 {
00371 char *tchar;
00372
00373 if (**current_dir == '\0')
00374 {
00375 *len = 0;
00376 return FALSE;
00377 }
00378
00379 *dir = (**current_dir == SEPCHAR ? *current_dir + 1 : *current_dir);
00380
00381
00382 for (tchar = *dir; *tchar != '\0' && *tchar != SEPCHAR; tchar++)
00383 {
00384 ;
00385 }
00386
00387 *current_dir = tchar;
00388 *len = size_fromInt (tchar - *dir);
00389 return TRUE;
00390 }
00391
00392 char *osd_getEnvironmentVariable (char *var)
00393 {
00394 # ifdef UNIX
00395 return getenv (var);
00396 # else
00397 # if defined(OS2) || defined(MSDOS)
00398 return getenv (var);
00399 # else
00400 # if defined(WIN32)
00401 return getenv (var);
00402 # else
00403 return NULL;
00404 # endif
00405 # endif
00406 # endif
00407 }
00408
00409 # ifndef NOLCL
00410
00411 # ifdef WIN32
00412 extern int _flushall (void) ;
00413 # endif
00414
00415 # ifndef system
00416 extern int system (const char *) ;
00417 # endif
00418 int osd_system (const char *cmd)
00419 {
00420 int res;
00421
00422 # ifdef WIN32
00423 (void) _flushall ();
00424 # endif
00425
00426 res = system (cmd);
00427 return res;
00428 }
00429 # endif
00430
00431 # ifndef unlink
00432
00433 extern int unlink (const char *) ;
00434
00435 # endif
00436
00437 int osd_unlink (const char *fname)
00438 {
00439 int res;
00440
00441 res = unlink (fname);
00442
00443 if (res != 0)
00444 {
00445 llcontbug (message ("Cannot remove temporary file: %s (%s)",
00446 cstring_fromChars (fname),
00447 cstring_fromChars (strerror (errno))));
00448 }
00449
00450 return res;
00451 }
00452
00453 # if defined (WIN32) || (defined(OS2) && defined(__IBMC__))
00454 # include <process.h>
00455 # elif defined OS2
00456 # include <unistd.h>
00457 # endif
00458
00459 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
00460 int
00461 # else
00462 int
00463 # endif
00464 osd_getPid ()
00465 {
00466 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
00467 int pid = _getpid ();
00468 # else
00469 pid_t pid = getpid ();
00470 # endif
00471
00472 return (int) pid;
00473 }
00474
00475 cstring osd_fixDefine (char *x)
00476 {
00477 # ifdef UNIX
00478 if (strchr (x, '\'') != NULL) {
00479
00480
00481
00482
00483
00484 char *eqs = strchr (x, '=');
00485
00486 if (eqs != NULL) {
00487 if (eqs[1] == '\'') {
00488 char *endqu = strrchr (x, '\'');
00489
00490 if (endqu != NULL) {
00491 if (*(endqu - 1) != '\\') {
00492 if (*(endqu + 1) == '\0') {
00493 cstring res;
00494 cstring def;
00495
00496 *endqu = '\0';
00497 def = cstring_fromChars (eqs + 2);
00498 eqs[1] = '\0';
00499 res = cstring_concat (cstring_fromChars (x), def);
00500 return res;
00501 }
00502 }
00503 }
00504 }
00505 }
00506 }
00507
00508 # endif
00509
00510 return cstring_fromCharsNew (x);
00511 }
00512
00513 bool osd_fileIsReadable (char *f)
00514 {
00515 FILE *fl = fopen (f, "r");
00516
00517 if (fl != (FILE *) 0)
00518 {
00519 check (fclose (fl) == 0);
00520 return (TRUE);
00521 }
00522 else
00523 {
00524 return (FALSE);
00525 }
00526 }
00527
00528 bool osd_isConnectChar (char c)
00529 {
00530 if (c == CONNECTCHAR)
00531 {
00532 return TRUE;
00533 }
00534
00535 # ifdef HASALTCONNECTCHAR
00536 if (c == ALTCONNECTCHAR)
00537 {
00538 return TRUE;
00539 }
00540 # endif
00541
00542 return FALSE;
00543 }
00544