* libc/include/sys/stdio.h: New file.

* libc/sys/linux/sys/stdio.h: New file.
	* libc/include/stdio.h: Add declarations for flockfile,
	ftrylockfile, and funlockfile.  Include <sys/stdio.h>.
	* libc/stdio/clearerr.c: Add file locking.
	* libc/stdio/fclose.c: Likewise.
	* libc/stdio/feof.c: Likewise.
	* libc/stdio/ferror.c: Likewise.
	* libc/stdio/fflush.c: Likewise.
	* libc/stdio/fgetc.c: Likewise.
	* libc/stdio/fgetpos.c: Likewise.
	* libc/stdio/fgets.c: Likewise.
	* libc/stdio/fileno.c: Likewise.
	* libc/stdio/fputc.c: Likewise.
	* libc/stdio/fputs.c: Likewise.
	* libc/stdio/fread.c: Likewise.
	* libc/stdio/freopen.c: Likewise.
	* libc/stdio/fseek.c: Likewise.
	* libc/stdio/ftell.c: Likewise.
	* libc/stdio/fwrite.c: Likewise.
	* libc/stdio/getc.c: Likewise.
	* libc/stdio/putc.c: Likewise.
	* libc/stdio/setvbuf.c: Likewise.
	* libc/stdio/ungetc.c: Likewise.
	* libc/stdio/vfprintf.c: Likewise.
This commit is contained in:
Thomas Fitzsimmons 2002-05-08 00:12:49 +00:00
parent 735bb7e7d7
commit e71372faea
25 changed files with 222 additions and 35 deletions

View File

@ -1,3 +1,31 @@
2002-05-07 Thomas Fitzsimmons <fitzsim@redhat.com>
* libc/include/sys/stdio.h: New file.
* libc/sys/linux/sys/stdio.h: New file.
* libc/include/stdio.h: Add declarations for flockfile,
ftrylockfile, and funlockfile. Include <sys/stdio.h>.
* libc/stdio/clearerr.c: Add file locking.
* libc/stdio/fclose.c: Likewise.
* libc/stdio/feof.c: Likewise.
* libc/stdio/ferror.c: Likewise.
* libc/stdio/fflush.c: Likewise.
* libc/stdio/fgetc.c: Likewise.
* libc/stdio/fgetpos.c: Likewise.
* libc/stdio/fgets.c: Likewise.
* libc/stdio/fileno.c: Likewise.
* libc/stdio/fputc.c: Likewise.
* libc/stdio/fputs.c: Likewise.
* libc/stdio/fread.c: Likewise.
* libc/stdio/freopen.c: Likewise.
* libc/stdio/fseek.c: Likewise.
* libc/stdio/ftell.c: Likewise.
* libc/stdio/fwrite.c: Likewise.
* libc/stdio/getc.c: Likewise.
* libc/stdio/putc.c: Likewise.
* libc/stdio/setvbuf.c: Likewise.
* libc/stdio/ungetc.c: Likewise.
* libc/stdio/vfprintf.c: Likewise.
2002-05-06 Jeff Johnston <jjohnstn@redhat.com> 2002-05-06 Jeff Johnston <jjohnstn@redhat.com>
* libc/include/stdlib.h (a64l, l64a, _l64a_r): Added prototypes. * libc/include/stdlib.h (a64l, l64a, _l64a_r): Added prototypes.

View File

@ -47,6 +47,8 @@ extern "C" {
#include <sys/reent.h> #include <sys/reent.h>
#include <sys/stdio.h>
typedef _fpos_t fpos_t; typedef _fpos_t fpos_t;
typedef struct __sFILE FILE; typedef struct __sFILE FILE;
@ -220,6 +222,9 @@ FILE * _EXFUN(popen, (const char *, const char *));
int _EXFUN(putw, (int, FILE *)); int _EXFUN(putw, (int, FILE *));
void _EXFUN(setbuffer, (FILE *, char *, int)); void _EXFUN(setbuffer, (FILE *, char *, int));
int _EXFUN(setlinebuf, (FILE *)); int _EXFUN(setlinebuf, (FILE *));
void _EXFUN(flockfile, (FILE *));
int _EXFUN(ftrylockfile, (FILE *));
void _EXFUN(funlockfile, (FILE *));
#endif #endif
/* /*

View File

@ -0,0 +1,14 @@
#ifndef _NEWLIB_STDIO_H
#define _NEWLIB_STDIO_H
/* Internal locking macros, used to protect stdio functions. In the
general case, expand to nothing. */
#if !defined(_flockfile)
# define _flockfile(fp)
#endif
#if !defined(_funlockfile)
# define _funlockfile(fp)
#endif
#endif /* _NEWLIB_STDIO_H */

View File

@ -59,5 +59,7 @@ _VOID
_DEFUN (clearerr, (fp), _DEFUN (clearerr, (fp),
FILE * fp) FILE * fp)
{ {
_flockfile(fp);
__sclearerr (fp); __sclearerr (fp);
_funlockfile(fp);
} }

View File

@ -64,10 +64,15 @@ _DEFUN (fclose, (fp),
if (fp == NULL) if (fp == NULL)
return (0); /* on NULL */ return (0); /* on NULL */
_flockfile(fp);
CHECK_INIT (fp); CHECK_INIT (fp);
if (fp->_flags == 0) /* not open! */ if (fp->_flags == 0) /* not open! */
return (0); {
_funlockfile(fp);
return (0);
}
r = fp->_flags & __SWR ? fflush (fp) : 0; r = fp->_flags & __SWR ? fflush (fp) : 0;
if (fp->_close != NULL && (*fp->_close) (fp->_cookie) < 0) if (fp->_close != NULL && (*fp->_close) (fp->_cookie) < 0)
r = EOF; r = EOF;
@ -78,5 +83,6 @@ _DEFUN (fclose, (fp),
if (HASLB (fp)) if (HASLB (fp))
FREELB (fp); FREELB (fp);
fp->_flags = 0; /* release this FILE for reuse */ fp->_flags = 0; /* release this FILE for reuse */
_funlockfile(fp);
return (r); return (r);
} }

View File

@ -36,5 +36,9 @@ int
_DEFUN (feof, (fp), _DEFUN (feof, (fp),
FILE * fp) FILE * fp)
{ {
return __sfeof (fp); int result;
_flockfile(fp);
result = __sfeof (fp);
_funlockfile(fp);
return result;
} }

View File

@ -63,5 +63,9 @@ int
_DEFUN (ferror, (fp), _DEFUN (ferror, (fp),
FILE * fp) FILE * fp)
{ {
return __sferror (fp); int result;
_flockfile(fp);
result = __sferror (fp);
_funlockfile(fp);
return result;
} }

View File

@ -69,11 +69,16 @@ _DEFUN (fflush, (fp),
if (fp == NULL) if (fp == NULL)
return _fwalk (_REENT, fflush); return _fwalk (_REENT, fflush);
_flockfile(fp);
CHECK_INIT (fp); CHECK_INIT (fp);
t = fp->_flags; t = fp->_flags;
if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL) if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL)
return 0; {
_funlockfile(fp);
return 0;
}
n = fp->_p - p; /* write this much */ n = fp->_p - p; /* write this much */
/* /*
@ -89,11 +94,13 @@ _DEFUN (fflush, (fp),
t = (*fp->_write) (fp->_cookie, (char *) p, n); t = (*fp->_write) (fp->_cookie, (char *) p, n);
if (t <= 0) if (t <= 0)
{ {
fp->_flags |= __SERR; fp->_flags |= __SERR;
return EOF; _funlockfile(fp);
return EOF;
} }
p += t; p += t;
n -= t; n -= t;
} }
_funlockfile(fp);
return 0; return 0;
} }

View File

@ -42,5 +42,9 @@ int
_DEFUN (fgetc, (fp), _DEFUN (fgetc, (fp),
FILE * fp) FILE * fp)
{ {
return __sgetc (fp); int result;
_flockfile(fp);
result = __sgetc (fp);
_funlockfile(fp);
return result;
} }

View File

@ -53,9 +53,14 @@ _DEFUN (fgetpos, (fp, pos),
FILE * fp _AND FILE * fp _AND
fpos_t * pos) fpos_t * pos)
{ {
_flockfile(fp);
*pos = ftell (fp); *pos = ftell (fp);
if (*pos != -1) if (*pos != -1)
return 0; {
_funlockfile(fp);
return 0;
}
_funlockfile(fp);
return 1; return 1;
} }

View File

@ -80,6 +80,7 @@ _DEFUN (fgets, (buf, n, fp),
s = buf; s = buf;
_flockfile(fp);
#ifdef __SCLE #ifdef __SCLE
if (fp->_flags & __SCLE) if (fp->_flags & __SCLE)
{ {
@ -92,8 +93,12 @@ _DEFUN (fgets, (buf, n, fp),
break; break;
} }
if (c == EOF && s == buf) if (c == EOF && s == buf)
return NULL; {
_funlockfile(fp);
return NULL;
}
*s = 0; *s = 0;
_funlockfile(fp);
return buf; return buf;
} }
#endif #endif
@ -110,7 +115,10 @@ _DEFUN (fgets, (buf, n, fp),
{ {
/* EOF: stop with partial or no line */ /* EOF: stop with partial or no line */
if (s == buf) if (s == buf)
return 0; {
_funlockfile(fp);
return 0;
}
break; break;
} }
len = fp->_r; len = fp->_r;
@ -133,6 +141,7 @@ _DEFUN (fgets, (buf, n, fp),
fp->_p = t; fp->_p = t;
(void) memcpy ((_PTR) s, (_PTR) p, len); (void) memcpy ((_PTR) s, (_PTR) p, len);
s[len] = 0; s[len] = 0;
_funlockfile(fp);
return (buf); return (buf);
} }
fp->_r -= len; fp->_r -= len;
@ -142,5 +151,6 @@ _DEFUN (fgets, (buf, n, fp),
} }
while ((n -= len) != 0); while ((n -= len) != 0);
*s = 0; *s = 0;
_funlockfile(fp);
return buf; return buf;
} }

View File

@ -35,6 +35,10 @@ int
_DEFUN (fileno, (f), _DEFUN (fileno, (f),
FILE * f) FILE * f)
{ {
int result;
_flockfile(f);
CHECK_INIT (f); CHECK_INIT (f);
return __sfileno (f); result = __sfileno (f);
_funlockfile(f);
return result;
} }

View File

@ -47,5 +47,9 @@ _DEFUN (fputc, (ch, file),
int ch _AND int ch _AND
FILE * file) FILE * file)
{ {
return putc (ch, file); int result;
_flockfile(file);
result = putc (ch, file);
_funlockfile(file);
return result;
} }

View File

@ -60,6 +60,7 @@ _DEFUN (fputs, (s, fp),
char _CONST * s _AND char _CONST * s _AND
FILE * fp) FILE * fp)
{ {
int result;
struct __suio uio; struct __suio uio;
struct __siov iov; struct __siov iov;
@ -67,5 +68,8 @@ _DEFUN (fputs, (s, fp),
iov.iov_len = uio.uio_resid = strlen (s); iov.iov_len = uio.uio_resid = strlen (s);
uio.uio_iov = &iov; uio.uio_iov = &iov;
uio.uio_iovcnt = 1; uio.uio_iovcnt = 1;
return __sfvwrite (fp, &uio); _flockfile(fp);
result = __sfvwrite (fp, &uio);
_funlockfile(fp);
return result;
} }

View File

@ -122,6 +122,8 @@ _DEFUN (fread, (buf, size, count, fp),
if ((resid = count * size) == 0) if ((resid = count * size) == 0)
return 0; return 0;
_flockfile(fp);
if (fp->_r < 0) if (fp->_r < 0)
fp->_r = 0; fp->_r = 0;
total = resid; total = resid;
@ -139,8 +141,12 @@ _DEFUN (fread, (buf, size, count, fp),
/* no more input: return partial result */ /* no more input: return partial result */
#ifdef __SCLE #ifdef __SCLE
if (fp->_flags & __SCLE) if (fp->_flags & __SCLE)
return crlf(fp, buf, total-resid, 1) / size; {
_funlockfile(fp);
return crlf(fp, buf, total-resid, 1) / size;
}
#endif #endif
_funlockfile(fp);
return (total - resid) / size; return (total - resid) / size;
} }
} }
@ -149,7 +155,11 @@ _DEFUN (fread, (buf, size, count, fp),
fp->_p += resid; fp->_p += resid;
#ifdef __SCLE #ifdef __SCLE
if (fp->_flags & __SCLE) if (fp->_flags & __SCLE)
return crlf(fp, buf, total, 0) / size; {
_funlockfile(fp);
return crlf(fp, buf, total, 0) / size;
}
#endif #endif
_funlockfile(fp);
return count; return count;
} }

