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