source: mainline/uspace/app/getvc/getvc.c@ e6b73ad0

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e6b73ad0 was c7dc8ad, checked in by Martin Decky <martin@…>, 16 years ago

proof-of-concept of stdin/stdout/stderr redirection

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/*
2 * Copyright (c) 2009 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup getvc GetVC
30 * @brief Console initialization task.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <sys/types.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <stdio.h>
41#include <task.h>
42#include "version.h"
43
44static void usage(void)
45{
46 printf("Usage: getvc <device> <path>\n");
47}
48
49static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
50{
51 if (fclose(*stream))
52 return;
53
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);
69}
70
71static task_id_t spawn(char *fname)
72{
73 char *args[2];
74
75 args[0] = fname;
76 args[1] = NULL;
77
78 task_id_t id = task_spawn(fname, args);
79
80 if (id == 0)
81 printf("Error spawning %s\n", fname);
82
83 return id;
84}
85
86int main(int argc, char *argv[])
87{
88 if (argc < 3) {
89 usage();
90 return -1;
91 }
92
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");
96
97 /*
98 * FIXME: fdopen() should actually detect that we are opening a console
99 * and it should set line-buffering mode automatically.
100 */
101 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
102
103 if (stdin == NULL)
104 return -2;
105
106 if (stdout == NULL)
107 return -3;
108
109 if (stderr == NULL)
110 return -4;
111
112 version_print(argv[1]);
113 task_id_t id = spawn(argv[2]);
114
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;
124}
125
126/** @}
127 */
Note: See TracBrowser for help on using the repository browser.