annotate lib/portability.c @ 1613:96aa7ec74936 draft

Fix yet another sed bug. The s/// command would copy the \ of substitutions before deciding what to do with them (generally overwriting the \ with the new data). When the substitution was A) at the very end of the new string, B) resolved to nothing, it could leave a trailing \ that didn't belong there and didn't get overwritten because the "copy trailing data" part that copies the original string's null terminator already happened before the \ overwrote it. The ghostwheel() function restarts regexes after embedded NUL bytes, but if the string it's passed is _longer_ than the length it's told then it gets confused (and it means we're off the end of our allocation so segfaults are likely). Fix: test for \ first and move the "copy byte" logic into an else case.
author Rob Landley <rob@landley.net>
date Mon, 15 Dec 2014 03:34:55 -0600
parents ce22ad7a26c1
children aafd2f28245a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
1 /* portability.c - code to workaround the deficiencies of various platforms.
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
2 *
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
3 * Copyright 2012 Rob Landley <rob@landley.net>
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
4 * Copyright 2012 Georgi Chorbadzhiyski <gf@unixsol.org>
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
5 */
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
6
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
7 #include "toys.h"
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
8
1600
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
9 #if !defined(__uClinux__)
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
10 pid_t xfork(void)
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
11 {
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
12 pid_t pid = fork();
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
13
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
14 if (pid < 0) perror_exit("fork");
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
15
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
16 return pid;
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
17 }
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
18 #endif
ce22ad7a26c1 Work with buildroot's extensively patched uClibc, and for nommu support move xfork() to portability.h and #ifdef based on __uClinux__ (which seems to be the nommu compiler define).
Rob Landley <rob@landley.net>
parents: 1569
diff changeset
19
1569
da72fa267b7b A patch against your current ToT that builds in AOSP master.
Elliott Hughes <enh@google.com>
parents: 1568
diff changeset
20 #if defined(__APPLE__)
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
21 ssize_t getdelim(char **linep, size_t *np, int delim, FILE *stream)
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
22 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
23 int ch;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
24 size_t new_len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
25 ssize_t i = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
26 char *line, *new_line;
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
27
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
28 // Invalid input
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
29 if (!linep || !np) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
30 errno = EINVAL;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
31 return -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
32 }
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
33
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
34 if (*linep == NULL || *np == 0) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
35 *np = 1024;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
36 *linep = calloc(1, *np);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
37 if (*linep == NULL) return -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
38 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
39 line = *linep;
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
40
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
41 while ((ch = getc(stream)) != EOF) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
42 if (i > *np) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
43 // Need more space
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
44 new_len = *np + 1024;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
45 new_line = realloc(*linep, new_len);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
46 if (!new_line) return -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
47 *np = new_len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
48 *linep = new_line;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
49 }
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
50
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
51 line[i] = ch;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
52 if (ch == delim) break;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
53 i += 1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
54 }
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
55
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
56 if (i > *np) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
57 // Need more space
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
58 new_len = i + 2;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
59 new_line = realloc(*linep, new_len);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
60 if (!new_line) return -1;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
61 *np = new_len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
62 *linep = new_line;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
63 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
64 line[i + 1] = '\0';
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
65
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
66 return i > 0 ? i : -1;
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
67 }
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
68
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
69 ssize_t getline(char **linep, size_t *np, FILE *stream)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
70 {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
71 return getdelim(linep, np, '\n', stream);
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
72 }
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
73
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
74 extern char **environ;
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
75
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
76 int clearenv(void)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
77 {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
78 *environ = NULL;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 549
diff changeset
79 return 0;
549
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
80 }
1f5bd8c93093 Implement Apple and Android versions of getline(), getdelim(), and clearenv().
Georgi Chorbadzhiyski <gf@unixsol.org>
parents:
diff changeset
81 #endif