]> andersk Git - splint.git/blobdiff - src/osd.c
Fixed problem with NULL being changed.
[splint.git] / src / osd.c
index 16f020dd881cfc357d98e0d551c4358185badea9..171d8777c364e0017d9c411aa0a74e72d9cf3339 100644 (file)
--- a/src/osd.c
+++ b/src/osd.c
@@ -1,6 +1,6 @@
 /*
 ** Splint - annotation-assisted static program checker
-** Copyright (C) 1994-2002 University of Virginia,
+** Copyright (C) 1994-2003 University of Virginia,
 **         Massachusetts Institute of Technology
 **
 ** This program is free software; you can redistribute it and/or modify it
@@ -62,7 +62,6 @@
 # include "splintMacros.nf"
 # include "basic.h"
 # include "osd.h"
-# include "portab.h"
 
 /* from stat.h */
 /*@ignore@*/
@@ -184,7 +183,7 @@ osd_getPath (cstring path, cstring file, cstring *returnPath)
       *fullPath == '\0' || 
       (*file == CONNECTCHAR || (file[0] != '\0' && file[1] == ':'))
 # else
-    (*file == CONNECTCHAR)
+      (*file == CONNECTCHAR)
 # endif
       )
     {
@@ -204,8 +203,8 @@ osd_getPath (cstring path, cstring file, cstring *returnPath)
       /* Path specified. Loop through directories in path looking for the */
       /* first occurrence of the file.                             */
       
-      while (nextdir (&fullPath, &dirPtr, &dirLen) &&
-            rVal == OSD_FILENOTFOUND)
+      while (nextdir (&fullPath, &dirPtr, &dirLen) 
+            && rVal == OSD_FILENOTFOUND)
        {
          if ((dirLen + strlen (file) + 2) <= MAXPATHLEN)
            {
@@ -302,10 +301,10 @@ osd_fileExists (cstring filespec)
 {
 # ifdef UNIX
   struct stat buf;
-  return (stat (cstring_toCharsSafe (filespec), &buf) == 0);
+  /*@i3@*/ return (stat (cstring_toCharsSafe (filespec), &buf) == 0); /* spurious */
 # else
 # if defined (WIN32) || defined (OS2)
-  FILE *test = fileTable_openFile (context_fileTable (), filespec, "r");
+  FILE *test = fileTable_openReadFile (context_fileTable (), filespec);
   
   if (test != NULL) 
     {
@@ -345,7 +344,7 @@ osd_executableFileExists (/*@unused@*/ char *filespec)
     { 
       /* mask by file type */
       /*@-unrecog@*/ /* S_IFMT is not defined */
-      if ((buf.st_mode & S_IFMT) != S_IFDIR /*@=unrecog@*/) /* not a directory */
+      /*@i32@*/ if ((buf.st_mode & S_IFMT) != S_IFDIR /*@=unrecog@*/) /* not a directory */ /* spurious */
        {
          /* as long as it is an executable file */
 # if defined(__IBMC__) && defined(OS2)
@@ -360,12 +359,12 @@ osd_executableFileExists (/*@unused@*/ char *filespec)
                   | (buf.st_mode & S_IXGRP) |
                   (buf.st_mode & S_IXOTH)
 # endif
-                  ) != 0);
+                  /*@i4@*/ ) != 0); /* spurious */
 # endif
        }
     }
 # endif
-  return (FALSE);
+  /*@i4@*/ return (FALSE); /* spurious */
 
 }
 
@@ -453,9 +452,7 @@ nextdir (d_char *current_dir, d_char *dir, size_t *len)
 # endif
 }
 
-# ifndef NOLCL
-
-# ifdef WIN32
+# if defined (WIN32) || defined (OS2) && defined (__IBMC__)
 extern /*@external@*/ int _flushall (void) /*@modifies fileSystem@*/ ;
 # endif
 
@@ -466,14 +463,13 @@ int osd_system (cstring cmd)
 {
   int res;
     /* system ("printenv"); */
-# ifdef WIN32
+# if defined (WIN32) || defined (OS2) && defined (__IBMC__)
   (void) _flushall (); 
 # endif
 
   res = system (cstring_toCharsSafe (cmd));
   return res;
 }
-# endif
 
 # ifndef unlink
 /* This should be defined by unistd.h */
@@ -482,7 +478,7 @@ extern /*@external@*/ int unlink (const char *) /*@modifies fileSystem@*/ ;
 /*@=redecl@*/
 # endif
 
