source: mainline/uspace/app/redir/redir.c@ 0fbe27d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0fbe27d was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

  • Property mode set to 100644
File size: 3.8 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 redir Redirector
30 * @brief Redirect stdin/stdout/stderr.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdlib.h>
38#include <str.h>
39#include <stdio.h>
40#include <task.h>
41#include <str_error.h>
42#include <errno.h>
43#include <vfs/vfs.h>
44
45#define NAME "redir"
46
47static void usage(void)
48{
49 fprintf(stderr, "Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
50 NAME);
51}
52
53static void reopen(FILE **stream, int fd, const char *path, int flags, int mode,
54 const char *fmode)
55{
56 if (fclose(*stream))
57 return;
58
59 *stream = NULL;
60
61 int oldfd = vfs_lookup_open(path, WALK_REGULAR | flags, mode);
62 if (oldfd < 0)
63 return;
64
65 if (oldfd != fd) {
66 if (vfs_clone(oldfd, fd, false) != fd)
67 return;
68
69 if (vfs_put(oldfd))
70 return;
71 }
72
73 *stream = fdopen(fd, fmode);
74}
75
76static task_id_t spawn(task_wait_t *wait, int argc, char *argv[])
77{
78 const char **args;
79 task_id_t id = 0;
80 int rc;
81
82 args = (const char **) calloc(argc + 1, sizeof(char *));
83 if (!args) {
84 fprintf(stderr, "No memory available\n");
85 return 0;
86 }
87
88 int i;
89 for (i = 0; i < argc; i++)
90 args[i] = argv[i];
91
92 args[argc] = NULL;
93
94 rc = task_spawnv(&id, wait, argv[0], args);
95
96 free(args);
97
98 if (rc != EOK) {
99 fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, argv[0],
100 str_error(rc));
101 return 0;
102 }
103
104 return id;
105}
106
107int main(int argc, char *argv[])
108{
109 if (argc < 3) {
110 usage();
111 return -1;
112 }
113
114 int i;
115 for (i = 1; i < argc; i++) {
116 if (str_cmp(argv[i], "-i") == 0) {
117 i++;
118 if (i >= argc) {
119 usage();
120 return -2;
121 }
122 reopen(&stdin, 0, argv[i], 0, MODE_READ, "r");
123 } else if (str_cmp(argv[i], "-o") == 0) {
124 i++;
125 if (i >= argc) {
126 usage();
127 return -3;
128 }
129 reopen(&stdout, 1, argv[i], WALK_MAY_CREATE, MODE_WRITE,
130 "w");
131 } else if (str_cmp(argv[i], "-e") == 0) {
132 i++;
133 if (i >= argc) {
134 usage();
135 return -4;
136 }
137 reopen(&stderr, 2, argv[i], WALK_MAY_CREATE, MODE_WRITE,
138 "w");
139 } else if (str_cmp(argv[i], "--") == 0) {
140 i++;
141 break;
142 }
143 }
144
145 if (i >= argc) {
146 usage();
147 return -5;
148 }
149
150 /*
151 * FIXME: fdopen() should actually detect that we are opening a console
152 * and it should set line-buffering mode automatically.
153 */
154 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
155
156 task_wait_t wait;
157 task_id_t id = spawn(&wait, argc - i, argv + i);
158
159 if (id != 0) {
160 task_exit_t texit;
161 int retval;
162 task_wait(&wait, &texit, &retval);
163
164 return retval;
165 }
166
167 return -6;
168}
169
170/** @}
171 */
Note: See TracBrowser for help on using the repository browser.