* as/hc08/asmain.c (asexit),
[fw/sdcc] / support / librarian / sdcclib.c
index 2993c32f1bde69b4448206066607794473ff0848..80aedc10ef40d8142aa1fe4391d9462d98072399 100644 (file)
@@ -15,11 +15,14 @@ You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
 
+#define _POSIX_
+#include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#define _POSIX_
-#include <limits.h>
+#if !defined(__BORLANDC__) && !defined(_MSC_VER)
+#include <unistd.h>
+#endif
 
 char ProgName[PATH_MAX];
 char LibName[PATH_MAX];
@@ -41,7 +44,7 @@ char AdbName[PATH_MAX];
 #define NEQ(A,B) strcmp((A),(B))
 
 int action=0;
-FILE *lib, *newlib, *rel, *adb, *index;
+FILE *lib, *newlib, *rel, *adb, *libindex;
 char FLine[MAXLINE+1];
 char ModName[MAXLINE+1];
 int state=0;
@@ -58,7 +61,7 @@ void GetNameFromPath(char * path, char * name)
 
 void ChangeExtension(char * path, char * ext)
 {
-       int i, j;
+       int i;
 
        for(i=0; path[i]!=0; i++);
        for(; (path[i]!='.')&&(path[i]!='\\')&&(path[i]!='/')&&(i>=0); i--);
@@ -86,7 +89,6 @@ void CleanLine(char * buff)
 
 int set_options (char * opt)
 {
-       char temp[255];
        int rvalue=0, unknown=0;
        static char Help[] =
        "Usage: %s [-options] library relfile\n\n"
@@ -186,6 +188,7 @@ void AddRel(void)
     long newlibpos, indexsize;
     char symname[MAXLINE+1];
     char c;
+    int IsDOSStyle=0;
 
     strcpy(LibNameTmp, LibName);
     ChangeExtension(LibNameTmp, "__L");
@@ -204,7 +207,7 @@ void AddRel(void)
         if(rel==NULL)
         {
             printf("ERROR: Couldn't open file '%s'", RelName);
-            fclose(lib);
+            if(lib!=NULL) fclose(lib);
             return;
         }
     }
@@ -214,17 +217,17 @@ void AddRel(void)
     if(newlib==NULL)
     {
         printf("ERROR: Couldn't create temporary file '%s'", LibNameTmp);
-        fclose(lib);
+        if(lib!=NULL) fclose(lib);
         fclose(rel);
         return;
     }
     fprintf(newlib, "<FILES>\n\n");
 
-    index=fopen(IndexName, "w");
-    if(index==NULL)
+    libindex=fopen(IndexName, "w");
+    if(libindex==NULL)
     {
         printf("ERROR: Couldn't create temporary file '%s'", IndexName);
-        fclose(lib);
+        if(lib!=NULL) fclose(lib);
         fclose(newlib);
         fclose(rel);
         return;
@@ -248,7 +251,7 @@ void AddRel(void)
                     {
                         newlibpos=ftell(newlib);
                         fprintf(newlib, "<FILE>\n%s\n", FLine);
-                        fprintf(index, "<MODULE>\n%s %ld\n", FLine, newlibpos);
+                        fprintf(libindex, "<MODULE>\n%s %ld\n", FLine, newlibpos);
                         state++;
                     }
                 }                
@@ -258,7 +261,7 @@ void AddRel(void)
                 if(EQ(FLine, "</FILE>"))
                 {
                     fprintf(newlib, "\n");
-                    fprintf(index, "</MODULE>\n\n");
+                    fprintf(libindex, "</MODULE>\n\n");
                     state=0;
                     inrel=0;
                 }
@@ -269,7 +272,7 @@ void AddRel(void)
                     if(FLine[0]=='S')
                     {
                         sscanf(FLine, "S %s %c", symname, &c);
-                        if(c=='D') fprintf(index, "%s\n", symname);
+                        if(c=='D') fprintf(libindex, "%s\n", symname);
                     }
                 }
             break;
@@ -280,7 +283,7 @@ void AddRel(void)
     {
         newlibpos=ftell(newlib);
         fprintf(newlib, "<FILE>\n%s\n<REL>\n", ModName);
-        fprintf(index, "<MODULE>\n%s %ld\n", ModName, newlibpos);
+        fprintf(libindex, "<MODULE>\n%s %ld\n", ModName, newlibpos);
         while(!feof(rel))
         {
             FLine[0]=0;
@@ -293,11 +296,11 @@ void AddRel(void)
             if(FLine[0]=='S')
             {
                 sscanf(FLine, "S %s %c", symname, &c);
-                if(c=='D') fprintf(index, "%s\n", symname);
+                if(c=='D') fprintf(libindex, "%s\n", symname);
             }
         }
         fclose(rel);
-        fprintf(index, "</MODULE>\n");
+        fprintf(libindex, "</MODULE>\n");
         fprintf(newlib, "</REL>\n<ADB>\n");
     
         adb=fopen(AdbName, "r");
@@ -319,27 +322,34 @@ void AddRel(void)
     }
 
     /*Put the temporary files together as a new library file*/
-    indexsize=ftell(index);
-    fflush(index);
+    indexsize=ftell(libindex);
+    fflush(libindex);
     fflush(newlib);
     fclose(newlib);
     if(lib!=NULL) fclose(lib);
-    fclose(index);
+    fclose(libindex);
 
     newlib=fopen(LibNameTmp, "r");
     lib=fopen(LibName, "w");
-    index=fopen(IndexName, "r");
+    libindex=fopen(IndexName, "r");
+
+    fprintf(lib, "<SDCCLIB>\n\n<INDEX>\n");
+
+    /*Find out if the \n is expanded to \r\n or not*/
+    if(ftell(lib)!=(long)strlen("<SDCCLIB>\n\n<INDEX>\n"))
+    {
+        IsDOSStyle=1;
+    }
 
-    fprintf(lib, "<SDCCLIB>\n\n");
-    fprintf(lib, "<INDEX>\n");
+    indexsize+=ftell(lib)+strlen("0123456789\n\n</INDEX>\n\n");
+    if(IsDOSStyle) indexsize+=4;
 
-    indexsize+=ftell(lib)+12+14;
     fprintf(lib, "%10ld\n", indexsize);
 
-    while(!feof(index))
+    while(!feof(libindex))
     {
         FLine[0]=0;
-        fgets(FLine, MAXLINE, index);
+        fgets(FLine, MAXLINE, libindex);
         fprintf(lib, "%s", FLine);
     }
     fprintf(lib, "\n</INDEX>\n\n");
@@ -355,10 +365,10 @@ void AddRel(void)
 
     fclose(newlib);
     fclose(lib);
-    fclose(index);
+    fclose(libindex);
 
-    unlink(LibNameTmp);
-    unlink(IndexName);
+    remove(LibNameTmp);
+    remove(IndexName);
 }
 
 void ExtractRel(void)