[awesome-menu] rewrite getline by avoiding useless free and setting

errno in case of error.
This commit is contained in:
Arnaud Fontaine 2008-05-08 18:29:32 +01:00
parent c3e063d55e
commit 37087f61e0
1 changed files with 26 additions and 21 deletions

View File

@ -1139,31 +1139,36 @@ handle_kpress(xcb_key_press_event_t *e)
} }
#ifndef HAVE_GETLINE #ifndef HAVE_GETLINE
static int static ssize_t
getline(char **buf, size_t *len, FILE* in) my_getline(char **lineptr, size_t *n, FILE *stream)
{ {
int i; char *ret = NULL;
if (*buf) if(lineptr == NULL || n == NULL)
p_delete(buf);
if (*len)
*len = 0;
do
{ {
p_realloc(buf, *len + 10); errno = EINVAL;
(*len) += 10; return -1;
for (i = 0; i < 10 && !feof(in); i++)
{
(*buf)[*len - 10 + i] = getchar();
if ((*buf)[*len - 10 + i] == '\n'
|| (*buf)[*len - 10 + i] == '\r')
return (*len - 10 + i + 1);
}
} }
while(!feof(in));
return -1; if(*n == 0)
{
*lineptr = p_new(char, CHUNK_SIZE);
*n = CHUNK_SIZE;
}
ret = fgets(*lineptr, *n, stream);
while(ret != NULL && (*lineptr)[a_strlen (*lineptr) - 1] != '\n')
{
*n += CHUNK_SIZE;
*lineptr = realloc(*lineptr, *n);
ret = fgets(*lineptr + a_strlen(*lineptr), CHUNK_SIZE, stream);
}
if (ret == NULL)
return -1;
return a_strlen(*lineptr);
} }
#endif #endif