| 48 | // getline() is only available on OS X Lion and newer |
| 49 | |
| 50 | #ifdef __APPLE__ |
| 51 | #ifndef __MAC_OS_X_VERSION_MIN_REQUIRED |
| 52 | #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050 |
| 53 | #include <Availability.h> |
| 54 | #else |
| 55 | #include <AvailabilityMacros.h> |
| 56 | #endif |
| 57 | #endif |
| 58 | #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1060 |
| 59 | |
| 60 | static const int line_size = 128; |
| 61 | |
| 62 | static ssize_t |
| 63 | getdelim (char **lineptr, size_t *n, int delim, FILE *stream); |
| 64 | |
| 65 | static ssize_t |
| 66 | getline (char **lineptr, size_t *n, FILE *stream); |
| 67 | |
| 68 | static ssize_t |
| 69 | getdelim (char **lineptr, size_t *n, int delim, FILE *stream) |
| 70 | { |
| 71 | int indx = 0; |
| 72 | int c; |
| 73 | |
| 74 | /* Sanity checks. */ |
| 75 | if (lineptr == NULL || n == NULL || stream == NULL) |
| 76 | return -1; |
| 77 | |
| 78 | /* Allocate the line the first time. */ |
| 79 | if (*lineptr == NULL) |
| 80 | { |
| 81 | *lineptr = malloc (line_size); |
| 82 | if (*lineptr == NULL) |
| 83 | return -1; |
| 84 | *n = line_size; |
| 85 | } |
| 86 | |
| 87 | /* Clear the line. */ |
| 88 | memset (*lineptr, '\0', *n); |
| 89 | |
| 90 | while ((c = getc (stream)) != EOF) |
| 91 | { |
| 92 | /* Check if more memory is needed. */ |
| 93 | if (indx >= *n) |
| 94 | { |
| 95 | *lineptr = realloc (*lineptr, *n + line_size); |
| 96 | if (*lineptr == NULL) |
| 97 | { |
| 98 | return -1; |
| 99 | } |
| 100 | /* Clear the rest of the line. */ |
| 101 | memset(*lineptr + *n, '\0', line_size); |
| 102 | *n += line_size; |
| 103 | } |
| 104 | |
| 105 | /* Push the result in the line. */ |
| 106 | (*lineptr)[indx++] = c; |
| 107 | |
| 108 | /* Bail out. */ |
| 109 | if (c == delim) |
| 110 | { |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | return (c == EOF) ? -1 : indx; |
| 115 | } |
| 116 | |
| 117 | static ssize_t |
| 118 | getline (char **lineptr, size_t *n, FILE *stream) |
| 119 | { |
| 120 | return getdelim (lineptr, n, '\n', stream); |
| 121 | } |
| 122 | #endif |
| 123 | #endif |