FreeCalypso > hg > freecalypso-tools
annotate libpwon/readconf.c @ 921:74d284add54d
fc-fsio: guard against bogus readdir results from the target
If the FFS being operated on contains SE K2x0 extended filenames,
readdir will return strings that are bad for printing. We need to
guard against this possibility, and also against possible other
bogosity that could be sent by other alien firmwares.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 31 Dec 2022 22:55:23 +0000 |
parents | 7d1df6d831e4 |
children | ab54957dbe35 |
rev | line source |
---|---|
571
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <ctype.h> |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <stdio.h> |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <stdlib.h> |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <string.h> |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <strings.h> |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 static char conf_file_pathname[] = "/opt/freecalypso/bootctrl.conf"; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #define MAX_BOOTCTRL_CONF_LINE 510 |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 char bootctrl_pwon_cmd[MAX_BOOTCTRL_CONF_LINE+1]; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 find_bootctrl_entry(soughtname) |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 char *soughtname; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 { |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 FILE *inf; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 char linebuf[MAX_BOOTCTRL_CONF_LINE+2]; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 int lineno; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 char *cp, *np; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
736
7d1df6d831e4
libpwon: added -Pdtr and -Prts support for DUART28C
Mychaela Falconia <falcon@freecalypso.org>
parents:
571
diff
changeset
|
21 if (bootctrl_check_duart28c(soughtname)) |
7d1df6d831e4
libpwon: added -Pdtr and -Prts support for DUART28C
Mychaela Falconia <falcon@freecalypso.org>
parents:
571
diff
changeset
|
22 return(0); |
571
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 inf = fopen(conf_file_pathname, "r"); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 if (!inf) { |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 perror(conf_file_pathname); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 return(-1); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 } |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 cp = index(linebuf, '\n'); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 if (!cp) { |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 fprintf(stderr, |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 "%s line %d: too long or missing newline\n", |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 conf_file_pathname, lineno); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 fclose(inf); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return(-1); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 *cp = '\0'; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 for (cp = linebuf; isspace(*cp); cp++) |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 ; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 if (*cp == '\0' || *cp == '#') |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 continue; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 for (np = cp; *cp && !isspace(*cp); cp++) |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 ; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (!*cp) { |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 invalid_syntax: fprintf(stderr, "%s line %d: invalid syntax\n", |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 conf_file_pathname, lineno); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 fclose(inf); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 return(-1); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 } |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 *cp++ = '\0'; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 while (isspace(*cp)) |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 cp++; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (*cp == '\0' || *cp == '#') |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 goto invalid_syntax; |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (!strcmp(np, soughtname)) { |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 strcpy(bootctrl_pwon_cmd, cp); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 fclose(inf); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 return(0); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 fclose(inf); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 fprintf(stderr, "error: no \"%s\" entry found in %s\n", soughtname, |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 conf_file_pathname); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 return(-1); |
41b3e010808d
libpwon started, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |