Changeset 2536 for branches/libc-0.6


Ignore:
Timestamp:
Feb 6, 2006, 6:22:10 AM (20 years ago)
Author:
bird
Message:

#48: Quick implementation of the freopen(NULL) operation to make cat from coreutils happy.

Location:
branches/libc-0.6/src/emx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/libc-0.6/src/emx/ChangeLog.LIBC

    r2535 r2536  
    22
    33TODO: open replace on RAMFS fails with error 32!
     4
     52006-02-06: knut st. osmundsen <bird-gccos2-spam@anduin.net>
     6    - libc:
     7        o #48: Quick implementation of the freopen(NULL,,) operation
     8          to make cat from coreutils happy.
    49
    5102006-02-04: knut st. osmundsen <bird-gccos2-spam@anduin.net>
  • branches/libc-0.6/src/emx/src/lib/io/freopen.c

    r1604 r2536  
    66#include <string.h>
    77#include <share.h>
     8#include <errno.h>
     9#include <sys/fcntl.h>
    810#include <sys/builtin.h>        /* For <sys/fmutex.h> */
    911#include <sys/fmutex.h>
    1012#include <emx/io.h>
     13
     14#define FALSE   0
     15#define TRUE    1
    1116
    1217FILE *_STD(freopen) (const char *fname, const char *mode, FILE *stream)
     
    1520
    1621  STREAMV_LOCK;
    17   if (stream->_flags & _IOOPEN)
    18     { /* duplication of fclose(), but no _closestream lock. */
    19       int result;
    20       char buf[L_tmpnam];
     22  if (!fname)
     23    {
     24      /*
     25       * Change the stream mode.
     26       */
     27      /** @todo freopen(NULL,,) isn't 100% right, but it'll do for now I hope */
     28      result = stream;
     29      if (result->_flags & _IOOPEN)
     30        {
     31          char bt, ok;
     32          int omode = 0;
    2133
    22       result = EOF;
    23       if ((stream->_flags & _IOOPEN) && !(stream->_flags & _IOSPECIAL))
     34          switch (*mode)
     35            {
     36            case 'r':
     37              if (!(result->_flags & (_IORW | _IOREAD)))
     38                {
     39                  result = NULL;
     40                  errno = EINVAL;
     41                }
     42              omode = O_RDONLY;
     43              break;
     44            case 'w':
     45              if (!(result->_flags & (_IORW | _IOWRT)))
     46                {
     47                  result = NULL;
     48                  errno = EINVAL;
     49                }
     50              omode = O_WRONLY;
     51              break;
     52            case 'a':
     53              if (!(result->_flags & (_IORW | _IOWRT)))
     54                {
     55                  result = NULL;
     56                  errno = EINVAL;
     57                }
     58              omode = O_WRONLY | O_APPEND;
     59              break;
     60            default:
     61              errno = EINVAL;
     62              result = NULL;
     63              break;
     64            }
     65          ++mode; ok = TRUE; bt = FALSE;
     66          while (result && *mode != 0 && ok)
     67            {
     68              switch (*mode)
     69                {
     70                case 't':
     71                  if (bt)
     72                    ok = FALSE;
     73                  else
     74                    {
     75                      bt = TRUE;
     76                      omode |= O_TEXT;
     77                    }
     78                  break;
     79                case 'b':
     80                  if (bt)
     81                    ok = FALSE;
     82                  else
     83                    {
     84                      bt = TRUE;
     85                      omode |= O_BINARY;
     86                    }
     87                  break;
     88                case '+':
     89                   if (!(result->_flags & _IORW))
     90                     {
     91                       result = NULL;
     92                       errno = EINVAL;
     93                     }
     94                   omode &= ~(O_RDONLY|O_WRONLY);
     95                   omode |= O_RDWR;
     96                   break;
     97                default:
     98                  ok = FALSE;
     99                  break;
     100                }
     101              if (ok)
     102                  mode++;
     103            }
     104
     105          /*
     106           * Was the mode ok And does this stream have a handle?
     107           */
     108          if (result && !(result->_flags & _IOSPECIAL))
     109            {
     110              /* flush it and set the new mode */
     111              fflush(result);
     112              int oldmode = setmode(fileno(result), omode);
     113              if (      oldmode == -1
     114                  ||    (omode & O_APPEND) != (oldmode & O_APPEND))
     115                result = NULL;
     116            }
     117        }
     118      else
    24119        {
    25           result = 0;
    26           result = fflush (stream);
    27           if (close (stream->_handle) < 0)
    28             result = EOF;
    29           if (result == 0 && (stream->_flags & _IOTMP))
     120          errno = EBADF;
     121          result = NULL;
     122        }
     123    }
     124  else
     125    {
     126      if (stream->_flags & _IOOPEN)
     127        { /* duplication of fclose(), but no _closestream lock. */
     128          int result;
     129          char buf[L_tmpnam];
     130
     131          result = EOF;
     132          if ((stream->_flags & _IOOPEN) && !(stream->_flags & _IOSPECIAL))
    30133            {
    31               _itoa (stream->_tmpidx, buf, 10);
    32               strcat (buf, ".tmp");
    33               if (remove (buf) != 0)
     134              result = 0;
     135              result = fflush (stream);
     136              if (close (stream->_handle) < 0)
    34137                result = EOF;
     138              if (result == 0 && (stream->_flags & _IOTMP))
     139                {
     140                  _itoa (stream->_tmpidx, buf, 10);
     141                  strcat (buf, ".tmp");
     142                  if (remove (buf) != 0)
     143                    result = EOF;
     144                }
     145              if ((stream->_flags & _IOBUFMASK) == _IOBUFLIB)
     146                free (stream->_buffer);
    35147            }
    36           if ((stream->_flags & _IOBUFMASK) == _IOBUFLIB)
    37             free (stream->_buffer);
     148          _closestream (stream);
    38149        }
    39       _closestream (stream);
     150      result = _openstream (stream, fname, mode, SH_DENYNO, 0);
    40151    }
    41   result = _openstream (stream, fname, mode, SH_DENYNO, 0);
    42152  STREAMV_UNLOCK;
    43153  return result;
Note: See TracChangeset for help on using the changeset viewer.