version 1.12, 2007/10/16 20:19:27 |
version 1.13, 2008/10/08 17:26:47 |
|
|
* together. Empty strings and files are ignored. |
* together. Empty strings and files are ignored. |
*/ |
*/ |
char * |
char * |
cu_fgets(char *buf, int n) |
cu_fgets(char **outbuf, size_t *outlen) |
{ |
{ |
static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; |
static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; |
static FILE *f; /* Current open file */ |
static FILE *f; /* Current open file */ |
static char *s; /* Current pointer inside string */ |
static char *s; /* Current pointer inside string */ |
static char string_ident[30]; |
static char string_ident[30]; |
|
size_t len; |
char *p; |
char *p; |
|
|
again: |
again: |
|
|
goto again; |
goto again; |
} |
} |
case ST_FILE: |
case ST_FILE: |
if ((p = fgets(buf, n, f)) != NULL) { |
if ((p = fgetln(f, &len)) != NULL) { |
linenum++; |
linenum++; |
if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') |
if (len > 0 && p[len-1] == '\n') |
|
len--; |
|
if (len >= *outlen) { |
|
do { |
|
*outlen *= 2; |
|
} while (len >= *outlen); |
|
free(*outbuf); |
|
*outbuf = xmalloc(*outlen); |
|
} |
|
memcpy(*outbuf, p, len); |
|
(*outbuf)[len] = '\0'; |
|
if (linenum == 1 && p[0] == '#' && p[1] == 'n') |
nflag = 1; |
nflag = 1; |
return (p); |
return (*outbuf); |
} |
} |
script = script->next; |
script = script->next; |
(void)fclose(f); |
(void)fclose(f); |
|
|
case ST_STRING: |
case ST_STRING: |
if (linenum == 0 && s[0] == '#' && s[1] == 'n') |
if (linenum == 0 && s[0] == '#' && s[1] == 'n') |
nflag = 1; |
nflag = 1; |
p = buf; |
p = *outbuf; |
|
len = *outlen; |
for (;;) { |
for (;;) { |
if (n-- <= 1) { |
if (len-- <= 1) { |
*p = '\0'; |
*outbuf = xrealloc(*outbuf, *outlen * 2); |
linenum++; |
p = *outbuf + *outlen - len - 1; |
return (buf); |
len += *outlen; |
|
*outlen *= 2; |
} |
} |
switch (*s) { |
switch (*s) { |
case '\0': |
case '\0': |
|
|
script = script->next; |
script = script->next; |
*p = '\0'; |
*p = '\0'; |
linenum++; |
linenum++; |
return (buf); |
return (*outbuf); |
} |
} |
case '\n': |
case '\n': |
*p++ = '\n'; |
*p++ = '\n'; |
*p = '\0'; |
*p = '\0'; |
s++; |
s++; |
linenum++; |
linenum++; |
return (buf); |
return (*outbuf); |
default: |
default: |
*p++ = *s++; |
*p++ = *s++; |
} |
} |