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