source: mainline/uspace/app/getvc/getvc.c@ 2e983c7

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

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())

  • 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 *argv[2];
74
75 argv[0] = fname;
76 argv[1] = NULL;
77
78 task_id_t id = task_spawn(fname, argv);
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 task_exit_t texit;
116 int retval;
117 task_wait(id, &texit, &retval);
118
119 return 0;
120}
121
122/** @}
123 */
Note: See TracBrowser for help on using the repository browser.