bug 427642 fix: error with files like ./file.asm
authorbernhardheld <bernhardheld@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Wed, 15 Aug 2001 12:39:09 +0000 (12:39 +0000)
committerbernhardheld <bernhardheld@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Wed, 15 Aug 2001 12:39:09 +0000 (12:39 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1144 4a8a32a2-be11-0410-ad9d-d568d2c75423

as/z80/asmain.c

index a6463ff2059def9af8182ef47eb338bb256831a5..19099878803ec9f7c0b8dd06e4ccad382efae488 100644 (file)
@@ -1024,30 +1024,36 @@ char *fn;
 char *ft;
 int wf;
 {
-       register char *p1, *p2, *p3;
+       register char *p2, *p3;
        register int c;
        FILE *fp;
 
-       p1 = fn;
        p2 = afn;
        p3 = ft;
-       while ((c = *p1++) != 0 && c != FSEPX) {
-               if (p2 < &afn[FILSPC-4])
-                       *p2++ = c;
-       }
+
+       strcpy (afn, fn);
+       p2 = strrchr (afn, FSEPX);              // search last '.'
+       if (!p2)
+               p2 = afn + strlen (afn);
+       if (p2 > &afn[FILSPC-4])                // truncate filename, if it's too long
+               p2 = &afn[FILSPC-4];
        *p2++ = FSEPX;
-       if (*p3 == 0) {
-               if (c == FSEPX) {
-                       p3 = p1;
-               } else {
-                       p3 = dsft;
-               }
+
+       // choose a file-extension
+       if (*p3 == 0) {                                 // extension supplied?
+               p3 = strrchr (fn, FSEPX);       // no: extension in fn?
+               if (p3)
+                       ++p3;
+               else
+                       p3 = dsft;                                      // no: default extension
        }
-       while ((c = *p3++) != 0) {
+
+       while ((c = *p3++) != 0) {              // strncpy
                if (p2 < &afn[FILSPC-1])
                        *p2++ = c;
        }
        *p2++ = 0;
+
        if ((fp = fopen(afn, wf?"w":"r")) == NULL) {
                fprintf(stderr, "%s: cannot %s.\n", afn, wf?"create":"open");
                asexit(1);