Changeset 5a4fef9 in mainline for uspace/app/getvc/getvc.c


Ignore:
Timestamp:
2009-11-16T21:24:28Z (14 years ago)
Author:
Pavel Rimsky <pavel@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
18baf9c0
Parents:
5f678b1c (diff), 9c70ed6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merged head changes to this branch

File:
1 edited

Legend:

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

    r5f678b1c r5a4fef9  
    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
    5971static task_id_t spawn(char *fname)
    6072{
    61         char *argv[2];
     73        char *args[2];
    6274       
    63         argv[0] = fname;
    64         argv[1] = NULL;
     75        args[0] = fname;
     76        args[1] = NULL;
    6577       
    66         task_id_t id = task_spawn(fname, argv);
     78        task_id_t id = task_spawn(fname, args);
    6779       
    6880        if (id == 0)
     
    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]);
    103         task_wait(id, &texit, &retval);
    104114       
    105         return 0;
     115        if (id != 0) {
     116                task_exit_t texit;
     117                int retval;
     118                task_wait(id, &texit, &retval);
     119               
     120                return 0;
     121        }
     122       
     123        return -5;
    106124}
    107125
Note: See TracChangeset for help on using the changeset viewer.