Fixed gcc warning
[fw/sdcc] / link / z80 / lklibr.c
index 47f499f3c09abe79a3370b6548c69366682636c5..be82f7bca79900a644ea727df81020b37e680590 100644 (file)
 #include <stdlib.h>
 #include "aslink.h"
 
+#ifdef OTHERSYSTEM
+#ifdef SDK
+#ifdef UNIX
+    #define LKDIRSEP '/'
+    #define LKDIRSEPSTR "/"
+#else /* UNIX */
+       #define LKDIRSEP '\\'
+       #define LKDIRSEPSTR "\\"
+#endif /* UNIX */
+#else /* SDK */
+       #define LKDIRSEP '\\'
+       #define LKDIRSEPSTR "\\"
+#endif /* SDK */
+#endif
+
 #ifdef __CYGWIN__
 void ToCygWin(char * filspc)
 {
@@ -140,6 +155,7 @@ addpath()
  *     global variables:
  *             lbpath  *lbphead        The pointer to the first
  *                                     path structure
+ *      ip a pointer to the library name
  *
  *      functions called:
  *             VOID    addfile()       lklibr.c
@@ -155,19 +171,28 @@ VOID
 addlib()
 {
        struct lbpath *lbph;
+    int foundcount=0;
 
        unget(getnb());
 
-       if (lbphead == NULL) {
-               addfile(NULL,ip);
-               return;
-       }       
-       for (lbph=lbphead; lbph; lbph=lbph->next) {
-               addfile(lbph->path,ip);
+       if (lbphead == NULL)
+    {
+               foundcount=addfile(NULL, ip);
        }
+    else
+    {
+           for (lbph=lbphead; lbph; lbph=lbph->next)
+        {
+                   foundcount+=addfile(lbph->path, ip);
+           }
+    }
+    if(foundcount == 0)
+    {
+        printf("\n?ASlink-Warning-Couldn't find library '%s'", ip);
+    }
 }
 
-/*)Function    VOID    addfile(path,libfil)
+/*)Function    int     addfile(path,libfil)
  *
  *             char    *path           library path specification
  *             char    *libfil         library file specification
@@ -199,72 +224,108 @@ addlib()
  *
  *     side effects:
  *             An lbname structure may be created.
+ *
+ *  return:
+ *      1: the library was found
+ *      0: the library was not found
  */
 
-VOID
-addfile(path,libfil)
-char *path;
-char *libfil;
+int addfile(char * path, char * libfil)
 {
        FILE *fp;
        char *str;
        struct lbname *lbnh, *lbn;
+    int libfilinc=0;
 
-       if ((path != NULL) && (strchr(libfil,':') == NULL)){
+       if (path != NULL)
+    {
                str = (char *) new (strlen(path) + strlen(libfil) + 6);
-               strcpy(str,path);
-#ifdef OTHERSYSTEM
-#ifdef SDK
-#ifdef UNIX
-               if (str[strlen(str)-1] != '/') {
-                       strcat(str,"/");
-#else /* UNIX */
-               if (str[strlen(str)-1] != '\\') {
-                       strcat(str,"\\");
-#endif /* UNIX */
-#else /* SDK */
-               if (str[strlen(str)-1] != '\\') {
-                       strcat(str,"\\");
-#endif /* SDK */
+               strcpy(str, path);
+
+        if (str[strlen(str)-1] != LKDIRSEP)
+        {
+                       strcat(str, LKDIRSEPSTR);
                }
-#endif
-       } else {
+       }
+    else
+    {
                str = (char *) new (strlen(libfil) + 5);
        }
-#ifdef OTHERSYSTEM
-#ifdef SDK
-#ifdef UNIX
-       if (libfil[0] == '/') { libfil++; }
-#else /* UNIX */
-       if (libfil[0] == '\\') { libfil++; }
-#endif /* UNIX */
-#else /* SDK */
-       if (libfil[0] == '\\') { libfil++; }
-#endif /* SDK */
-#endif
-       strcat(str,libfil);
-       if(strchr(libfil,FSEPX) == NULL) {
+
+       if (libfil[0] == LKDIRSEP)
+    {
+        libfil++;
+        libfilinc=1;
+    }
+       
+    strcat(str, libfil);
+
+       if(strchr(libfil, FSEPX) == NULL)
+    {
                sprintf(&str[strlen(str)], "%clib", FSEPX);
        }
-       if ((fp = fopen(str, "r")) != NULL) {
+
+    fp=fopen(str, "r");
+    if(fp == NULL)
+    {
+        /*Ok, that didn't work.  Try with the 'libfil' name only*/
+        if(libfilinc) libfil--;
+        fp=fopen(libfil, "r");
+        if(fp != NULL) 
+        {
+            /*Bingo!  'libfil' is the absolute path of the library*/
+            strcpy(str, libfil);
+            path=NULL;/*This way 'libfil' and 'path' will be rebuilt from 'str'*/
+        }
+    }
+
+    if(path==NULL)
+    {
+        /*'path' can not be null since it is needed to find the '.o' files associated with
+        the library.  So, get 'path' from 'str' and then chop it off and recreate 'libfil'.
+        That way putting 'path' and 'libfil' together will result into the original filepath
+        as contained in 'str'.*/
+        int j;
+        path = (char *) new (strlen(str));
+        strcpy(path, str);
+        for(j=strlen(path)-1; j>=0; j--)
+        {
+            if((path[j]=='\\')||(path[j]=='/'))
+            {
+                strcpy(libfil, &path[j+1]);
+                path[j+1]=0;
+                break;
+            }
+        }
+        if(j<=0) path[0]=0;
+    }
+
+       if (fp != NULL)
+    {
                fclose(fp);
                lbnh = (struct lbname *) new (sizeof(struct lbname));
-               if (lbnhead == NULL) {
+               if (lbnhead == NULL)
+        {
                        lbnhead = lbnh;
-               } else {
+               }
+        else
+        {
                        lbn = lbnhead;
                        while (lbn->next)
                                lbn = lbn->next;
                        lbn->next = lbnh;
                }
-               if ((path != NULL) && (strchr(libfil,':') == NULL)){
-                       lbnh->path = path;
-               }
+
+               lbnh->path = path;
                lbnh->libfil = (char *) new (strlen(libfil) + 1);
-               strcpy(lbnh->libfil,libfil);
+               strcpy(lbnh->libfil, libfil);
                lbnh->libspc = str;
-       } else {
+        return 1;
+       }
+    else
+    {
                free(str);
+        return 0;
        }
 }
 
@@ -457,26 +518,36 @@ int fndsym( char *name )
        pmlibraryfile ThisLibr;
        pmlibrarysymbol ThisSym = NULL;
 
+    pmlibraryfile FirstFound;
+    int numfound=0;
+
        /* Build the index if this is the first call to fndsym */
        if (libr.next==NULL)
                buildlibraryindex();
        
        /* Iterate through all library object files */
        ThisLibr = libr.next;
-       while (ThisLibr) {
-
+    FirstFound = libr.next; /*So gcc stops whining*/
+       while (ThisLibr)
+    {
                /* Iterate through all symbols in an object file */
                ThisSym = ThisLibr->symbols->next;
 
-               while (ThisSym) {
+               while (ThisSym)
+        {
             //printf("ThisSym->name=%s\n", ThisSym->name);
-                       if (!strcmp(ThisSym->name, name)) {
-                               if (!ThisLibr->loaded) {
+                       if (!strcmp(ThisSym->name, name))
+            {
+                               if ((!ThisLibr->loaded) && (numfound==0))
+                {
                                        /* Object file is not loaded - add it to the list */
                                        lbfh = (struct lbfile *) new (sizeof(struct lbfile));
-                                       if (lbfhead == NULL) {
+                                       if (lbfhead == NULL)
+                    {
                                                lbfhead = lbfh;
-                                       } else {
+                                       }
+                    else
+                    {
                                                lbf = lbfhead;
                                                while (lbf->next)
                                                lbf = lbf->next;
@@ -498,13 +569,35 @@ int fndsym( char *name )
                     }
                                        ThisLibr->loaded=1;
                                }
-                               return (1);     /* Found the symbol, return */
+
+                if(numfound==0)
+                {
+                    numfound++;
+                    FirstFound=ThisLibr;
+                }
+                else
+                {
+                    if( !( EQ(FirstFound->libspc, ThisLibr->libspc) && 
+                           EQ(FirstFound->relfil, ThisLibr->relfil) ) )
+                    {
+                        if(numfound==1)
+                        {
+                            printf("?Aslink-Warning-Definition of public symbol '%s'"
+                                   " found more than once:\n", name);
+                            printf("   Library: '%s', Module: '%s'\n",
+                                    FirstFound->libspc, FirstFound->relfil);
+                        }
+                        printf("   Library: '%s', Module: '%s'\n",
+                                ThisLibr->libspc, ThisLibr->relfil);
+                        numfound++;
+                    }
+                }
                        }
                        ThisSym=ThisSym->next;  /* Next sym in library */
                }
                ThisLibr=ThisLibr->next; /* Next library in list */
        }
-       return 0;       /* Failure - symbol not found in any library */
+       return numfound;
 }
 
 pmlibraryfile buildlibraryindex_SdccLib(char * PathLib, FILE * libfp, char * DirLib, pmlibraryfile This)