summaryrefslogtreecommitdiff
path: root/libyasm
diff options
context:
space:
mode:
authorPeter Johnson <peter@tortall.net>2001-12-02 23:15:30 +0000
committerPeter Johnson <peter@tortall.net>2001-12-02 23:15:30 +0000
commit6bbcad2e6c5841e7bb9602e322ac7bebc2801f41 (patch)
tree4a760187cba206eb3a3e96d81dd7cdde90d279b2 /libyasm
parentc42294ab0a65b1588bb3cca802478fde5860898e (diff)
downloadyasm-6bbcad2e6c5841e7bb9602e322ac7bebc2801f41.tar.gz
Make extension-replacement a utility function in file.c (as it will need to
be performed for the list file, etc, as well as the object file). svn path=/trunk/yasm/; revision=381
Diffstat (limited to 'libyasm')
-rw-r--r--libyasm/file.c43
-rw-r--r--libyasm/file.h7
2 files changed, 50 insertions, 0 deletions
diff --git a/libyasm/file.c b/libyasm/file.c
index 410c6825..54c5534a 100644
--- a/libyasm/file.c
+++ b/libyasm/file.c
@@ -24,6 +24,49 @@
#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)
diff --git a/libyasm/file.h b/libyasm/file.h
index b1e5229f..e9a93877 100644
--- a/libyasm/file.h
+++ b/libyasm/file.h
@@ -22,6 +22,13 @@
#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) \