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

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

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 3.9 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 <stdlib.h>
[19f857a]38#include <str.h>
[c7dc8ad]39#include <stdio.h>
40#include <task.h>
[d9fae235]41#include <str_error.h>
[0485135]42#include <errno.h>
[fcab7ef]43#include <vfs/vfs.h>
[d9fae235]44
45#define NAME "redir"
[c7dc8ad]46
47static void usage(void)
48{
[4688fab8]49 fprintf(stderr, "Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
[d9fae235]50 NAME);
[c7dc8ad]51}
52
[b19e892]53static void reopen(FILE **stream, int fd, const char *path, int flags, int mode,
54 const char *fmode)
[c7dc8ad]55{
56 if (fclose(*stream))
57 return;
58
59 *stream = NULL;
60
[f77c1c9]61 int oldfd;
[b7fd2a0]62 errno_t rc = vfs_lookup_open(path, WALK_REGULAR | flags, mode, &oldfd);
[f77c1c9]63 if (rc != EOK)
[c7dc8ad]64 return;
65
66 if (oldfd != fd) {
[f77c1c9]67 int newfd;
68 if (vfs_clone(oldfd, fd, false, &newfd) != EOK)
[c7dc8ad]69 return;
70
[f77c1c9]71 assert(newfd == fd);
72
[9c4cf0d]73 if (vfs_put(oldfd))
[c7dc8ad]74 return;
75 }
76
[b19e892]77 *stream = fdopen(fd, fmode);
[c7dc8ad]78}
79
[1c635d6]80static task_id_t spawn(task_wait_t *wait, int argc, char *argv[])
[c7dc8ad]81{
[0485135]82 const char **args;
[a7e7716]83 task_id_t id = 0;
[b7fd2a0]84 errno_t rc;
[0485135]85
86 args = (const char **) calloc(argc + 1, sizeof(char *));
[c7dc8ad]87 if (!args) {
[4688fab8]88 fprintf(stderr, "No memory available\n");
[c7dc8ad]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
[1c635d6]98 rc = task_spawnv(&id, wait, argv[0], args);
[c7dc8ad]99
100 free(args);
101
[0485135]102 if (rc != EOK) {
[4688fab8]103 fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, argv[0],
[0485135]104 str_error(rc));
[a7e7716]105 return 0;
[0485135]106 }
[c7dc8ad]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 }
[b19e892]126 reopen(&stdin, 0, argv[i], 0, MODE_READ, "r");
[c7dc8ad]127 } else if (str_cmp(argv[i], "-o") == 0) {
128 i++;
129 if (i >= argc) {
130 usage();
131 return -3;
132 }
[b19e892]133 reopen(&stdout, 1, argv[i], WALK_MAY_CREATE, MODE_WRITE,
134 "w");
[c7dc8ad]135 } else if (str_cmp(argv[i], "-e") == 0) {
136 i++;
137 if (i >= argc) {
138 usage();
139 return -4;
140 }
[b19e892]141 reopen(&stderr, 2, argv[i], WALK_MAY_CREATE, MODE_WRITE,
142 "w");
[c7dc8ad]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);
[1c635d6]159
160 task_wait_t wait;
161 task_id_t id = spawn(&wait, argc - i, argv + i);
[c7dc8ad]162
163 if (id != 0) {
164 task_exit_t texit;
165 int retval;
[1c635d6]166 task_wait(&wait, &texit, &retval);
[c7dc8ad]167
168 return retval;
169 }
170
171 return -6;
172}
173
174/** @}
175 */
Note: See TracBrowser for help on using the repository browser.