Mercurial > hg > toybox
comparison lib/portability.c @ 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.
The actual code should be the same afterward, this is just cosmetic refactoring.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 13 Nov 2012 17:14:08 -0600 |
parents | 1f5bd8c93093 |
children | 685a0da6ca59 |
comparison
equal
deleted
inserted
replaced
693:4a5a250e0633 | 694:786841fdb1e0 |
---|---|
1 /* vi: set sw=4 ts=4 :*/ | |
2 /* portability.c - code to workaround the deficiencies of various platforms. | 1 /* portability.c - code to workaround the deficiencies of various platforms. |
3 * | 2 * |
4 * Copyright 2012 Rob Landley <rob@landley.net> | 3 * Copyright 2012 Rob Landley <rob@landley.net> |
5 * Copyright 2012 Georgi Chorbadzhiyski <gf@unixsol.org> | 4 * Copyright 2012 Georgi Chorbadzhiyski <gf@unixsol.org> |
6 */ | 5 */ |
8 #include "toys.h" | 7 #include "toys.h" |
9 | 8 |
10 #if defined(__APPLE__) || defined(__ANDROID__) | 9 #if defined(__APPLE__) || defined(__ANDROID__) |
11 ssize_t getdelim(char **linep, size_t *np, int delim, FILE *stream) | 10 ssize_t getdelim(char **linep, size_t *np, int delim, FILE *stream) |
12 { | 11 { |
13 int ch; | 12 int ch; |
14 size_t new_len; | 13 size_t new_len; |
15 ssize_t i = 0; | 14 ssize_t i = 0; |
16 char *line, *new_line; | 15 char *line, *new_line; |
17 | 16 |
18 // Invalid input | 17 // Invalid input |
19 if (!linep || !np) { | 18 if (!linep || !np) { |
20 errno = EINVAL; | 19 errno = EINVAL; |
21 return -1; | 20 return -1; |
22 } | 21 } |
23 | 22 |
24 if (*linep == NULL || *np == 0) { | 23 if (*linep == NULL || *np == 0) { |
25 *np = 1024; | 24 *np = 1024; |
26 *linep = calloc(1, *np); | 25 *linep = calloc(1, *np); |
27 if (*linep == NULL) | 26 if (*linep == NULL) return -1; |
28 return -1; | 27 } |
29 } | 28 line = *linep; |
30 line = *linep; | |
31 | 29 |
32 while ((ch = getc(stream)) != EOF) { | 30 while ((ch = getc(stream)) != EOF) { |
33 if (i > *np) { | 31 if (i > *np) { |
34 // Need more space | 32 // Need more space |
35 new_len = *np + 1024; | 33 new_len = *np + 1024; |
36 new_line = realloc(*linep, new_len); | 34 new_line = realloc(*linep, new_len); |
37 if (!new_line) | 35 if (!new_line) return -1; |
38 return -1; | 36 *np = new_len; |
39 *np = new_len; | 37 *linep = new_line; |
40 *linep = new_line; | 38 } |
41 } | |
42 | 39 |
43 line[i] = ch; | 40 line[i] = ch; |
44 if (ch == delim) | 41 if (ch == delim) break; |
45 break; | 42 i += 1; |
46 i += 1; | 43 } |
47 } | |
48 | 44 |
49 if (i > *np) { | 45 if (i > *np) { |
50 // Need more space | 46 // Need more space |
51 new_len = i + 2; | 47 new_len = i + 2; |
52 new_line = realloc(*linep, new_len); | 48 new_line = realloc(*linep, new_len); |
53 if (!new_line) | 49 if (!new_line) return -1; |
54 return -1; | 50 *np = new_len; |
55 *np = new_len; | 51 *linep = new_line; |
56 *linep = new_line; | 52 } |
57 } | 53 line[i + 1] = '\0'; |
58 line[i + 1] = '\0'; | |
59 | 54 |
60 return i > 0 ? i : -1; | 55 return i > 0 ? i : -1; |
61 } | 56 } |
62 | 57 |
63 ssize_t getline(char **linep, size_t *np, FILE *stream) { | 58 ssize_t getline(char **linep, size_t *np, FILE *stream) |
64 return getdelim(linep, np, '\n', stream); | 59 { |
60 return getdelim(linep, np, '\n', stream); | |
65 } | 61 } |
66 #endif | 62 #endif |
67 | 63 |
68 #if defined(__APPLE__) | 64 #if defined(__APPLE__) |
69 extern char **environ; | 65 extern char **environ; |
70 | 66 |
71 int clearenv(void) { | 67 int clearenv(void) |
72 *environ = NULL; | 68 { |
73 return 0; | 69 *environ = NULL; |
70 return 0; | |
74 } | 71 } |
75 #endif | 72 #endif |