Applied patch #2762516
[fw/sdcc] / as / asranlib / asranlib.c
index 69876283d61fc67882827adb622ebaf7e1a8a950..52dd6111fd59ced3aeef277ebfbe5ac2faffa967 100644 (file)
@@ -21,6 +21,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #include <string.h>
 #include <assert.h>
 #include <time.h>
+#include <sys/stat.h>
 #include "dbuf_string.h"
 #include "lkar.h"
 
@@ -590,6 +591,8 @@ do_ranlib (const char *archive)
       int nsym;
       int symtab_size;
       char tmpfile[] = "arXXXXXX";
+      struct stat stat_buf;
+      int can_stat;
 
 #ifdef _WIN32
       if (NULL == _mktemp (tmpfile) || NULL == (outfp = fopen (tmpfile, "wb")))
@@ -646,7 +649,6 @@ do_ranlib (const char *archive)
           fwrite (buf, 1, sizeof (buf), outfp);
         }
 
-
       for (symp = symlist; symp; symp = symp->next)
         {
           fputs (symp->name, outfp);
@@ -662,6 +664,17 @@ do_ranlib (const char *archive)
         putc (pad, outfp);
 
       fclose (outfp);
+
+      if (0 != fstat(fileno(infp), &stat_buf))
+        {
+          fprintf (stderr, "asranlib: can't stat %s: ", archive);
+          perror (NULL);
+          fclose (infp);
+          can_stat = 0;
+        }
+      else
+        can_stat = 1;
+
       fclose (infp);
 
       if (0 != remove (archive))
@@ -674,6 +687,11 @@ do_ranlib (const char *archive)
           fprintf (stderr, "asranlib: can't rename %s to %s: ", tmpfile, archive);
           perror (NULL);
         }
+      else if (!can_stat || 0 != chmod (archive, stat_buf.st_mode))
+        {
+          fprintf (stderr, "asranlib: can't chmod %s: ", archive);
+          perror (NULL);
+        }
     }
   else
     fclose (infp);