View File

@ -76,12 +76,15 @@ _DEFUN (freopen, (file, mode, fp),
int flags, oflags, e; int flags, oflags, e;
struct _reent *ptr; struct _reent *ptr;
_flockfile(fp);
CHECK_INIT (fp); CHECK_INIT (fp);
ptr = fp->_data; ptr = fp->_data;
if ((flags = __sflags (ptr, mode, &oflags)) == 0) if ((flags = __sflags (ptr, mode, &oflags)) == 0)
{ {
(void) fclose (fp); (void) fclose (fp);
_funlockfile(fp);
return NULL; return NULL;
} }
@ -137,6 +140,7 @@ _DEFUN (freopen, (file, mode, fp),
{ /* did not get it after all */ { /* did not get it after all */
fp->_flags = 0; /* set it free */ fp->_flags = 0; /* set it free */
ptr->_errno = e; /* restore in case _close clobbered */ ptr->_errno = e; /* restore in case _close clobbered */
_funlockfile(fp);
return NULL; return NULL;
} }
@ -153,5 +157,6 @@ _DEFUN (freopen, (file, mode, fp),
fp->_flags |= __SCLE; fp->_flags |= __SCLE;
#endif #endif
_funlockfile(fp);
return fp; return fp;
} }

View File

@ -96,6 +96,8 @@ fseek (fp, offset, whence)
struct stat st; struct stat st;
int havepos; int havepos;
_flockfile(fp);
/* Make sure stdio is set up. */ /* Make sure stdio is set up. */
CHECK_INIT (fp); CHECK_INIT (fp);
@ -115,6 +117,7 @@ fseek (fp, offset, whence)
if ((seekfn = fp->_seek) == NULL) if ((seekfn = fp->_seek) == NULL)
{ {
ptr->_errno = ESPIPE; /* ??? */ ptr->_errno = ESPIPE; /* ??? */
_funlockfile(fp);
return EOF; return EOF;
} }
@ -138,6 +141,7 @@ fseek (fp, offset, whence)
{ {
curoff = (*seekfn) (fp->_cookie, (fpos_t) 0, SEEK_CUR); curoff = (*seekfn) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
if (curoff == -1L) if (curoff == -1L)
_funlockfile(fp);
return EOF; return EOF;
} }
if (fp->_flags & __SRD) if (fp->_flags & __SRD)
@ -161,6 +165,7 @@ fseek (fp, offset, whence)
default: default:
ptr->_errno = EINVAL; ptr->_errno = EINVAL;
_funlockfile(fp);
return (EOF); return (EOF);
} }
@ -262,6 +267,7 @@ fseek (fp, offset, whence)
if (HASUB (fp)) if (HASUB (fp))
FREEUB (fp); FREEUB (fp);
fp->_flags &= ~__SEOF; fp->_flags &= ~__SEOF;
_funlockfile(fp);
return 0; return 0;
} }
@ -290,6 +296,7 @@ fseek (fp, offset, whence)
fp->_p += n; fp->_p += n;
fp->_r -= n; fp->_r -= n;
} }
_funlockfile(fp);
return 0; return 0;
/* /*
@ -299,7 +306,10 @@ fseek (fp, offset, whence)
dumb: dumb:
if (fflush (fp) || (*seekfn) (fp->_cookie, offset, whence) == POS_ERR) if (fflush (fp) || (*seekfn) (fp->_cookie, offset, whence) == POS_ERR)
return EOF; {
_funlockfile(fp);
return EOF;
}
/* success: clear EOF indicator and discard ungetc() data */ /* success: clear EOF indicator and discard ungetc() data */
if (HASUB (fp)) if (HASUB (fp))
FREEUB (fp); FREEUB (fp);
@ -307,5 +317,6 @@ dumb:
fp->_r = 0; fp->_r = 0;
/* fp->_w = 0; *//* unnecessary (I think...) */ /* fp->_w = 0; *//* unnecessary (I think...) */
fp->_flags &= ~__SEOF; fp->_flags &= ~__SEOF;
_funlockfile(fp);
return 0; return 0;
} }

