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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a35b458 was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

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