1 /***********************************************************************
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 *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
27 getdelim(char** sp
, size_t* np
, int delim
, Sfio_t
* f
)
37 STDIO_INT(f
, "getdelim", ssize_t
, (char**, size_t*, int, Sfio_t
*), (sp
, np
, delim
, f
))
41 if(delim
< 0 || delim
> 255 || !sp
|| !np
) /* bad parameters */
44 if(f
->mode
!= SF_READ
&& _sfmode(f
,SF_READ
,0) < 0)
49 if(!(s
= (uchar
*)(*sp
)) || (n
= *np
) < 0)
50 { s
= NIL(uchar
*); n
= 0; }
53 if((p
= f
->endb
- (ps
= f
->next
)) <= 0 )
56 if(SFRPEEK(f
,ps
,p
) <= 0)
62 for(k
= 0; k
< p
; ++k
) /* find the delimiter */
64 { k
+= 1; /* include delim in copying */
69 if((m
+k
+1) >= n
) /* make sure there is space */
71 if(!(s
= (uchar
*)realloc(s
, n
)) )
76 *sp
= (char*)s
; *np
= n
;
79 memcpy(s
+m
, ps
, k
); m
+= k
;
80 f
->next
= ps
+k
; /* skip copied data in buffer */
83 { s
[m
] = 0; /* 0-terminated */
93 __getdelim(char** sp
, size_t* np
, int delim
, Sfio_t
* f
)
95 return getdelim(sp
, np
, delim
, f
);