Changeset b19e892 in mainline for uspace/lib/c/generic/io/io.c
- Timestamp:
- 2017-04-02T10:39:13Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 9c4cf0d
- Parents:
- 80743a1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/io/io.c
r80743a1 rb19e892 35 35 #include <stdio.h> 36 36 #include <unistd.h> 37 #include <fcntl.h>38 37 #include <assert.h> 39 38 #include <str.h> … … 115 114 int stdinfd = vfs_clone(infd, -1, false); 116 115 assert(stdinfd == 0); 117 _vfs_open(stdinfd, MODE_READ);116 vfs_open(stdinfd, MODE_READ); 118 117 stdin = fdopen(stdinfd, "r"); 119 118 } else { … … 128 127 while (stdoutfd < 1) 129 128 stdoutfd = vfs_clone(outfd, -1, false); 130 _vfs_open(stdoutfd, MODE_APPEND);129 vfs_open(stdoutfd, MODE_APPEND); 131 130 stdout = fdopen(stdoutfd, "a"); 132 131 } else { … … 141 140 while (stderrfd < 2) 142 141 stderrfd = vfs_clone(errfd, -1, false); 143 _vfs_open(stderrfd, MODE_APPEND);142 vfs_open(stderrfd, MODE_APPEND); 144 143 stderr = fdopen(stderrfd, "a"); 145 144 } else { … … 157 156 } 158 157 159 static bool parse_mode(const char * mode, int *flags)158 static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate) 160 159 { 161 160 /* Parse mode except first character. */ 162 const char *mp = mode;161 const char *mp = fmode; 163 162 if (*mp++ == 0) { 164 163 errno = EINVAL; … … 180 179 return false; 181 180 } 182 183 /* Parse first character of mode and determine flags for open(). */ 184 switch (mode[0]) { 181 182 *create = false; 183 *truncate = false; 184 185 /* Parse first character of fmode and determine mode for vfs_open(). */ 186 switch (fmode[0]) { 185 187 case 'r': 186 * flags = plus ? O_RDWR : O_RDONLY;188 *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ; 187 189 break; 188 190 case 'w': 189 *flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY); 191 *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE; 192 *create = true; 193 if (!plus) 194 *truncate = true; 190 195 break; 191 196 case 'a': … … 195 200 return false; 196 201 } 197 *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY); 202 203 *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE); 204 *create = true; 198 205 break; 199 206 default: … … 269 276 * 270 277 */ 271 FILE *fopen(const char *path, const char *mode) 272 { 273 int flags; 274 if (!parse_mode(mode, &flags)) 278 FILE *fopen(const char *path, const char *fmode) 279 { 280 int mode; 281 bool create; 282 bool truncate; 283 284 if (!parse_mode(fmode, &mode, &create, &truncate)) 275 285 return NULL; 276 286 … … 281 291 return NULL; 282 292 } 283 284 stream->fd = open(path, flags, 0666); 285 if (stream->fd < 0) { 286 /* errno was set by open() */ 293 294 int flags = WALK_REGULAR; 295 if (create) 296 flags |= WALK_MAY_CREATE; 297 int file = vfs_lookup(path, flags); 298 if (file < 0) { 299 errno = file; 287 300 free(stream); 288 301 return NULL; 289 302 } 290 303 304 int rc = vfs_open(file, mode); 305 if (rc != EOK) { 306 errno = rc; 307 close(file); 308 free(stream); 309 return NULL; 310 } 311 312 if (truncate) { 313 rc = vfs_resize(file, 0); 314 if (rc != EOK) { 315 errno = rc; 316 close(file); 317 free(stream); 318 return NULL; 319 } 320 } 321 322 stream->fd = file; 291 323 stream->pos = 0; 292 324 stream->error = false;
Note:
See TracChangeset
for help on using the changeset viewer.