2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
27 ** Provide a system-independent interface to system-dependent
32 * Modified by Herbert 04/19/97:
33 * - added conditional 'OS2' to conditional 'MSDOS'
34 * - added include of new header portab.h.
35 * - changed '/' to macro.
36 * - added DOS / OS/2 specific stuff in osd_getPath.
38 * - added OS/2 specific includes before osd_getPid()
39 * - handle files like in WIN32 for OS/2 in osd_fileExists()
44 # include <sys/types.h>
45 # include <sys/stat.h>
46 /* Fix suggested by Lars Rasmussen */
50 # include "lclintMacros.nf"
57 extern int stat (const char *, /*@out@*/ struct stat *);
60 static bool osd_executableFileExists (char *);
62 static bool nextdir (char **p_current_dir, /*@out@*/ char **p_dir,
63 /*@out@*/ size_t *p_len);
65 extern cstring LSLRootName (cstring filespec)
68 char *result, *startName, *tail;
71 llassert (cstring_isDefined (filespec));
72 tail = strrchr (filespec, CONNECTCHAR);
73 startName = (tail == NULL ? filespec : &tail[1]);
74 tail = strrchr (startName, '.');
75 nameLength = (tail == NULL ? strlen (startName)
76 : size_fromInt (tail - startName));
77 result = dmalloc (nameLength + 1);
78 strncpy (result, startName, nameLength);
79 result[(int) nameLength] = '\0';
81 /*@noaccess cstring@*/
84 extern /*@observer@*/ cstring
85 osd_getEnvironment (cstring env, /*@returned@*/ cstring def)
88 char *ret = osd_getEnvironmentVariable (env);
98 /*@noaccess cstring@*/
104 ** FUNCTIONAL DESCRIPTION:
106 ** This function attempts to locate a file in a search list. On VMS, it
107 ** just concatinates the path and file names, and then lets RMS do the
108 ** searching. On Ultrix, it searches for the file on the path.
110 ** FORMAL PARAMETERS:
112 ** path: search path where to look for the file.
113 ** file: name of file to search for.
114 ** returnPath: if a file is found, this is where the concatenated
115 ** directory and file name are returned.
119 ** OSD_FILEFOUND: the file was found on the search list.
120 ** OSD_FILENOTFOUND the file was not found.
121 ** OSD_PATHTOOLONG the concatenated directory and file name are too
130 ** Requires that parameters, path and file, are valid C strings.
136 extern /*@observer@*/ cstring osd_getHomeDir ()
138 /* Would something different be better for windows? */
139 return (osd_getEnvironmentVariable (cstring_makeLiteralTemp ("HOME")));
142 filestatus osd_findOnLarchPath (cstring file, cstring *returnPath)
144 return (osd_getPath (context_getLarchPath (), file, returnPath));
148 osd_getPath (cstring path, cstring file, cstring *returnPath)
153 char aPath[MAXPATHLEN];
154 filestatus rVal = OSD_FILENOTFOUND; /* assume file not found. */
159 llassert (cstring_isDefined (file));
163 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
164 /* under OS/2 and MSDOS the includePath may be empty, if so, search
165 * the current directory. */
167 (*file == CONNECTCHAR || (file[0] != '\0' && file[1] == ':'))
169 (*file == CONNECTCHAR)
173 /* No path specified. Look for it in the current directory. */
175 strcpy (&aPath[0], file);
177 if (osd_fileExists (&aPath[0]))
179 rVal = OSD_FILEFOUND;
180 *returnPath = dmalloc (strlen (&aPath[0]) + 1);
181 strcpy (*returnPath, &aPath[0]);
186 /* Path specified. Loop through directories in path looking for the */
187 /* first occurrence of the file. */
189 while (nextdir (&fullPath, &dirPtr, &dirLen) &&
190 rVal == OSD_FILENOTFOUND)
192 if ((dirLen + strlen (file) + 2) <= MAXPATHLEN)
194 /* Cat directory and filename, and see if file exists. */
195 strncpy (&aPath[0], dirPtr, dirLen);
196 strcpy (&aPath[0] + dirLen, ""); /* Null terminate aPath. */
197 strcat (&aPath[0], CONNECTSTR);
198 strcat (&aPath[0], file);
200 if (osd_fileExists (&aPath[0]))
202 rVal = OSD_FILEFOUND;
203 *returnPath = (char *) dmalloc (strlen (&aPath[0]) + 1);
204 strcpy (*returnPath, &aPath[0]);
209 rVal = OSD_PATHTOOLONG;
215 /*@noaccess cstring@*/
219 osd_getExePath (cstring path, cstring file, cstring *returnPath)
224 char aPath[MAXPATHLEN];
225 filestatus rVal = OSD_FILENOTFOUND; /* assume file not found. */
228 fullPath = osd_getEnvironmentVariable (path);
230 if (fullPath == NULL)
232 /* No path specified. Look for it in the current directory. */
233 llassert (cstring_isDefined (file));
234 strcpy (&aPath[0], file);
236 if (osd_fileExists (&aPath[0]))
238 rVal = OSD_FILEFOUND;
239 *returnPath = dmalloc (strlen (&aPath[0]) + 1);
240 strcpy (*returnPath, &aPath[0]);
246 ** Path specified. Loop through directories in path looking
247 ** for the first occurrence of the file.
250 while (nextdir (&fullPath, &dirPtr, &dirLen) &&
251 rVal == OSD_FILENOTFOUND)
253 llassert (cstring_isDefined (file));
255 if ((dirLen + cstring_length (file) + 2) <= MAXPATHLEN)
257 /* Cat directory and filename, and see if file exists. */
258 strncpy (&aPath[0], dirPtr, dirLen);
259 strcpy (&aPath[0] + dirLen, ""); /* Null terminate aPath. */
260 strcat (&aPath[0], CONNECTSTR);
261 strcat (&aPath[0], file);
263 if (osd_executableFileExists (&aPath[0]))
265 rVal = OSD_FILEFOUND;
266 *returnPath = dmalloc (strlen (&aPath[0]) + 1);
267 strcpy (*returnPath, &aPath[0]);
272 rVal = OSD_PATHTOOLONG;
278 /*@noaccess cstring@*/
282 osd_fileExists (cstring filespec)
286 return (stat (cstring_toCharsSafe (filespec), &buf) == 0);
288 # if defined (WIN32) || defined (OS2)
289 FILE *test = fopen (cstring_toCharsSafe (filespec), "r");
293 (void) fclose (test);
307 osd_executableFileExists (/*@unused@*/ char *filespec)
311 # if defined(__IBMC__) && defined(OS2)
312 # define S_IFMT (unsigned short)0xFFFF
314 if (stat (filespec, &buf) == 0)
316 /* mask by file type */
317 /*@-unrecog@*/ /* S_IFMT is not defined */
318 if ((buf.st_mode & S_IFMT) != S_IFDIR /*@=unrecog@*/) /* not a directory */
320 /* as long as it is an executable file */
321 # if defined(__IBMC__) && defined(OS2)
322 int com_or_exe_pos = strlen( filespec) - 4;
323 return stricmp( &filespec[com_or_exe_pos], ".exe") == 0
324 || stricmp( &filespec[com_or_exe_pos], ".com") == 0
325 || stricmp( &filespec[com_or_exe_pos], ".bat") == 0
326 || stricmp( &filespec[com_or_exe_pos], ".cmd") == 0;
328 return (((buf.st_mode & S_IXUSR)
329 # if !defined(MSDOS) && !defined(OS2)
330 | (buf.st_mode & S_IXGRP) |
331 (buf.st_mode & S_IXOTH)
345 ** FUNCTIONAL DESCRIPTION:
347 ** Find the next directory from a directory path.
349 ** FORMAL PARAMETERS:
351 ** char ** current_dir :
352 ** Points to the current position in the path string. The first time
353 ** you call this routine, this should point to the first character of
354 ** the path. On return, this will be updated to point to the
355 ** terminating \0 or : of the first directory found. You can then pass
356 ** it unchanged for subsequent calls; this routine will correctly skip
360 ** On exit, this will point to the first character of the directory
361 ** that was found. This will be a pointer directly into the client's
364 ** unsigned int * len :
365 ** On exit, this will contain the length of the directory that was
366 ** found, not counting any terminating \0 or :. If no directory was
367 ** found, this will be 0.
370 ** TRUE if we found another directory.
375 ** We return a pointer and length, rather than a string, because of a)
376 ** historical reasons; and b) this avoids having to allocate storage.
384 nextdir (d_char *current_dir, d_char *dir, size_t *len)
388 if (**current_dir == '\0')
394 *dir = (**current_dir == PATH_SEPARATOR ? *current_dir + 1 : *current_dir);
396 /* Find next ':' or end of string */
397 for (tchar = *dir; *tchar != '\0' && *tchar != PATH_SEPARATOR; tchar++)
402 *current_dir = tchar;
403 *len = size_fromInt (tchar - *dir);
407 /*@observer@*/ /*@null@*/ cstring osd_getEnvironmentVariable (cstring var)
409 # if defined(UNIX) || defined(OS) || defined(MSDOS) || defined(WIN32)
410 char *val = getenv (cstring_toCharsSafe (var));
414 return cstring_undefined;
418 return cstring_makeLiteralTemp (val);
421 return cstring_undefined;
428 extern /*@external@*/ int _flushall (void) /*@modifies fileSystem@*/ ;
432 extern /*@external@*/ int system (const char *) /*@modifies fileSystem@*/ ;
434 int osd_system (cstring cmd)
437 /* system ("printenv"); */
442 res = system (cstring_toCharsSafe (cmd));
449 extern /*@external@*/ int unlink (const char *) /*@modifies fileSystem@*/ ;
453 int osd_unlink (cstring fname)
457 res = unlink (cstring_toCharsSafe (fname));
461 llcontbug (message ("Cannot remove temporary file: %s (%s)",
463 cstring_fromChars (strerror (errno))));
469 # if defined (WIN32) || (defined(OS2) && defined(__IBMC__))
470 # include <process.h>
475 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
482 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
483 int pid = _getpid ();
485 pid_t pid = getpid ();
491 cstring osd_fixDefine (cstring x)
494 llassert (cstring_isDefined (x));
496 if (strchr (x, '\'') != NULL) {
498 ** If there is a single quote, check for <ident>='<string>' and
499 ** produce <ident>=<string>
502 char *eqs = strchr (x, '=');
505 if (eqs[1] == '\'') {
506 char *endqu = strrchr (x, '\'');
509 if (*(endqu - 1) != '\\') {
510 if (*(endqu + 1) == '\0') {
515 def = cstring_fromChars (eqs + 2);
517 res = cstring_concat (cstring_fromChars (x), def);
528 return cstring_copy (x);
529 /*@noaccess cstring@*/
532 bool osd_fileIsReadable (cstring f)
534 FILE *fl = fopen (cstring_toCharsSafe (f), "r");
536 if (fl != (FILE *) 0)
538 check (fclose (fl) == 0);
547 bool osd_isConnectChar (char c)
549 if (c == CONNECTCHAR)
554 # ifdef HASALTCONNECTCHAR
555 if (c == ALTCONNECTCHAR)