Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 2b88074b in mainline for uspace/app/getvc/getvc.c


Ignore:
Timestamp:
2009-10-29T22:06:46Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master
Children:
2e983c7
Parents:
14ecd6c
Message:

vfs: file descriptors housekeeping changes

  • add support for allocating new file descriptors from the end of the fd range (O_DESC)
  • add support for assigning of an already opened file to a free file descriptor

vfs: VFS_OUT_CLOSE is called only when the file reference count is about to drop to zero
vfs: implement VFS_IN_DUP

libc: optimize current working directory housekeeping

  • using opendir() was an overkill
  • allocate current working directory file descriptor from the end of the fd range using O_DESC (so it doesn't mess with the well-known descriptors 0, 1 and 2)

libc: implement dup2() (via VFS_IN_DUP)

getvc: change stdin/stdout/stderr by a slightly more elegant way (using dup2())

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/getvc/getvc.c

    r14ecd6c r2b88074b  
    3636
    3737#include <sys/types.h>
     38#include <fcntl.h>
    3839#include <unistd.h>
    3940#include <stdio.h>
     
    4647}
    4748
    48 static void closeall(void)
     49static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
    4950{
    50         fclose(stdin);
    51         fclose(stdout);
    52         fclose(stderr);
     51        if (fclose(*stream))
     52                return;
    5353       
    54         close(0);
    55         close(1);
    56         close(2);
     54        *stream = NULL;
     55       
     56        int oldfd = open(path, flags);
     57        if (oldfd < 0)
     58                return;
     59       
     60        if (oldfd != fd) {
     61                if (dup2(oldfd, fd) != fd)
     62                        return;
     63               
     64                if (close(oldfd))
     65                        return;
     66        }
     67       
     68        *stream = fdopen(fd, mode);
    5769}
    5870
     
    7486int main(int argc, char *argv[])
    7587{
    76         task_exit_t texit;
    77         int retval;
    78 
    7988        if (argc < 3) {
    8089                usage();
     
    8291        }
    8392       
    84         closeall();
     93        reopen(&stdin, 0, argv[1], O_RDONLY, "r");
     94        reopen(&stdout, 1, argv[1], O_WRONLY, "w");
     95        reopen(&stderr, 2, argv[1], O_WRONLY, "w");
    8596       
    86         stdin = fopen(argv[1], "r");
    87         stdout = fopen(argv[1], "w");
    88         stderr = fopen(argv[1], "w");
    89 
    9097        /*
    91          * FIXME: fopen() should actually detect that we are opening a console
     98         * FIXME: fdopen() should actually detect that we are opening a console
    9299         * and it should set line-buffering mode automatically.
    93100         */
    94101        setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
    95102       
    96         if ((stdin == NULL)
    97             || (stdout == NULL)
    98             || (stderr == NULL))
     103        if (stdin == NULL)
    99104                return -2;
     105       
     106        if (stdout == NULL)
     107                return -3;
     108       
     109        if (stderr == NULL)
     110                return -4;
    100111       
    101112        version_print(argv[1]);
    102113        task_id_t id = spawn(argv[2]);
     114       
     115        task_exit_t texit;
     116        int retval;
    103117        task_wait(id, &texit, &retval);
    104118       
Note: See TracChangeset for help on using the changeset viewer.