xref: /titanic_44/usr/src/lib/libast/common/stdio/getdelim.c (revision 8339b41da2395f0525c46ceedfeb01961893ec44)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 #include "stdhdr.h"
25 
26 ssize_t
27 getdelim(char** sp, size_t* np, int delim, Sfio_t* f)
28 {
29 	ssize_t		m;
30 	ssize_t		n;
31 	ssize_t		k;
32 	ssize_t		p;
33 	uchar*		s;
34 	uchar*		ps;
35 	SFMTXDECL(f);
36 
37 	STDIO_INT(f, "getdelim", ssize_t, (char**, size_t*, int, Sfio_t*), (sp, np, delim, f))
38 
39 	SFMTXENTER(f, -1);
40 
41 	if(delim < 0 || delim > 255 || !sp || !np) /* bad parameters */
42 		SFMTXRETURN(f, -1);
43 
44 	if(f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0)
45 		SFMTXRETURN(f, -1);
46 
47 	SFLOCK(f,0);
48 
49 	if(!(s = (uchar*)(*sp)) || (n = *np) < 0)
50 		{ s = NIL(uchar*); n = 0; }
51 	for(m = 0;; )
52 	{	/* read new data */
53 		if((p = f->endb - (ps = f->next)) <= 0 )
54 		{	f->getr = delim;
55 			f->mode |= SF_RC;
56 			if(SFRPEEK(f,ps,p) <= 0)
57 			{	m = -1;
58 				break;
59 			}
60 		}
61 
62 		for(k = 0; k < p; ++k) /* find the delimiter */
63 		{	if(ps[k] == delim)
64 			{	k += 1; /* include delim in copying */
65 				break;
66 			}
67 		}
68 
69 		if((m+k+1) >= n ) /* make sure there is space */
70 		{	n = ((m+k+15)/8)*8;
71 			if(!(s = (uchar*)realloc(s, n)) )
72 			{	*sp = 0; *np = 0;
73 				m = -1;
74 				break;
75 			}
76 			*sp = (char*)s; *np = n;
77 		}
78 
79 		memcpy(s+m, ps, k); m += k;
80 		f->next = ps+k; /* skip copied data in buffer */
81 
82 		if(s[m-1] == delim)
83 		{	s[m] = 0; /* 0-terminated */
84 			break;
85 		}
86 	}
87 
88 	SFOPEN(f,0);
89 	SFMTXRETURN(f,m);
90 }
91 
92 ssize_t
93 __getdelim(char** sp, size_t* np, int delim, Sfio_t* f)
94 {
95 	return getdelim(sp, np, delim, f);
96 }
97