FreeCalypso > hg > freecalypso-tools
view loadtools/hwparam.c @ 465:003e48f8ebe1
rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel
I generally don't use NULL and use plain 0 instead, based on a "NULL
considered harmful" discussion on the classiccmp mailing list many aeons
ago (I couldn't find it, and I reason that it must have been 2005 or
earlier), but a recent complaint by a packager sent me searching, and I
found this:
https://ewontfix.com/11/
While I don't give a @#$% about "modern" systems and code-nazi tools,
I realized that passing a plain 0 as a pointer sentinel in execl is wrong
because it will break on systems where pointers are longer than the plain
int type. Again, I don't give a @#$% about the abomination of x86_64 and
the like, but if anyone ever manages to port my code to something like a
PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0
as a function argument where a pointer is expected most definitely won't
work: if the most natural stack slot and SP alignment unit is 16 bits,
fitting an int, with longs and pointers taking up two such slots, then
the call stack will be totally wrong with a plain 0 passed for a pointer.
Casting the 0 to (char *) ought to be the most kosher solution for the
most retro systems possible.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 11 Feb 2019 00:00:19 +0000 |
parents | e7502631a0f9 |
children | 0dd2c87c1b63 |
line wrap: on
line source
/* * This module contains the code that reads the hardware parameter files * specified with -h or -H, and sets variables for later use by other code. */ #include <sys/param.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include <strings.h> #include <stdlib.h> extern char default_helpers_dir[]; extern void set_boot_reflash_hack(); extern void set_default_exit_mode(); extern void set_flash_device(); char hw_init_script[128]; static void handle_compal_stage(arg, filename_for_errs, lineno_for_errs) char *arg; char *filename_for_errs; int lineno_for_errs; { char *cp; while (isspace(*arg)) arg++; if (!*arg) { fprintf(stderr, "%s line %d: compal-stage setting requires an argument\n", filename_for_errs, lineno_for_errs); exit(1); } for (cp = arg; *cp && !isspace(*cp); cp++) ; *cp = '\0'; set_compalstage_short(arg); } static void handle_init_script(arg, filename_for_errs, lineno_for_errs) char *arg; char *filename_for_errs; int lineno_for_errs; { char *cp; while (isspace(*arg)) arg++; if (!*arg) { fprintf(stderr, "%s line %d: init-script setting requires an argument\n", filename_for_errs, lineno_for_errs); exit(1); } for (cp = arg; *cp && !isspace(*cp); cp++) ; *cp = '\0'; if (cp - arg > sizeof(hw_init_script) - 1) { fprintf(stderr, "%s line %d: init-script argument is too long (buffer overflow)\n", filename_for_errs, lineno_for_errs); exit(1); } strcpy(hw_init_script, arg); } static void handle_pll_config(arg, filename_for_errs, lineno_for_errs) char *arg; char *filename_for_errs; int lineno_for_errs; { int mult, div; while (isspace(*arg)) arg++; if (!isdigit(*arg)) { inv: fprintf(stderr, "%s line %d: pll-config argument must be M/N\n", filename_for_errs, lineno_for_errs); exit(1); } mult = atoi(arg); arg++; if (isdigit(*arg)) arg++; if (*arg++ != '/') goto inv; if (!isdigit(*arg)) goto inv; div = atoi(arg); arg++; if (*arg && !isspace(*arg)) goto inv; if (mult < 0 || mult > 31 || div < 1 || div > 4) { fprintf(stderr, "%s line %d: pll-config argument is out of range\n", filename_for_errs, lineno_for_errs); exit(1); } set_romload_pll_conf((mult << 2) | (div - 1)); } static void handle_rhea_cntl(arg, filename_for_errs, lineno_for_errs) char *arg; char *filename_for_errs; int lineno_for_errs; { int byte; while (isspace(*arg)) arg++; if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X')) arg += 2; byte = decode_hex_byte(arg); if (byte < 0 || arg[2] && !isspace(arg[2])) { fprintf(stderr, "%s line %d: rhea-cntl argument must be a hex byte value\n", filename_for_errs, lineno_for_errs); exit(1); } set_romload_rhea_cntl(byte); } static struct cmdtab { char *name; void (*func)(); } cmdtab[] = { {"boot-reflash-hack", set_boot_reflash_hack}, {"compal-stage", handle_compal_stage}, {"exit-mode", set_default_exit_mode}, {"flash", set_flash_device}, {"init-script", handle_init_script}, {"pll-config", handle_pll_config}, {"rhea-cntl", handle_rhea_cntl}, {0, 0} }; void read_hwparam_file_fullpath(filename) char *filename; { FILE *f; char linebuf[512]; int lineno; char *cp, *np; struct cmdtab *tp; f = fopen(filename, "r"); if (!f) { perror(filename); exit(1); } for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { for (cp = linebuf; isspace(*cp); cp++) ; if (!*cp || *cp == '#') continue; for (np = cp; *cp && !isspace(*cp); cp++) ; if (*cp) *cp++ = '\0'; for (tp = cmdtab; tp->name; tp++) if (!strcmp(tp->name, np)) break; if (tp->func) tp->func(cp, filename, lineno); else { fprintf(stderr, "%s line %d: setting \"%s\" not understood\n", filename, lineno, np); exit(1); } } fclose(f); } void read_hwparam_file_shortname(confname) char *confname; { char pathname[MAXPATHLEN]; sprintf(pathname, "%s/%s.config", default_helpers_dir, confname); read_hwparam_file_fullpath(pathname); }