ib_getlin.c (2133B)
1 /* 2 * Copyright (c) 2003 Gunnar Ritter 3 * 4 * This software is provided 'as-is', without any express or implied 5 * warranty. In no event will the authors be held liable for any damages 6 * arising from the use of this software. 7 * 8 * Permission is granted to anyone to use this software for any purpose, 9 * including commercial applications, and to alter it and redistribute 10 * it freely, subject to the following restrictions: 11 * 12 * 1. The origin of this software must not be misrepresented; you must not 13 * claim that you wrote the original software. If you use this software 14 * in a product, an acknowledgment in the product documentation would be 15 * appreciated but is not required. 16 * 17 * 2. Altered source versions must be plainly marked as such, and must not be 18 * misrepresented as being the original software. 19 * 20 * 3. This notice may not be removed or altered from any source distribution. 21 */ 22 /* Sccsid @(#)ib_getlin.c 1.2 (gritter) 4/17/03 */ 23 24 #include <string.h> 25 #include <stdlib.h> 26 #include "iblok.h" 27 28 size_t 29 ib_getlin(struct iblok *ip, char **line, size_t *alcd, 30 void *(*reallc)(void *, size_t)) 31 { 32 char *nl; 33 size_t sz, llen = 0, nllen; 34 35 for (;;) { 36 if (ip->ib_cur >= ip->ib_end) { 37 if (ip->ib_incompl) { 38 ip->ib_incompl = 0; 39 return 0; 40 } 41 if (ib_read(ip) == EOF) { 42 if (llen) { 43 ip->ib_incompl++; 44 (*line)[llen] = '\0'; 45 return llen; 46 } else 47 return 0; 48 } 49 /* 50 * ib_read() advances ib_cur since *ib_cur++ gives 51 * better performance than *++ib_cur for ib_get(). 52 * Go back again. 53 */ 54 ip->ib_cur--; 55 } 56 sz = ip->ib_end - ip->ib_cur; 57 if ((nl = memchr(ip->ib_cur, '\n', sz)) != NULL) { 58 sz = nl - ip->ib_cur + 1; 59 if ((nllen = llen + sz + 1) > *alcd) { 60 *line = reallc(*line, nllen); 61 *alcd = nllen; 62 } 63 memcpy(&(*line)[llen], ip->ib_cur, sz); 64 (*line)[llen + sz] = '\0'; 65 ip->ib_cur = nl + 1; 66 return llen + sz; 67 } 68 if ((nllen = llen + sz + 1) > *alcd) { 69 *line = reallc(*line, nllen); 70 *alcd = nllen; 71 } 72 memcpy(&(*line)[llen], ip->ib_cur, sz); 73 llen += sz; 74 ip->ib_cur = ip->ib_end; 75 } 76 /*NOTREACHED*/ 77 return 0; 78 }