]> granicus.if.org Git - yasm/commitdiff
Allow replacing with no extension (eg w/o an appended '.'). Needed for bin
authorPeter Johnson <peter@tortall.net>
Sun, 10 Mar 2002 23:07:20 +0000 (23:07 -0000)
committerPeter Johnson <peter@tortall.net>
Sun, 10 Mar 2002 23:07:20 +0000 (23:07 -0000)
objfmt output.

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

libyasm/file.c
libyasm/file.h
src/file.c
src/file.h

index 21a8f556fa7e191eba771c9dc4ca201b15b985ee..a6649dc1056f2c86d3ada47e1a81ab9ae7428ebe 100644 (file)
 
 
 char *
-replace_extension(const char *orig, const char *ext, const char *def)
+replace_extension(const char *orig, /*@null@*/ const char *ext,
+                 const char *def)
 {
     char *out, *outext;
 
     /* allocate enough space for full existing name + extension */
-    out = xmalloc(strlen(orig)+strlen(ext)+2);
+    out = xmalloc(strlen(orig)+(ext ? (strlen(ext)+2) : 1));
     strcpy(out, orig);
     outext = strrchr(out, '.');
     if (outext) {
@@ -41,7 +42,7 @@ replace_extension(const char *orig, const char *ext, const char *def)
         * (as we don't want to overwrite the source file).
         */
        outext++;   /* advance past '.' */
-       if (strcmp(outext, ext) == 0) {
+       if (ext && strcmp(outext, ext) == 0) {
            outext = NULL;  /* indicate default should be used */
            WarningNow(_("file name already ends in `.%s': output will be in `%s'"),
                       ext, def);
@@ -50,7 +51,7 @@ replace_extension(const char *orig, const char *ext, const char *def)
        /* No extension: make sure the output extension is not empty
         * (again, we don't want to overwrite the source file).
         */
-       if (*ext == '\0')
+       if (!ext)
            WarningNow(_("file name already has no extension: output will be in `%s'"),
                       def);
        else {
@@ -60,9 +61,14 @@ replace_extension(const char *orig, const char *ext, const char *def)
     }
 
     /* replace extension or use default name */
-    if (outext)
-       strcpy(outext, ext);
-    else
+    if (outext) {
+       if (!ext) {
+           /* Back up and replace '.' with string terminator */
+           outext--;
+           *outext = '\0';
+       } else
+           strcpy(outext, ext);
+    } else
        strcpy(out, def);
 
     return out;
index e9a93877470a15a0222e287b211545c8311899e1..31b8f257bc73bb8a075f47769b485f812bc60343 100644 (file)
 /* 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.
+ * A NULL ext means the trailing '.' should NOT be included, whereas a "" ext
+ * means the trailing '.' should be included.
  */
-/*@only@*/ char *replace_extension(const char *orig, const char *ext,
-                                  const char *def);
+/*@only@*/ char *replace_extension(const char *orig, /*@null@*/
+                                  const char *ext, const char *def);
 
 /* These functions only work properly if p is an (unsigned char *) */
 
index 21a8f556fa7e191eba771c9dc4ca201b15b985ee..a6649dc1056f2c86d3ada47e1a81ab9ae7428ebe 100644 (file)
 
 
 char *
-replace_extension(const char *orig, const char *ext, const char *def)
+replace_extension(const char *orig, /*@null@*/ const char *ext,
+                 const char *def)
 {
     char *out, *outext;
 
     /* allocate enough space for full existing name + extension */
-    out = xmalloc(strlen(orig)+strlen(ext)+2);
+    out = xmalloc(strlen(orig)+(ext ? (strlen(ext)+2) : 1));
     strcpy(out, orig);
     outext = strrchr(out, '.');
     if (outext) {
@@ -41,7 +42,7 @@ replace_extension(const char *orig, const char *ext, const char *def)
         * (as we don't want to overwrite the source file).
         */
        outext++;   /* advance past '.' */
-       if (strcmp(outext, ext) == 0) {
+       if (ext && strcmp(outext, ext) == 0) {
            outext = NULL;  /* indicate default should be used */
            WarningNow(_("file name already ends in `.%s': output will be in `%s'"),
                       ext, def);
@@ -50,7 +51,7 @@ replace_extension(const char *orig, const char *ext, const char *def)
        /* No extension: make sure the output extension is not empty
         * (again, we don't want to overwrite the source file).
         */
-       if (*ext == '\0')
+       if (!ext)
            WarningNow(_("file name already has no extension: output will be in `%s'"),
                       def);
        else {
@@ -60,9 +61,14 @@ replace_extension(const char *orig, const char *ext, const char *def)
     }
 
     /* replace extension or use default name */
-    if (outext)
-       strcpy(outext, ext);
-    else
+    if (outext) {
+       if (!ext) {
+           /* Back up and replace '.' with string terminator */
+           outext--;
+           *outext = '\0';
+       } else
+           strcpy(outext, ext);
+    } else
        strcpy(out, def);
 
     return out;
index e9a93877470a15a0222e287b211545c8311899e1..31b8f257bc73bb8a075f47769b485f812bc60343 100644 (file)
 /* 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.
+ * A NULL ext means the trailing '.' should NOT be included, whereas a "" ext
+ * means the trailing '.' should be included.
  */
-/*@only@*/ char *replace_extension(const char *orig, const char *ext,
-                                  const char *def);
+/*@only@*/ char *replace_extension(const char *orig, /*@null@*/
+                                  const char *ext, const char *def);
 
 /* These functions only work properly if p is an (unsigned char *) */