OSDN Git Service

* libc/include/sys/stdio.h: New file.
authorfitzsim <fitzsim>
Wed, 8 May 2002 00:12:46 +0000 (00:12 +0000)
committerfitzsim <fitzsim>
Wed, 8 May 2002 00:12:46 +0000 (00:12 +0000)
* 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.

25 files changed:
newlib/ChangeLog
newlib/libc/include/stdio.h
newlib/libc/include/sys/stdio.h [new file with mode: 0644]
newlib/libc/stdio/clearerr.c
newlib/libc/stdio/fclose.c
newlib/libc/stdio/feof.c
newlib/libc/stdio/ferror.c
newlib/libc/stdio/fflush.c
newlib/libc/stdio/fgetc.c
newlib/libc/stdio/fgetpos.c
newlib/libc/stdio/fgets.c
newlib/libc/stdio/fileno.c
newlib/libc/stdio/fputc.c
newlib/libc/stdio/fputs.c
newlib/libc/stdio/fread.c
newlib/libc/stdio/freopen.c
newlib/libc/stdio/fseek.c
newlib/libc/stdio/ftell.c
newlib/libc/stdio/fwrite.c
newlib/libc/stdio/getc.c
newlib/libc/stdio/putc.c
newlib/libc/stdio/setvbuf.c
newlib/libc/stdio/ungetc.c
newlib/libc/stdio/vfprintf.c
newlib/libc/sys/linux/sys/stdio.h [new file with mode: 0644]