-static s_tempError = FALSE;
+static bool s_tempError = FALSE;
 
 void osd_setTempError (void)
 {
@@ -518,7 +514,7 @@ osd_getPid ()
 # if defined (WIN32) || defined (OS2) && defined (__IBMC__)
   int pid = _getpid ();
 # else
-  pid_t pid = getpid ();
+  __pid_t pid = getpid ();
 # endif
 
   return (int) pid;
@@ -567,7 +563,7 @@ cstring osd_fixDefine (cstring x)
 
 bool osd_fileIsReadable (cstring f)
 {
-  FILE *fl = fileTable_openFile (context_fileTable (), f, "r");
+  FILE *fl = fileTable_openReadFile (context_fileTable (), f);
 
   if (fl != NULL)
     {
@@ -871,6 +867,7 @@ static void osd_setWorkingDirectory (void)
     {
       lldiagmsg (message ("Cannot get working directory: %s\n",
                          lldecodeerror (errno)));
+      osd_cwd = cstring_makeLiteral ("<missing directory>");
     }
   else
     {
@@ -888,6 +885,12 @@ void osd_initMod (void)
   osd_setWorkingDirectory ();
 }
 
+void osd_destroyMod (void)
+{
+  cstring_free (osd_cwd);
+  osd_cwd = cstring_undefined;
+}
+
 cstring osd_absolutePath (cstring cwd, cstring filename)
 {
 # if defined (UNIX) || defined (OS2)
@@ -900,7 +903,7 @@ cstring osd_absolutePath (cstring cwd, cstring filename)
   llassert (cstring_isDefined (cwd2));
   llassert (cstring_isDefined (filename));
 
-  abs_buffer = (char *) dmalloc (size_fromInt (cstring_length (cwd2) + cstring_length (filename) + 2));
+  abs_buffer = (char *) dmalloc (cstring_length (cwd2) + cstring_length (filename) + 2);
   endp = abs_buffer;
   
   /*
@@ -1041,13 +1044,20 @@ cstring osd_outputPath (cstring filename)
   int unmatched_slash_count = 0;
   size_t filename_len = cstring_length (filename);
   
-  llassertretval (filename_len > 0, /*@-type@*/ filename /*@=type@*/);
+  llassertretval (filename_len > 0, filename);
 
   /*@access cstring@*/
   path_p = filename;
-  rel_buf_p = rel_buffer = (char *) dmalloc (filename_len);
+  rel_buffer = (char *) dmalloc (filename_len);
+  rel_buf_p = rel_buffer;
   *rel_buf_p = '\0';
 
+  if (cwd_p == NULL) 
+    {
+      /* Need to prevent recursive assertion failures */
+      return cstring_copy (filename);
+    }
+
   llassert (cwd_p != NULL);
   llassert (path_p != NULL);
 
@@ -1060,25 +1070,34 @@ cstring osd_outputPath (cstring filename)
   if ((*cwd_p == '\0') && (*path_p == '\0' || osd_isConnectChar (*path_p)))  /* whole pwd matched */
     {
       if (*path_p == '\0')             /* input *is* the current path! */
-       return cstring_makeLiteral (".");
+       {
+         cstring_free (rel_buffer);
+         return cstring_makeLiteral (".");
+       }
       else
        {
-         /*@i324 ! splint didn't report an errors for: return ++path_p; */
-         return cstring_fromCharsNew (++path_p);
+         cstring_free (rel_buffer);
+         return cstring_fromCharsNew (path_p + 1);
        }
     }
   else
     {
+
+      /* drl   2002-10/14 I had to put this code back*/
+      /* the case that needs it is when splint is given an absolute path name of a file outside of the current directory and the subdirectories below the current directory. e.g. cd /home/; splint /tmp/prog.c
+       */
+      
       /* evans 2002-02-05 This is horrible code, which I've removed.  I couldn't find any
       ** test cases that need it, so I hope I'm not breaking anything.
       */
+      /*#if 0*/
 
-# if 0      
       if (*path_p != '\0')
         {
           --cwd_p;
           --path_p;
-          while (!osd_isConnectChar (*cwd_p))         /* backup to last slash */
+
+          while (cwd_p >= osd_cwd && !osd_isConnectChar (*cwd_p)) /* backup to last slash */
             {
               --cwd_p;
               --path_p;
@@ -1099,16 +1118,28 @@ cstring osd_outputPath (cstring filename)
          Reject it if longer than the input.  */
       if (unmatched_slash_count * 3 + strlen (path_p) >= filename_len)
        {
+         cstring_free (rel_buffer);
+         /* fprintf (stderr, "Returning filename: %s [%p]\n", filename); */
          return cstring_copy (filename);
        }
-# endif
-      
+
+      /*drl 10-14-2002 end previously removed code */
+      /*#endif*/
       /* For each of them, put a `../' at the beginning of the short name.  */
       while (unmatched_slash_count-- > 0)
         {
           /* Give up if the result gets to be longer
              than the absolute path name.  */
-          if (rel_buffer + filename_len <= rel_buf_p + 3)
+         char * temp_rel_buf_p;
+
+         /*drl This comment is necessary because for some reason Splint
+           does not realize that the pasts where rel_buf_p is released
+           do not reach here*/
+         /*@-usereleased@*/
+         temp_rel_buf_p = rel_buf_p;
+         /*@-usereleased@*/
+         
+          if (rel_buffer + filename_len <= temp_rel_buf_p + 3)
            {
              sfree (rel_buffer);
              return cstring_copy (filename);
@@ -1120,6 +1151,7 @@ cstring osd_outputPath (cstring filename)
         }
       
       /* Then tack on the unmatched part of the desired file's name.  */
+
       do
         {
           if (rel_buffer + filename_len <= rel_buf_p)
@@ -1130,12 +1162,14 @@ cstring osd_outputPath (cstring filename)
         } /*@-usereleased@*/
       while ((*rel_buf_p++ = *path_p++) != '\0') ;
 
-      /*@=usereleased@*/ /*@i523! shouldn't need these */
+      
+      /*@=usereleased@*/ /* Splint limitation: shouldn't need these */
       --rel_buf_p;
 
       if (osd_isConnectChar (*(rel_buf_p-1)))
         *--rel_buf_p = '\0';
 
+      /* fprintf (stderr, "Returning buffer: %s [%p]\n", rel_buffer, rel_buffer); */
       return rel_buffer;
     }
   /*@noaccess cstring@*/
This page took 0.049001 seconds and 4 git commands to generate.