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

Last change on this file was a63966d, checked in by Jakub Jermar <jakub@…>, 7 years ago

Provide doc/doxygroups.h for most apps

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