index dfc3087..2158397 100644 (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>
 
         *  libc/include/stdlib.h (a64l, l64a, _l64a_r): Added prototypes.
index 9fb9451..767438f 100644 (file)
@@ -47,6 +47,8 @@ extern "C" {
 
 #include <sys/reent.h>
 
+#include <sys/stdio.h>
+
 typedef _fpos_t fpos_t;
 
 typedef struct __sFILE FILE;
@@ -220,6 +222,9 @@ FILE *  _EXFUN(popen, (const char *, const char *));
 int    _EXFUN(putw, (int, FILE *));
 void    _EXFUN(setbuffer, (FILE *, char *, int));
 int    _EXFUN(setlinebuf, (FILE *));
+void    _EXFUN(flockfile, (FILE *));
+int     _EXFUN(ftrylockfile, (FILE *));
+void    _EXFUN(funlockfile, (FILE *));
 #endif
 
 /*
diff --git a/newlib/libc/include/sys/stdio.h b/newlib/libc/include/sys/stdio.h
new file mode 100644 (file)
index 0000000..8177322
--- /dev/null
@@ -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 */
index 861d9a2..6a1b0af 100644 (file)
@@ -59,5 +59,7 @@ _VOID
 _DEFUN (clearerr, (fp),
        FILE * fp)
 {
+  _flockfile(fp);
   __sclearerr (fp);
+  _funlockfile(fp);
 }
index 3266d8a..ad3d1e8 100644 (file)
@@ -64,10 +64,15 @@ _DEFUN (fclose, (fp),
   if (fp == NULL)
     return (0);                        /* on NULL */
 
+  _flockfile(fp);
+  
   CHECK_INIT (fp);
 
   if (fp->_flags == 0)         /* not open! */
-    return (0);
+    {
+      _funlockfile(fp);
+      return (0);
+    }
   r = fp->_flags & __SWR ? fflush (fp) : 0;
   if (fp->_close != NULL && (*fp->_close) (fp->_cookie) < 0)
     r = EOF;
@@ -78,5 +83,6 @@ _DEFUN (fclose, (fp),
   if (HASLB (fp))
     FREELB (fp);
   fp->_flags = 0;              /* release this FILE for reuse */
+  _funlockfile(fp);
   return (r);
 }
index 55f151b..b7981bd 100644 (file)
@@ -36,5 +36,9 @@ int
 _DEFUN (feof, (fp),
        FILE * fp)
 {
-  return __sfeof (fp);
+  int result;
+  _flockfile(fp);
+  result = __sfeof (fp);
+  _funlockfile(fp);
+  return result;
 }
index 19fa78d..ef17fd8 100644 (file)
@@ -63,5 +63,9 @@ int
 _DEFUN (ferror, (fp),
        FILE * fp)
 {
-  return __sferror (fp);
+  int result;
+  _flockfile(fp);
+  result = __sferror (fp);
+  _funlockfile(fp);
+  return result;
 }
index 635e5a7..866bc83 100644 (file)
@@ -69,11 +69,16 @@ _DEFUN (fflush, (fp),
   if (fp == NULL)
     return _fwalk (_REENT, fflush);
 
+  _flockfile(fp);
+
   CHECK_INIT (fp);
 
   t = fp->_flags;
   if ((t & __SWR) == 0 || (p = fp->_bf._base) == NULL)
-    return 0;
+    {
+      _funlockfile(fp);
+      return 0;
+    }
   n = fp->_p - p;              /* write this much */
 
   /*
@@ -89,11 +94,13 @@ _DEFUN (fflush, (fp),
       t = (*fp->_write) (fp->_cookie, (char *) p, n);
       if (t <= 0)
        {
-         fp->_flags |= __SERR;
-         return EOF;
+          fp->_flags |= __SERR;
+          _funlockfile(fp);
+          return EOF;
        }
       p += t;
       n -= t;
     }
+  _funlockfile(fp);
   return 0;
 }
index 3d3d2c4..1160b67 100644 (file)
@@ -42,5 +42,9 @@ int
 _DEFUN (fgetc, (fp),
        FILE * fp)
 {
-  return __sgetc (fp);
+  int result;
+  _flockfile(fp);
+  result = __sgetc (fp);
+  _funlockfile(fp);
+  return result;
 }
index 214021a..ed6f5cf 100644 (file)
@@ -53,9 +53,14 @@ _DEFUN (fgetpos, (fp, pos),
        FILE * fp _AND
        fpos_t * pos)
 {
+  _flockfile(fp);
   *pos = ftell (fp);
 
   if (*pos != -1)
-    return 0;
+    {
+      _funlockfile(fp);
+      return 0;
+    }
+  _funlockfile(fp);
   return 1;
 }
index d395d33..46e1900 100644 (file)
@@ -80,6 +80,7 @@ _DEFUN (fgets, (buf, n, fp),
 
   s = buf;
 
+  _flockfile(fp);
 #ifdef __SCLE
   if (fp->_flags & __SCLE)
     {
@@ -92,8 +93,12 @@ _DEFUN (fgets, (buf, n, fp),
            break;
        }
       if (c == EOF && s == buf)
-       return NULL;
+        {
+          _funlockfile(fp);
+          return NULL;
+        }
       *s = 0;
+      _funlockfile(fp);
       return buf;
     }
 #endif
@@ -110,7 +115,10 @@ _DEFUN (fgets, (buf, n, fp),
            {
              /* EOF: stop with partial or no line */
              if (s == buf)
-               return 0;
+                {
+                  _funlockfile(fp);
+                  return 0;
+                }
              break;
            }
          len = fp->_r;
@@ -133,6 +141,7 @@ _DEFUN (fgets, (buf, n, fp),
          fp->_p = t;
          (void) memcpy ((_PTR) s, (_PTR) p, len);
          s[len] = 0;
+          _funlockfile(fp);
          return (buf);
        }
       fp->_r -= len;
@@ -142,5 +151,6 @@ _DEFUN (fgets, (buf, n, fp),
     }
   while ((n -= len) != 0);
   *s = 0;
+  _funlockfile(fp);
   return buf;
 }
index d578c3e..c9ecbb4 100644 (file)
@@ -35,6 +35,10 @@ int
 _DEFUN (fileno, (f),
        FILE * f)
 {
+  int result;
+  _flockfile(f);
   CHECK_INIT (f);
-  return __sfileno (f);
+  result = __sfileno (f);
+  _funlockfile(f);
+  return result;
 }
index 966a0db..b945095 100644 (file)
@@ -47,5 +47,9 @@ _DEFUN (fputc, (ch, file),
        int ch _AND
        FILE * file)
 {
-  return putc (ch, file);
+  int result;
+   _flockfile(file);
+  result = putc (ch, file);
+  _funlockfile(file);
+  return result;
 }
index 62ce98f..78758ce 100644 (file)
@@ -60,6 +60,7 @@ _DEFUN (fputs, (s, fp),
        char _CONST * s _AND
        FILE * fp)
 {
+  int result;
   struct __suio uio;
   struct __siov iov;
 
@@ -67,5 +68,8 @@ _DEFUN (fputs, (s, fp),
   iov.iov_len = uio.uio_resid = strlen (s);
   uio.uio_iov = &iov;
   uio.uio_iovcnt = 1;
-  return __sfvwrite (fp, &uio);
+  _flockfile(fp);
+  result = __sfvwrite (fp, &uio);
+  _funlockfile(fp);
+  return result;
 }
index 38f791b..faca537 100644 (file)
@@ -122,6 +122,8 @@ _DEFUN (fread, (buf, size, count, fp),
 
   if ((resid = count * size) == 0)
     return 0;
+
+  _flockfile(fp);
   if (fp->_r < 0)
     fp->_r = 0;
   total = resid;
@@ -139,8 +141,12 @@ _DEFUN (fread, (buf, size, count, fp),
          /* no more input: return partial result */
 #ifdef __SCLE
           if (fp->_flags & __SCLE)
-            return crlf(fp, buf, total-resid, 1) / size;
+            {
+              _funlockfile(fp);
+              return crlf(fp, buf, total-resid, 1) / size;
+            }
 #endif
+          _funlockfile(fp);
          return (total - resid) / size;
        }
     }
@@ -149,7 +155,11 @@ _DEFUN (fread, (buf, size, count, fp),
   fp->_p += resid;
 #ifdef __SCLE
   if (fp->_flags & __SCLE)
-    return crlf(fp, buf, total, 0) / size;
+    {
+      _funlockfile(fp);
+      return crlf(fp, buf, total, 0) / size;
+    }
 #endif
+  _funlockfile(fp);
   return count;
 }
index 63f5833..ea61f04 100644 (file)
@@ -76,12 +76,15 @@ _DEFUN (freopen, (file, mode, fp),
   int flags, oflags, e;
   struct _reent *ptr;
 
+  _flockfile(fp);
+
   CHECK_INIT (fp);
   ptr = fp->_data;
 
   if ((flags = __sflags (ptr, mode, &oflags)) == 0)
     {
       (void) fclose (fp);
+      _funlockfile(fp);
       return NULL;
     }
 
@@ -137,6 +140,7 @@ _DEFUN (freopen, (file, mode, fp),
     {                          /* did not get it after all */
       fp->_flags = 0;          /* set it free */
       ptr->_errno = e;         /* restore in case _close clobbered */
+      _funlockfile(fp);
       return NULL;
     }
 
@@ -153,5 +157,6 @@ _DEFUN (freopen, (file, mode, fp),
     fp->_flags |= __SCLE;
 #endif
 
+  _funlockfile(fp);
   return fp;
 }
index 1b72989..10c6c53 100644 (file)
@@ -96,6 +96,8 @@ fseek (fp, offset, whence)
   struct stat st;
   int havepos;
 
+  _flockfile(fp);
+
   /* Make sure stdio is set up.  */
 
   CHECK_INIT (fp);
@@ -115,6 +117,7 @@ fseek (fp, offset, whence)
   if ((seekfn = fp->_seek) == NULL)
     {
       ptr->_errno = ESPIPE;    /* ??? */
+      _funlockfile(fp);
       return EOF;
     }
 
@@ -138,6 +141,7 @@ fseek (fp, offset, whence)
        {
          curoff = (*seekfn) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
          if (curoff == -1L)
+            _funlockfile(fp);
            return EOF;
        }
       if (fp->_flags & __SRD)
@@ -161,6 +165,7 @@ fseek (fp, offset, whence)
 
     default:
       ptr->_errno = EINVAL;
+      _funlockfile(fp);
       return (EOF);
     }
 
@@ -262,6 +267,7 @@ fseek (fp, offset, whence)
       if (HASUB (fp))
        FREEUB (fp);
       fp->_flags &= ~__SEOF;
+      _funlockfile(fp);
       return 0;
     }
 
@@ -290,6 +296,7 @@ fseek (fp, offset, whence)
       fp->_p += n;
       fp->_r -= n;
     }
+  _funlockfile(fp);
   return 0;
 
   /*
@@ -299,7 +306,10 @@ fseek (fp, offset, whence)
 
 dumb:
   if (fflush (fp) || (*seekfn) (fp->_cookie, offset, whence) == POS_ERR)
-    return EOF;
+    {
+      _funlockfile(fp);
+      return EOF;
+    }
   /* success: clear EOF indicator and discard ungetc() data */
   if (HASUB (fp))
     FREEUB (fp);
@@ -307,5 +317,6 @@ dumb:
   fp->_r = 0;
   /* fp->_w = 0; *//* unnecessary (I think...) */
   fp->_flags &= ~__SEOF;
+  _funlockfile(fp);
   return 0;
 }
index 6a80618..8066acc 100644 (file)
@@ -80,6 +80,8 @@ _DEFUN (ftell, (fp),
 {
   fpos_t pos;
 
+  _flockfile(fp);
+
   /* Ensure stdio is set up.  */
 
   CHECK_INIT (fp);
@@ -87,6 +89,7 @@ _DEFUN (ftell, (fp),
   if (fp->_seek == NULL)
     {
       fp->_data->_errno = ESPIPE;
+      _funlockfile(fp);
       return -1L;
     }
 
@@ -99,7 +102,10 @@ _DEFUN (ftell, (fp),
     {
       pos = (*fp->_seek) (fp->_cookie, (fpos_t) 0, SEEK_CUR);
       if (pos == -1L)
-       return pos;
+        {
+          _funlockfile(fp);
+          return pos;
+        }
     }
   if (fp->_flags & __SRD)
     {
@@ -122,5 +128,6 @@ _DEFUN (ftell, (fp),
       pos += fp->_p - fp->_bf._base;
     }
 
+  _funlockfile(fp);
   return pos;
 }
index 642fb27..1c91632 100644 (file)
@@ -98,7 +98,12 @@ _DEFUN (fwrite, (buf, size, count, fp),
    * generally slow and since this occurs whenever size==0.
    */
 
+  _flockfile(fp);
   if (__sfvwrite (fp, &uio) == 0)
-    return count;
+    {
+      _funlockfile(fp);
+      return count;
+    }
+  _funlockfile(fp);
   return (n - uio.uio_resid) / size;
 }
index 4b25097..f7a2cac 100644 (file)
@@ -74,7 +74,10 @@ int
 getc (fp)
      register FILE *fp;
 {
+  int result;
+  _flockfile(fp);
   /* CHECK_INIT is called (eventually) by __srefill.  */
-
-  return __sgetc (fp);
+  result = __sgetc (fp);
+  _funlockfile(fp);
+  return result;
 }
index 27a7a42..7e53868 100644 (file)
@@ -78,7 +78,10 @@ putc (c, fp)
      int c;
      register FILE *fp;
 {
+  int result;
+  _flockfile(fp);
   /* CHECK_INIT is (eventually) called by __swbuf.  */
-
-  return __sputc (c, fp);
+  result = __sputc (c, fp);
+  _funlockfile(fp);
+  return result;
 }
index 357ea51..d44cdba 100644 (file)
@@ -103,6 +103,9 @@ _DEFUN (setvbuf, (fp, buf, mode, size),
        register size_t size)
 {
   int ret = 0;
+
+  _flockfile(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)
-    return (EOF);
+    {
+      _funlockfile(fp);
+      return (EOF);
+    }
 
   /*
    * Write current buffer, if any; drop read count, if any.
@@ -145,15 +151,16 @@ _DEFUN (setvbuf, (fp, buf, mode, size),
          size = BUFSIZ;
        }
       if (buf == NULL)
-       {
-         /* Can't allocate it, let's try another approach */
+        {
+          /* Can't allocate it, let's try another approach */
 nbf:
-         fp->_flags |= __SNBF;
-         fp->_w = 0;
-         fp->_bf._base = fp->_p = fp->_nbuf;
-         fp->_bf._size = 1;
-         return (ret);
-       }
+          fp->_flags |= __SNBF;
+          fp->_w = 0;
+          fp->_bf._base = fp->_p = fp->_nbuf;
+          fp->_bf._size = 1;
+          _funlockfile(fp);
+          return (ret);
+        }
       fp->_flags |= __SMBF;
     }
   /*
@@ -186,5 +193,6 @@ nbf:
   if (fp->_flags & __SWR)
     fp->_w = fp->_flags & (__SLBF | __SNBF) ? 0 : size;
 
+  _funlockfile(fp);
   return 0;
 }
index 418717e..9e54e5e 100644 (file)
@@ -73,6 +73,8 @@ ungetc (c, fp)
   if (c == EOF)
     return (EOF);
 
+  _flockfile(fp);
+  
   /* Ensure stdio has been initialized.
      ??? Might be able to remove this as some other stdio routine should
      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.
        */
       if ((fp->_flags & __SRW) == 0)
-       return EOF;
+        {
+          _funlockfile(fp);
+          return EOF;
+        }
       if (fp->_flags & __SWR)
        {
          if (fflush (fp))
-           return EOF;
+            {
+              _funlockfile(fp);
+              return EOF;
+            }
          fp->_flags &= ~__SWR;
          fp->_w = 0;
          fp->_lbfsize = 0;
@@ -110,9 +118,13 @@ ungetc (c, fp)
   if (HASUB (fp))
     {
       if (fp->_r >= fp->_ub._size && __submore (fp))
-       return EOF;
+        {
+          _funlockfile(fp);
+          return EOF;
+        }
       *--fp->_p = c;
       fp->_r++;
+      _funlockfile(fp);
       return c;
     }
 
@@ -126,6 +138,7 @@ ungetc (c, fp)
     {
       fp->_p--;
       fp->_r++;
+      _funlockfile(fp);
       return c;
     }
 
@@ -141,5 +154,6 @@ ungetc (c, fp)
   fp->_ubuf[sizeof (fp->_ubuf) - 1] = c;
   fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - 1];
   fp->_r = 1;
+  _funlockfile(fp);
   return c;
 }
index 302b5aa..27cc6d9 100644 (file)
@@ -298,8 +298,12 @@ _DEFUN (VFPRINTF, (fp, fmt0, ap),
        _CONST char *fmt0 _AND
        va_list ap)
 {
+  int result;
+  _flockfile(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 
diff --git a/newlib/libc/sys/linux/sys/stdio.h b/newlib/libc/sys/linux/sys/stdio.h
new file mode 100644 (file)
index 0000000..859406f
--- /dev/null
@@ -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 */