]> git.sur5r.net Git - cc65/blobdiff - src/ar65/library.c
Fix style
[cc65] / src / ar65 / library.c
index 9fdbf9e91239ec7c00930da6142c27894d22fc30..c72e6e3cf3cad9761a9f21c2e433c07248d38bc3 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2012, Ullrich von Bassewitz                                      */
+/* (C) 1998-2013, Ullrich von Bassewitz                                      */
 /*                Roemerstrasse 52                                           */
 /*                D-70794 Filderstadt                                        */
 /* EMail:         uz@cc65.org                                                */
@@ -38,6 +38,7 @@
 #include <errno.h>
 
 /* common */
+#include "cmdline.h"
 #include "exprdefs.h"
 #include "libdefs.h"
 #include "print.h"
 /*****************************************************************************/
 
 
-                        
+
 /* Name of the library file */
 const char*             LibName = 0;
+static char*            NewLibName = 0;
 
 /* File descriptor for the library file */
-FILE*                   NewLib = 0;
 static FILE*            Lib = 0;
+static FILE*            NewLib = 0;
 
 /* The library header */
 static LibHeader        Header = {
@@ -101,7 +103,7 @@ static void ReadHeader (void)
     }
     Header.Flags   = Read16 (Lib);
     Header.IndexOffs = Read32 (Lib);
-}                                                  
+}
 
 
 
@@ -214,9 +216,9 @@ static void WriteIndex (void)
 
 void LibOpen (const char* Name, int MustExist, int NeedTemp)
 /* Open an existing library and a temporary copy. If MustExist is true, the
- * old library is expected to exist. If NeedTemp is true, a temporary library
- * is created.
- */
+** old library is expected to exist. If NeedTemp is true, a temporary library
+** is created.
+*/
 {
     /* Remember the name */
     LibName = xstrdup (Name);
@@ -229,7 +231,9 @@ void LibOpen (const char* Name, int MustExist, int NeedTemp)
         if (MustExist) {
             Error ("Library `%s' does not exist", Name);
         } else {
-            Warning ("Library `%s' not found - will be created", Name);
+            /* Announce the library's creation if ar65 is verbose. */
+            Print (stdout, 1,
+                   "%s: Library `%s' will be created.\n", ProgName, Name);
         }
 
     } else {
@@ -243,10 +247,16 @@ void LibOpen (const char* Name, int MustExist, int NeedTemp)
     }
 
     if (NeedTemp) {
+
+        /* Create the temporary library name */
+        NewLibName = xmalloc (strlen (Name) + strlen (".temp") + 1);
+        strcpy (NewLibName, Name);
+        strcat (NewLibName, ".temp");
+
         /* Create the temporary library */
-        NewLib = tmpfile ();
+        NewLib = fopen (NewLibName, "w+b");
         if (NewLib == 0) {
-            Error ("Cannot create temporary file: %s", strerror (errno));
+            Error ("Cannot create temporary library file: %s", strerror (errno));
         }
 
         /* Write a dummy header to the temp file */
@@ -258,8 +268,8 @@ void LibOpen (const char* Name, int MustExist, int NeedTemp)
 
 unsigned long LibCopyTo (FILE* F, unsigned long Bytes)
 /* Copy data from F to the temp library file, return the start position in
- * the temporary library file.
- */
+** the temporary library file.
+*/
 {
     unsigned char Buf [4096];
 
@@ -301,13 +311,13 @@ void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
 
 static void LibCheckExports (ObjData* O)
 /* Insert all exports from the given object file into the global list
- * checking for duplicates.
- */
+** checking for duplicates.
+*/
 {
     unsigned I;
 
     /* Let the user know what we do */
-    Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, CollCount (&O->Exports));
+    Print (stdout, 2, "Module `%s' (%u exports):\n", O->Name, CollCount (&O->Exports));
 
     /* Insert the exports into the global table */
     for (I = 0; I < CollCount (&O->Exports); ++I) {
@@ -316,7 +326,7 @@ static void LibCheckExports (ObjData* O)
         const char* Name = CollConstAt (&O->Exports, I);
 
         /* Insert the name into the hash table */
-        Print (stdout, 1, "  %s\n", Name);
+        Print (stdout, 2, "  %s\n", Name);
         ExpInsert (Name, O);
     }
 }
@@ -325,8 +335,8 @@ static void LibCheckExports (ObjData* O)
 
 void LibClose (void)
 /* Write remaining data, close both files and copy the temp file to the old
- * filename
- */
+** filename
+*/
 {
     /* Do we have a temporary library? */
     if (NewLib) {
@@ -336,9 +346,9 @@ void LibClose (void)
         size_t Count;
 
         /* Walk through the object file list, inserting exports into the
-         * export list checking for duplicates. Copy any data that is still
-         * in the old library into the new one.
-         */
+        ** export list checking for duplicates. Copy any data that is still
+        ** in the old library into the new one.
+        */
         for (I = 0; I < CollCount (&ObjPool); ++I) {
 
             /* Get a pointer to the object */
@@ -375,7 +385,7 @@ void LibClose (void)
                    LibName, strerror (errno));
         }
 
-        /* Copy the new library to the new one */
+        /* Copy the temporary library to the new one */
         fseek (NewLib, 0, SEEK_SET);
         while ((Count = fread (Buf, 1, sizeof (Buf), NewLib)) != 0) {
             if (fwrite (Buf, 1, Count, Lib) != Count) {
@@ -391,7 +401,7 @@ void LibClose (void)
     if (NewLib && fclose (NewLib) != 0) {
         Error ("Problem closing temporary library file: %s", strerror (errno));
     }
+    if (NewLibName && remove (NewLibName) != 0) {
+        Error ("Problem deleting temporary library file: %s", strerror (errno));
+    }
 }
-
-
-