| 301 | #ifdef __APPLE__ |
| 302 | #include <Availability.h> |
| 303 | #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1060 |
| 304 | |
| 305 | static const int line_size = 128; |
| 306 | |
| 307 | static ssize_t |
| 308 | getdelim (char **lineptr, size_t *n, int delim, FILE *stream); |
| 309 | |
| 310 | static ssize_t |
| 311 | getline (char **lineptr, size_t *n, FILE *stream); |
| 312 | |
| 313 | static ssize_t |
| 314 | getdelim (char **lineptr, size_t *n, int delim, FILE *stream) |
| 315 | { |
| 316 | int indx = 0; |
| 317 | int c; |
| 318 | |
| 319 | /* Sanity checks. */ |
| 320 | if (lineptr == NULL || n == NULL || stream == NULL) |
| 321 | return -1; |
| 322 | |
| 323 | /* Allocate the line the first time. */ |
| 324 | if (*lineptr == NULL) |
| 325 | { |
| 326 | *lineptr = malloc (line_size); |
| 327 | if (*lineptr == NULL) |
| 328 | return -1; |
| 329 | *n = line_size; |
| 330 | } |
| 331 | |
| 332 | /* Clear the line. */ |
| 333 | memset (*lineptr, '\0', *n); |
| 334 | |
| 335 | while ((c = getc (stream)) != EOF) |
| 336 | { |
| 337 | /* Check if more memory is needed. */ |
| 338 | if (indx >= *n) |
| 339 | { |
| 340 | *lineptr = realloc (*lineptr, *n + line_size); |
| 341 | if (*lineptr == NULL) |
| 342 | { |
| 343 | return -1; |
| 344 | } |
| 345 | /* Clear the rest of the line. */ |
| 346 | memset(*lineptr + *n, '\0', line_size); |
| 347 | *n += line_size; |
| 348 | } |
| 349 | |
| 350 | /* Push the result in the line. */ |
| 351 | (*lineptr)[indx++] = c; |
| 352 | |
| 353 | /* Bail out. */ |
| 354 | if (c == delim) |
| 355 | { |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | return (c == EOF) ? -1 : indx; |
| 360 | } |
| 361 | |
| 362 | static ssize_t |
| 363 | getline (char **lineptr, size_t *n, FILE *stream) |
| 364 | { |
| 365 | return getdelim (lineptr, n, '\n', stream); |
| 366 | } |
| 367 | #endif |
| 368 | #endif |
| 369 | |