00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <config.h>
00023
00024 #ifdef FREEBSD
00025
00026 #include <stdio.h>
00027
00028 #include <sys/types.h>
00029 #include <limits.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032
00033 #ifndef SIZE_MAX
00034 # define SIZE_MAX ((size_t) -1)
00035 #endif
00036 #ifndef SSIZE_MAX
00037 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
00038 #endif
00039 #if !HAVE_FLOCKFILE
00040 # undef flockfile
00041 # define flockfile(x) ((void) 0)
00042 #endif
00043 #if !HAVE_FUNLOCKFILE
00044 # undef funlockfile
00045 # define funlockfile(x) ((void) 0)
00046 #endif
00047
00048
00049 #ifndef EOVERFLOW
00050 # define EOVERFLOW E2BIG
00051 #endif
00052
00053
00054
00055
00056
00057
00058
00059 ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
00060 {
00061 ssize_t result;
00062 size_t cur_len = 0;
00063
00064 if (lineptr == NULL || n == NULL || fp == NULL)
00065 {
00066 errno = EINVAL;
00067 return -1;
00068 }
00069
00070 flockfile (fp);
00071
00072 if (*lineptr == NULL || *n == 0)
00073 {
00074 *n = 120;
00075 *lineptr = (char *) realloc (*lineptr, *n);
00076 if (*lineptr == NULL)
00077 {
00078 result = -1;
00079 goto unlock_return;
00080 }
00081 }
00082
00083 for (;;)
00084 {
00085 int i;
00086
00087 i = getc (fp);
00088 if (i == EOF)
00089 {
00090 result = -1;
00091 break;
00092 }
00093
00094
00095 if (cur_len + 1 >= *n)
00096 {
00097 size_t needed_max =
00098 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
00099 size_t needed = 2 * *n + 1;
00100 char *new_lineptr;
00101
00102 if (needed_max < needed)
00103 needed = needed_max;
00104 if (cur_len + 1 >= needed)
00105 {
00106 result = -1;
00107 errno = EOVERFLOW;
00108 goto unlock_return;
00109 }
00110
00111 new_lineptr = (char *) realloc (*lineptr, needed);
00112 if (new_lineptr == NULL)
00113 {
00114 result = -1;
00115 goto unlock_return;
00116 }
00117
00118 *lineptr = new_lineptr;
00119 *n = needed;
00120 }
00121
00122 (*lineptr)[cur_len] = i;
00123 cur_len++;
00124
00125 if (i == delimiter)
00126 break;
00127 }
00128 (*lineptr)[cur_len] = '\0';
00129 result = cur_len ? (ssize_t)cur_len : result;
00130
00131 unlock_return:
00132 funlockfile (fp);
00133
00134 return result;
00135 }
00136
00137 #endif