]> granicus.if.org Git - yasm/commitdiff
Make extension-replacement a utility function in file.c (as it will need to
authorPeter Johnson <peter@tortall.net>
Sun, 2 Dec 2001 23:15:30 +0000 (23:15 -0000)
committerPeter Johnson <peter@tortall.net>
Sun, 2 Dec 2001 23:15:30 +0000 (23:15 -0000)
be performed for the list file, etc, as well as the object file).

svn path=/trunk/yasm/; revision=381

frontends/yasm/yasm.c
libyasm/file.c
libyasm/file.h
src/file.c
src/file.h
src/main.c

index 53445b9296c5df4dcf6a3f026b9dcea99245342c..e4e3d7874fabadd92663737b377396360d228475 100644 (file)
@@ -27,6 +27,7 @@
 #endif
 
 #include "bitvect.h"
+#include "file.h"
 
 #include "globals.h"
 #include "options.h"
@@ -139,20 +140,12 @@ main(int argc, char *argv[])
     /* open the object file if not specified */
     if (!obj) {
        /* build the object filename */
-       char *exttail;
        if (obj_filename)
            xfree(obj_filename);
        assert(in_filename != NULL);
-       /* allocate enough space for full existing name + extension */
-       obj_filename = xmalloc(strlen(in_filename)+
-                              strlen(cur_objfmt->extension)+2);
-       strcpy(obj_filename, in_filename);
-       exttail = strrchr(obj_filename, '.');
-       if (!exttail)
-           exttail = strrchr(obj_filename, '\0');
-       /*@-nullpass@*/
-       sprintf(exttail, ".%s", cur_objfmt->extension);
-       /*@=nullpass@*/
+       /* replace (or add) extension */
+       obj_filename = replace_extension(in_filename, cur_objfmt->extension,
+                                        "yasm.out");
 
        /* open the built filename */
        obj = fopen(obj_filename, "wb");
index 410c68257f6230bcb17cdf34efed4df1cf99dd38..54c5534a9c7dfb7d0237509d5a4df16495ea6b5a 100644 (file)
 
 #include "file.h"
 
+#include "errwarn.h"
+
+
+char *
+replace_extension(const char *orig, const char *ext, const char *def)
+{
+    char *out, *outext;
+
+    /* allocate enough space for full existing name + extension */
+    out = xmalloc(strlen(orig)+strlen(ext)+2);
+    strcpy(out, orig);
+    outext = strrchr(out, '.');
+    if (outext) {
+       /* Existing extension: make sure it's not the same as the replacement
+        * (as we don't want to overwrite the source file).
+        */
+       outext++;   /* advance past '.' */
+       if (strcmp(outext, ext)) {
+           outext = NULL;  /* indicate default should be used */
+           WarningNow(_("file name already ends in `.%s': output will be in `%s'"),
+                      ext, def);
+       }
+    } else {
+       /* No extension: make sure the output extension is not empty
+        * (again, we don't want to overwrite the source file).
+        */
+       if (*ext == '\0')
+           WarningNow(_("file name already has no extension: output will be in `%s'"),
+                      def);
+       else {
+           outext = strrchr(out, '\0');    /* point to end of the string */
+           *outext++ = '.';                /* append '.' */
+       }
+    }
+
+    /* replace extension or use default name */
+    if (outext)
+       strcpy(outext, ext);
+    else
+       strcpy(out, def);
+
+    return out;
+}
 
 size_t
 fwrite_short(unsigned short val, FILE *f)
index b1e5229f4615cf8b129778ccde6f24b53157d156..e9a93877470a15a0222e287b211545c8311899e1 100644 (file)
 #ifndef YASM_FILE_H
 #define YASM_FILE_H
 
+/* Replace extension on a filename (or append one if none is present).
+ * If output filename would be identical to input (same extension out as in),
+ * returns (copy of) def.
+ */
+/*@only@*/ char *replace_extension(const char *orig, const char *ext,
+                                  const char *def);
+
 /* These functions only work properly if p is an (unsigned char *) */
 
 #define WRITE_BYTE(ptr, val)                   \
index 410c68257f6230bcb17cdf34efed4df1cf99dd38..54c5534a9c7dfb7d0237509d5a4df16495ea6b5a 100644 (file)
 
 #include "file.h"
 
+#include "errwarn.h"
+
+
+char *
+replace_extension(const char *orig, const char *ext, const char *def)
+{
+    char *out, *outext;
+
+    /* allocate enough space for full existing name + extension */
+    out = xmalloc(strlen(orig)+strlen(ext)+2);
+    strcpy(out, orig);
+    outext = strrchr(out, '.');
+    if (outext) {
+       /* Existing extension: make sure it's not the same as the replacement
+        * (as we don't want to overwrite the source file).
+        */
+       outext++;   /* advance past '.' */
+       if (strcmp(outext, ext)) {
+           outext = NULL;  /* indicate default should be used */
+           WarningNow(_("file name already ends in `.%s': output will be in `%s'"),
+                      ext, def);
+       }
+    } else {
+       /* No extension: make sure the output extension is not empty
+        * (again, we don't want to overwrite the source file).
+        */
+       if (*ext == '\0')
+           WarningNow(_("file name already has no extension: output will be in `%s'"),
+                      def);
+       else {
+           outext = strrchr(out, '\0');    /* point to end of the string */
+           *outext++ = '.';                /* append '.' */
+       }
+    }
+
+    /* replace extension or use default name */
+    if (outext)
+       strcpy(outext, ext);
+    else
+       strcpy(out, def);
+
+    return out;
+}
 
 size_t
 fwrite_short(unsigned short val, FILE *f)
index b1e5229f4615cf8b129778ccde6f24b53157d156..e9a93877470a15a0222e287b211545c8311899e1 100644 (file)
 #ifndef YASM_FILE_H
 #define YASM_FILE_H
 
+/* Replace extension on a filename (or append one if none is present).
+ * If output filename would be identical to input (same extension out as in),
+ * returns (copy of) def.
+ */
+/*@only@*/ char *replace_extension(const char *orig, const char *ext,
+                                  const char *def);
+
 /* These functions only work properly if p is an (unsigned char *) */
 
 #define WRITE_BYTE(ptr, val)                   \
index 53445b9296c5df4dcf6a3f026b9dcea99245342c..e4e3d7874fabadd92663737b377396360d228475 100644 (file)
@@ -27,6 +27,7 @@
 #endif
 
 #include "bitvect.h"
+#include "file.h"
 
 #include "globals.h"
 #include "options.h"
@@ -139,20 +140,12 @@ main(int argc, char *argv[])
     /* open the object file if not specified */
     if (!obj) {
        /* build the object filename */
-       char *exttail;
        if (obj_filename)
            xfree(obj_filename);
        assert(in_filename != NULL);
-       /* allocate enough space for full existing name + extension */
-       obj_filename = xmalloc(strlen(in_filename)+
-                              strlen(cur_objfmt->extension)+2);
-       strcpy(obj_filename, in_filename);
-       exttail = strrchr(obj_filename, '.');
-       if (!exttail)
-           exttail = strrchr(obj_filename, '\0');
-       /*@-nullpass@*/
-       sprintf(exttail, ".%s", cur_objfmt->extension);
-       /*@=nullpass@*/
+       /* replace (or add) extension */
+       obj_filename = replace_extension(in_filename, cur_objfmt->extension,
+                                        "yasm.out");
 
        /* open the built filename */
        obj = fopen(obj_filename, "wb");