ref: b06e3b239d128826a33c97f38217cda0c25fee54
parent: 47442941b6a9480b14d784336fbb79c80b3000d8
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Nov 6 09:21:19 EDT 2021
Clean up -MT/-MQ code Remove unreachable argument presence check (handled by `getopt`) Merge allocation paths into a single `realloc` call Avoid searching for string lengths multiple times Tiny (compatible) change: no space between last dependent and colon if `-MT` or `-MQ` is specified
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -288,33 +288,23 @@
generatePhonyDeps = true;
break;
+ char *newTarget;
case 'Q':
case 'T':
- if (musl_optind == argc)
- errx(1, "-M%c takes a target file name argument", depType);
- ep = musl_optarg;
+ newTarget = musl_optarg;
if (depType == 'Q')
- ep = make_escape(ep);
+ newTarget = make_escape(newTarget);
+ size_t newTargetLen = strlen(newTarget) + 1; // Plus the space
- targetFileNameLen += strlen(ep) + 1;
- if (!targetFileName) {
- /* On first alloc, make an empty str */
- targetFileName = malloc(targetFileNameLen + 1);
- if (targetFileName)
- *targetFileName = '\0';
- } else {
- targetFileName = realloc(targetFileName,
- targetFileNameLen + 1);
- }
+ targetFileName = realloc(targetFileName,
+ targetFileNameLen + newTargetLen + 1);
if (targetFileName == NULL)
err(1, "Cannot append new file to target file list");
- strcat(targetFileName, ep);
+ memcpy(&targetFileName[targetFileNameLen], newTarget, newTargetLen);
if (depType == 'Q')
- free(ep);
- char *ptr = targetFileName + strlen(targetFileName);
-
- *ptr++ = ' ';
- *ptr = '\0';
+ free(newTarget);
+ targetFileNameLen += newTargetLen;
+ targetFileName[targetFileNameLen - 1] = ' ';
break;
}
break;
@@ -328,6 +318,8 @@
if (targetFileName == NULL)
targetFileName = objectName;
+ else
+ targetFileName[targetFileNameLen - 1] = '\0'; // Overwrite the last space
if (argc == musl_optind) {
fputs("FATAL: No input files\n", stderr);
@@ -344,7 +336,7 @@
if (dependfile) {
if (!targetFileName)
- errx(1, "Dependency files can only be created if a target file is specified with either -o, -MQ or -MT\n");
+ errx(1, "Dependency files can only be created if a target file is specified with either -o, -MQ or -MT");
fprintf(dependfile, "%s: %s\n", targetFileName, mainFileName);
}