Skip to content

Commit

Permalink
Separate extern getopt implementation from the unistd.h one
Browse files Browse the repository at this point in the history
Fixes #710
  • Loading branch information
Rangi42 authored and ISSOtm committed Feb 11, 2021
1 parent 88e1cc7 commit 464a3a4
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 150 deletions.
4 changes: 2 additions & 2 deletions include/extern/getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
#ifndef RGBDS_EXTERN_GETOPT_H
#define RGBDS_EXTERN_GETOPT_H

extern char *optarg;
extern int optind, opterr, optopt, optreset;
extern char *musl_optarg;
extern int musl_optind, musl_opterr, musl_optopt, musl_optreset;

struct option {
const char *name;
Expand Down
44 changes: 22 additions & 22 deletions src/asm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,20 @@ int main(int argc, char *argv[])
while ((ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1) {
switch (ch) {
case 'b':
if (strlen(optarg) == 2)
opt_B(&optarg[1]);
if (strlen(musl_optarg) == 2)
opt_B(&musl_optarg[1]);
else
errx(1, "Must specify exactly 2 characters for option 'b'");
break;

char *equals;
case 'D':
equals = strchr(optarg, '=');
equals = strchr(musl_optarg, '=');
if (equals) {
*equals = '\0';
sym_AddString(optarg, equals + 1);
sym_AddString(musl_optarg, equals + 1);
} else {
sym_AddString(optarg, "1");
sym_AddString(musl_optarg, "1");
}
break;

Expand All @@ -199,8 +199,8 @@ int main(int argc, char *argv[])
break;

case 'g':
if (strlen(optarg) == 4)
opt_G(&optarg[1]);
if (strlen(musl_optarg) == 4)
opt_G(&musl_optarg[1]);
else
errx(1, "Must specify exactly 4 characters for option 'g'");
break;
Expand All @@ -210,31 +210,31 @@ int main(int argc, char *argv[])
break;

case 'i':
fstk_AddIncludePath(optarg);
fstk_AddIncludePath(musl_optarg);
break;

case 'L':
optimizeloads = false;
break;

case 'M':
if (!strcmp("-", optarg))
if (!strcmp("-", musl_optarg))
dependfile = stdout;
else
dependfile = fopen(optarg, "w");
dependfile = fopen(musl_optarg, "w");
if (dependfile == NULL)
err(1, "Could not open dependfile %s", optarg);
err(1, "Could not open dependfile %s", musl_optarg);
break;

case 'o':
out_SetFileName(optarg);
out_SetFileName(musl_optarg);
break;

unsigned long fill;
case 'p':
fill = strtoul(optarg, &ep, 0);
fill = strtoul(musl_optarg, &ep, 0);

if (optarg[0] == '\0' || *ep != '\0')
if (musl_optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'p'");

if (fill < 0 || fill > 0xFF)
Expand All @@ -244,9 +244,9 @@ int main(int argc, char *argv[])
break;

case 'r':
maxRecursionDepth = strtoul(optarg, &ep, 0);
maxRecursionDepth = strtoul(musl_optarg, &ep, 0);

if (optarg[0] == '\0' || *ep != '\0')
if (musl_optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'r'");
break;

Expand All @@ -258,7 +258,7 @@ int main(int argc, char *argv[])
break;

case 'W':
processWarningFlag(optarg);
processWarningFlag(musl_optarg);
break;

case 'w':
Expand All @@ -278,9 +278,9 @@ int main(int argc, char *argv[])

case 'Q':
case 'T':
if (optind == argc)
if (musl_optind == argc)
errx(1, "-M%c takes a target file name argument", depType);
ep = optarg;
ep = musl_optarg;
if (depType == 'Q')
ep = make_escape(ep);

Expand Down Expand Up @@ -317,15 +317,15 @@ int main(int argc, char *argv[])
if (tzTargetFileName == NULL)
tzTargetFileName = tzObjectname;

if (argc == optind) {
if (argc == musl_optind) {
fputs("FATAL: No input files\n", stderr);
print_usage();
} else if (argc != optind + 1) {
} else if (argc != musl_optind + 1) {
fputs("FATAL: More than one input file given\n", stderr);
print_usage();
}

char const *mainFileName = argv[optind];
char const *mainFileName = argv[musl_optind];

if (verbose)
printf("Assembling %s\n", mainFileName);
Expand Down
Loading

0 comments on commit 464a3a4

Please sign in to comment.