View File

@ -80,6 +80,8 @@ _DEFUN (ftell, (fp),
{ {
fpos_t pos; fpos_t pos;
_flockfile(fp);
/* Ensure stdio is set up. */ /* Ensure stdio is set up. */
CHECK_INIT (fp); CHECK_INIT (fp);
@ -87,6 +89,7 @@ _DEFUN (ftell, (fp),
if (fp->_seek == NULL) if (fp->_seek == NULL)
{ {
fp->_data->_errno = ESPIPE; fp->_data->_errno = ESPIPE;
_funlockfile(fp);
return -1L; return -1L;
} }
@ -99,7 +102,10 @@ _DEFUN (ftell, (fp),
{ {
pos = (*fp->_seek) (fp->_cookie, (fpos_t) 0, SEEK_CUR); pos = (*fp->_seek) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
if (pos == -1L) if (pos == -1L)
return pos; {
_funlockfile(fp);
return pos;
}
} }
if (fp->_flags & __SRD) if (fp->_flags & __SRD)
{ {
@ -122,5 +128,6 @@ _DEFUN (ftell, (fp),
pos += fp->_p - fp->_bf._base; pos += fp->_p - fp->_bf._base;
} }
_funlockfile(fp);
return pos; return pos;
} }

View File

@ -98,7 +98,12 @@ _DEFUN (fwrite, (buf, size, count, fp),
* generally slow and since this occurs whenever size==0. * generally slow and since this occurs whenever size==0.
*/ */
_flockfile(fp);
if (__sfvwrite (fp, &uio) == 0) if (__sfvwrite (fp, &uio) == 0)
return count; {
_funlockfile(fp);
return count;
}
_funlockfile(fp);
return (n - uio.uio_resid) / size; return (n - uio.uio_resid) / size;
} }

View File

@ -74,7 +74,10 @@ int
getc (fp) getc (fp)
register FILE *fp; register FILE *fp;
{ {
int result;
_flockfile(fp);
/* CHECK_INIT is called (eventually) by __srefill. */ /* CHECK_INIT is called (eventually) by __srefill. */
result = __sgetc (fp);
return __sgetc (fp); _funlockfile(fp);
return result;
} }

View File

@ -78,7 +78,10 @@ putc (c, fp)
int c; int c;
register FILE *fp; register FILE *fp;
{ {
int result;
_flockfile(fp);
/* CHECK_INIT is (eventually) called by __swbuf. */ /* CHECK_INIT is (eventually) called by __swbuf. */
result = __sputc (c, fp);
return __sputc (c, fp); _funlockfile(fp);
return result;
} }

View File

@ -103,6 +103,9 @@ _DEFUN (setvbuf, (fp, buf, mode, size),
register size_t size) register size_t size)
{ {
int ret = 0; int ret = 0;
_flockfile(fp);
CHECK_INIT (fp); CHECK_INIT (fp);
/* /*
@ -111,7 +114,10 @@ _DEFUN (setvbuf, (fp, buf, mode, size),
*/ */
if ((mode != _IOFBF && mode != _IOLBF && mode != _IONBF) || (int)(_POINTER_INT) size < 0) if ((mode != _IOFBF && mode != _IOLBF && mode != _IONBF) || (int)(_POINTER_INT) size < 0)
return (EOF); {
_funlockfile(fp);
return (EOF);
}
/* /*
* Write current buffer, if any; drop read count, if any. * Write current buffer, if any; drop read count, if any.
@ -145,15 +151,16 @@ _DEFUN (setvbuf, (fp, buf, mode, size),
size = BUFSIZ; size = BUFSIZ;
} }
if (buf == NULL) if (buf == NULL)
{ {
/* Can't allocate it, let's try another approach */ /* Can't allocate it, let's try another approach */
nbf: nbf:
fp->_flags |= __SNBF; fp->_flags |= __SNBF;
fp->_w = 0; fp->_w = 0;
fp->_bf._base = fp->_p = fp->_nbuf; fp->_bf._base = fp->_p = fp->_nbuf;
fp->_bf._size = 1; fp->_bf._size = 1;
return (ret); _funlockfile(fp);
} return (ret);
}
fp->_flags |= __SMBF; fp->_flags |= __SMBF;
} }
/* /*
@ -186,5 +193,6 @@ nbf:
if (fp->_flags & __SWR) if (fp->_flags & __SWR)
fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : size; fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : size;
_funlockfile(fp);
return 0; return 0;
} }

View File

@ -73,6 +73,8 @@ ungetc (c, fp)
if (c == EOF) if (c == EOF)
return (EOF); return (EOF);
_flockfile(fp);
/* Ensure stdio has been initialized. /* Ensure stdio has been initialized.
??? Might be able to remove this as some other stdio routine should ??? Might be able to remove this as some other stdio routine should
have already been called to get the char we are un-getting. */ have already been called to get the char we are un-getting. */
@ -89,11 +91,17 @@ ungetc (c, fp)
* Otherwise, flush any current write stuff. * Otherwise, flush any current write stuff.
*/ */
if ((fp->_flags & __SRW) == 0) if ((fp->_flags & __SRW) == 0)
return EOF; {
_funlockfile(fp);
return EOF;
}
if (fp->_flags & __SWR) if (fp->_flags & __SWR)
{ {
if (fflush (fp)) if (fflush (fp))
return EOF; {
_funlockfile(fp);
return EOF;
}
fp->_flags &= ~__SWR; fp->_flags &= ~__SWR;
fp->_w = 0; fp->_w = 0;
fp->_lbfsize = 0; fp->_lbfsize = 0;
@ -110,9 +118,13 @@ ungetc (c, fp)
if (HASUB (fp)) if (HASUB (fp))
{ {
if (fp->_r >= fp->_ub._size && __submore (fp)) if (fp->_r >= fp->_ub._size && __submore (fp))
return EOF; {
_funlockfile(fp);
return EOF;
}
*--fp->_p = c; *--fp->_p = c;
fp->_r++; fp->_r++;
_funlockfile(fp);
return c; return c;
} }
@ -126,6 +138,7 @@ ungetc (c, fp)
{ {
fp->_p--; fp->_p--;
fp->_r++; fp->_r++;
_funlockfile(fp);
return c; return c;
} }
@ -141,5 +154,6 @@ ungetc (c, fp)
fp->_ubuf[sizeof (fp->_ubuf) - 1] = c; fp->_ubuf[sizeof (fp->_ubuf) - 1] = c;
fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1]; fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1];
fp->_r = 1; fp->_r = 1;
_funlockfile(fp);
return c; return c;
} }

View File

@ -298,8 +298,12 @@ _DEFUN (VFPRINTF, (fp, fmt0, ap),
_CONST char *fmt0 _AND _CONST char *fmt0 _AND
va_list ap) va_list ap)
{ {
int result;
_flockfile(fp);
CHECK_INIT (fp); CHECK_INIT (fp);
return _VFPRINTF_R (fp->_data, fp, fmt0, ap); result = _VFPRINTF_R (fp->_data, fp, fmt0, ap);
_funlockfile(fp);
return result;
} }
int int

View File

@ -0,0 +1,16 @@
#ifndef _NEWLIB_STDIO_H
#define _NEWLIB_STDIO_H
/* Internal locking macros, used to protect stdio functions. In the
linux case, expand to flockfile, and funlockfile, both defined in
LinuxThreads. */
#if !defined(__SINGLE_THREAD__)
# if !defined(_flockfile)
# define _flockfile(fp) /* FIXME: Uncomment when LinuxThreads is in: flockfile(fp) */
# endif
# if !defined(_funlockfile)
# define _funlockfile(fp) /* FIXME: Uncomment when LinuxThreads is in: funlockfile(fp) */
# endif
#endif /* __SINGLE_THREAD__ */
#endif /* _NEWLIB_STDIO_H */