Changeset b19e892 in mainline for uspace/lib/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
- Location:
- uspace/lib/c
- Files:
-
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/elf/elf_mod.c
r80743a1 rb19e892 97 97 98 98 int ofile = vfs_clone(file, -1, true); 99 int rc = _vfs_open(ofile, MODE_READ);99 int rc = vfs_open(ofile, MODE_READ); 100 100 if (rc != EOK) { 101 101 return rc; -
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; -
uspace/lib/c/generic/rtld/module.c
r80743a1 rb19e892 38 38 #include <elf/elf_load.h> 39 39 #include <errno.h> 40 #include <fcntl.h>41 40 #include <loader/pcb.h> 42 41 #include <stdio.h> -
uspace/lib/c/generic/vfs/vfs.c
r80743a1 rb19e892 41 41 #include <unistd.h> 42 42 #include <dirent.h> 43 #include <fcntl.h>44 43 #include <stdio.h> 45 44 #include <sys/types.h> … … 155 154 } 156 155 157 int _vfs_open(int fildes, int mode) 158 { 159 async_exch_t *exch = vfs_exchange_begin(); 160 sysarg_t rc = async_req_2_0(exch, VFS_IN_OPEN, fildes, mode); 161 vfs_exchange_end(exch); 162 163 return (int) rc; 156 int vfs_open(int file, int mode) 157 { 158 async_exch_t *exch = vfs_exchange_begin(); 159 int rc = async_req_2_0(exch, VFS_IN_OPEN, file, mode); 160 vfs_exchange_end(exch); 161 162 return rc; 163 } 164 165 int vfs_lookup_open(const char *path, int flags, int mode) 166 { 167 int file = vfs_lookup(path, flags); 168 if (file < 0) 169 return file; 170 171 int rc = vfs_open(file, mode); 172 if (rc != EOK) { 173 close(file); 174 return rc; 175 } 176 177 return file; 164 178 } 165 179 … … 353 367 } 354 368 355 static int walk_flags(int oflags)356 {357 int flags = 0;358 if (oflags & O_CREAT) {359 if (oflags & O_EXCL)360 flags |= WALK_MUST_CREATE;361 else362 flags |= WALK_MAY_CREATE;363 }364 return flags;365 }366 367 /** Open file.368 *369 * @param path File path370 * @param oflag O_xxx flags371 * @param mode File mode (only with O_CREAT)372 *373 * @return Nonnegative file descriptor on success. On error -1 is returned374 * and errno is set.375 */376 int open(const char *path, int oflag, ...)377 {378 if (((oflag & (O_RDONLY | O_WRONLY | O_RDWR)) == 0) ||379 ((oflag & (O_RDONLY | O_WRONLY)) == (O_RDONLY | O_WRONLY)) ||380 ((oflag & (O_RDONLY | O_RDWR)) == (O_RDONLY | O_RDWR)) ||381 ((oflag & (O_WRONLY | O_RDWR)) == (O_WRONLY | O_RDWR))) {382 errno = EINVAL;383 return -1;384 }385 386 int fd = vfs_lookup(path, walk_flags(oflag) | WALK_REGULAR);387 if (fd < 0) {388 errno = fd;389 return -1;390 }391 392 int mode =393 ((oflag & O_RDWR) ? MODE_READ | MODE_WRITE : 0) |394 ((oflag & O_RDONLY) ? MODE_READ : 0) |395 ((oflag & O_WRONLY) ? MODE_WRITE : 0) |396 ((oflag & O_APPEND) ? MODE_APPEND : 0);397 398 int rc = _vfs_open(fd, mode);399 if (rc < 0) {400 close(fd);401 errno = rc;402 return -1;403 }404 405 if (oflag & O_TRUNC) {406 assert(oflag & O_WRONLY || oflag & O_RDWR);407 assert(!(oflag & O_APPEND));408 409 (void) vfs_resize(fd, 0);410 }411 412 return fd;413 }414 415 369 /** Close file. 416 370 * … … 703 657 } 704 658 705 int rc = _vfs_open(fd, MODE_READ);659 int rc = vfs_open(fd, MODE_READ); 706 660 if (rc < 0) { 707 661 free(dirp); -
uspace/lib/c/include/vfs/vfs.h
r80743a1 rb19e892 83 83 84 84 extern int _vfs_walk(int, const char *, int); 85 extern int _vfs_open(int, int);86 extern int vfs_lookup(const char *, int);87 85 88 86 extern int vfs_pass_handle(async_exch_t *, int, async_exch_t *); … … 92 90 extern int vfs_link(int, const char *, vfs_file_kind_t); 93 91 extern int vfs_link_path(const char *, vfs_file_kind_t); 92 extern int vfs_lookup(const char *, int); 93 extern int vfs_lookup_open(const char *, int, int); 94 94 extern int vfs_mount_path(const char *, const char *, const char *, 95 95 const char *, unsigned int, unsigned int); 96 96 extern int vfs_mount(int, const char *, service_id_t, const char *, unsigned, 97 97 unsigned, int *); 98 extern int vfs_open(int, int); 98 99 extern int vfs_resize(int, aoff64_t); 99 100 extern int vfs_root(void);
Note:
See TracChangeset
for help on using the changeset viewer.