- Timestamp:
- Feb 7, 2006, 1:55:41 AM (20 years ago)
- File:
-
- 1 edited
-
branches/libc-0.6/src/emx/src/lib/io/freopen.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/libc-0.6/src/emx/src/lib/io/freopen.c
r2536 r2541 15 15 #define TRUE 1 16 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 17 94 FILE *_STD(freopen) (const char *fname, const char *mode, FILE *stream) 18 95 { 19 FILE *result ;96 FILE *result; 20 97 21 98 STREAMV_LOCK; … … 25 102 * Change the stream mode. 26 103 */ 27 /** @todo freopen(NULL,,) isn't 100% right, but it'll do for now I hope */ 28 result = stream; 29 if (result->_flags & _IOOPEN) 104 if (stream->_flags & _IOOPEN) 30 105 { 31 char bt, ok; 32 int omode = 0; 33 34 switch (*mode) 106 int omode; 107 int flags = _interpret_stream_mode(mode, &omode); 108 if (flags != -1) 35 109 { 36 case 'r': 37 if (!(result->_flags & (_IORW | _IOREAD))) 110 if ( ((flags & _IORW) && !(stream->_flags & _IORW)) 111 || ((flags & _IOREAD) && !(stream->_flags & (_IORW | _IOREAD))) 112 || ((flags & _IOWRT) && !(stream->_flags & (_IORW | _IOWRT))) 113 ) 114 errno = EINVAL; 115 else 38 116 { 39 result = NULL; 40 errno = EINVAL; 117 if (!(stream->_flags & _IOSPECIAL)) 118 { 119 /* flush it and set the new mode */ 120 fflush(stream); 121 if (!fcntl(fileno(stream), F_SETFL, omode)) 122 stream = stream; 123 } 124 else 125 errno = EBADF; /* doesn't support the mode. */ 41 126 } 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 else74 {75 bt = TRUE;76 omode |= O_TEXT;77 }78 break;79 case 'b':80 if (bt)81 ok = FALSE;82 else83 {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 == -1114 || (omode & O_APPEND) != (oldmode & O_APPEND))115 result = NULL;116 127 } 117 128 } 118 129 else 119 { 120 errno = EBADF; 121 result = NULL; 122 } 130 errno = EBADF; 123 131 } 124 132 else
Note:
See TracChangeset
for help on using the changeset viewer.
