|
- #include <stdio.h>
-
- #include "compat_impl.h"
-
-
- #if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__))
-
- #include <stdlib.h>
- #include <string.h>
-
- /*
- static inline size_t strnlen(const char* str, size_t size) {
- int len = 0, remain = size;
- while (remain > 0 && *str++) --remain;
- return (size - remain);
- }
- */
-
- char* strndup(const char *str, size_t size) {
- size_t len = strnlen(str, size);
- char* dup = malloc(len + 1);
- dup[len] = '\0';
- return memcpy(dup, str, len);
- }
-
- ssize_t getline(char **restrict lineptr, size_t *restrict n,
- FILE *restrict stream) {
- if (NULL == *lineptr || 0 == *n) {
- *n = 120;
- *lineptr = (char*)malloc(*n);
- if (NULL == *lineptr) return -1;
- }
-
- int chr = 0;
- ssize_t len = 0;
- char* ptr = *lineptr;
- while ((chr = fgetc(stream)) >= 0) {
- ++len;
- *ptr++ = chr;
- if (*n < len + 1) {
- char* more = realloc(*lineptr, *n * 2);
- if (NULL == more) return -1;
- *lineptr = more;
- *n *= 2;
- ptr = *lineptr + len;
- }
- if ('\n' == chr) break;
- }
-
- *ptr = '\0';
-
- return (len == 0 ? -1 : len);
- }
-
-
- #endif
|