source: mainline/uspace/app/redir/redir.c@ 17aca1c

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

task_spawnv() sets id only on success, so make sure to clear it before calling
task_spawnv() so that redir can detect a failure to spawn 'cmd' and exits
with an exit code.

  • Property mode set to 100644
File size: 3.7 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 <sys/types.h>
38#include <stdlib.h>
39#include <fcntl.h>
40#include <unistd.h>
41#include <str.h>
42#include <stdio.h>
43#include <task.h>
44#include <str_error.h>
45#include <errno.h>
46
47#define NAME "redir"
48
49static void usage(void)
50{
51 printf("Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
52 NAME);
53}
54
55static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
56{
57 if (fclose(*stream))
58 return;
59
60 *stream = NULL;
61
62 int oldfd = open(path, flags);
63 if (oldfd < 0)
64 return;
65
66 if (oldfd != fd) {
67 if (dup2(oldfd, fd) != fd)
68 return;
69
70 if (close(oldfd))
71 return;
72 }
73
74 *stream = fdopen(fd, mode);
75}
76
77static task_id_t spawn(int argc, char *argv[])
78{
79 const char **args;
80 task_id_t id = 0;
81 int rc;
82
83 args = (const char **) calloc(argc + 1, sizeof(char *));
84 if (!args) {
85 printf("No memory available\n");
86 return 0;
87 }
88
89 int i;
90 for (i = 0; i < argc; i++)
91 args[i] = argv[i];
92
93 args[argc] = NULL;
94
95 rc = task_spawnv(&id, argv[0], args);
96
97 free(args);
98
99 if (rc != EOK) {
100 printf("%s: Error spawning %s (%s)\n", NAME, argv[0],
101 str_error(rc));
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], O_RDONLY, "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], O_WRONLY | O_CREAT, "w");
130 } else if (str_cmp(argv[i], "-e") == 0) {
131 i++;
132 if (i >= argc) {
133 usage();
134 return -4;
135 }
136 reopen(&stderr, 2, argv[i], O_WRONLY | O_CREAT, "w");
137 } else if (str_cmp(argv[i], "--") == 0) {
138 i++;
139 break;
140 }
141 }
142
143 if (i >= argc) {
144 usage();
145 return -5;
146 }
147
148 /*
149 * FIXME: fdopen() should actually detect that we are opening a console
150 * and it should set line-buffering mode automatically.
151 */
152 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
153
154 task_id_t id = spawn(argc - i, argv + i);
155
156 if (id != 0) {
157 task_exit_t texit;
158 int retval;
159 task_wait(id, &texit, &retval);
160
161 return retval;
162 }
163
164 return -6;
165}
166
167/** @}
168 */
Note: See TracBrowser for help on using the repository browser.