source: mainline/uspace/app/redir/redir.c@ d4d74dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d4d74dc was 4688fab8, checked in by Martin Sucha <sucha14@…>, 15 years ago

redir: display error messages on stderr instead of stdout

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[c7dc8ad]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>
[19f857a]41#include <str.h>
[c7dc8ad]42#include <stdio.h>
43#include <task.h>
[d9fae235]44#include <str_error.h>
[0485135]45#include <errno.h>
[d9fae235]46
47#define NAME "redir"
[c7dc8ad]48
49static void usage(void)
50{
[4688fab8]51 fprintf(stderr, "Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
[d9fae235]52 NAME);
[c7dc8ad]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{
[0485135]79 const char **args;
[a7e7716]80 task_id_t id = 0;
[0485135]81 int rc;
82
83 args = (const char **) calloc(argc + 1, sizeof(char *));
[c7dc8ad]84 if (!args) {
[4688fab8]85 fprintf(stderr, "No memory available\n");
[c7dc8ad]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
[0485135]95 rc = task_spawnv(&id, argv[0], args);
[c7dc8ad]96
97 free(args);
98
[0485135]99 if (rc != EOK) {
[4688fab8]100 fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, argv[0],
[0485135]101 str_error(rc));
[a7e7716]102 return 0;
[0485135]103 }
[c7dc8ad]104
105 return id;
106}
107
108int main(int argc, char *argv[])
109{
110 if (argc < 3) {
111 usage();
112 return -1;
113 }
114
115 int i;
116 for (i = 1; i < argc; i++) {
117 if (str_cmp(argv[i], "-i") == 0) {
118 i++;
119 if (i >= argc) {
120 usage();
121 return -2;
122 }
123 reopen(&stdin, 0, argv[i], O_RDONLY, "r");
124 } else if (str_cmp(argv[i], "-o") == 0) {
125 i++;
126 if (i >= argc) {
127 usage();
128 return -3;
129 }
130 reopen(&stdout, 1, argv[i], O_WRONLY | O_CREAT, "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], O_WRONLY | O_CREAT, "w");
138 } else if (str_cmp(argv[i], "--") == 0) {
139 i++;
140 break;
141 }
142 }
143
144 if (i >= argc) {
145 usage();
146 return -5;
147 }
148
149 /*
150 * FIXME: fdopen() should actually detect that we are opening a console
151 * and it should set line-buffering mode automatically.
152 */
153 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
154
155 task_id_t id = spawn(argc - i, argv + i);
156
157 if (id != 0) {
158 task_exit_t texit;
159 int retval;
160 task_wait(id, &texit, &retval);
161
162 return retval;
163 }
164
165 return -6;
166}
167
168/** @}
169 */
Note: See TracBrowser for help on using the repository browser.