+2003-09-15 Paul Eggert <eggert@twinsun.com>
+
+ * lib/getndelim2.c (getndelim2): Don't trash errno when a read
+ fails, so that the caller gets the proper errno.
+
+ * readutmp.c (read_utmp): Likewise.
+ Check for fstat error. Close stream and free storage
+ when failing.
+
2003-09-14 Bruno Haible <bruno@clisp.org>
* fwriteerror.h: New file.
{
/* Here always *lineptr + *linesize == read_pos + nbytes_avail. */
- register int c = getc (stream);
+ register int c;
/* We always want at least one char left in the buffer, since we
always (unless we get an error while reading the first char)
}
}
- if (c == EOF || ferror (stream))
+ c = getc (stream);
+ if (c == EOF)
{
/* Return partial line, if any. */
if (read_pos == *lineptr)
if (utmp == NULL)
return 1;
- fstat (fileno (utmp), &file_stats);
+ if (fstat (fileno (utmp), &file_stats) != 0)
+ {
+ int e = errno;
+ fclose (utmp);
+ errno = e;
+ return 1;
+ }
size = file_stats.st_size;
- if (size > 0)
- buf = xmalloc (size);
- else
+ buf = xmalloc (size);
+ n_read = fread (buf, sizeof *buf, size / sizeof *buf, utmp);
+ if (ferror (utmp))
{
+ int e = errno;
+ free (buf);
fclose (utmp);
+ errno = e;
+ return 1;
+ }
+ if (fclose (utmp) != 0)
+ {
+ int e = errno;
+ free (buf);
+ errno = e;
return 1;
}
- /* Use < instead of != in case the utmp just grew. */
- n_read = fread (buf, 1, size, utmp);
- if (ferror (utmp) || fclose (utmp) == EOF
- || n_read < size)
- return 1;
-
- *n_entries = size / sizeof (STRUCT_UTMP);
+ *n_entries = n_read;
*utmp_buf = buf;
return 0;