rtc: get rid of "port + 1" and use a preprocessor macro
[helenos.git] / uspace / app / redir / redir.c
blob896d7d4aad5268a0549983b7482f47b3bc275e6c
1 /*
2 * Copyright (c) 2009 Martin Decky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup redir Redirector
30 * @brief Redirect stdin/stdout/stderr.
31 * @{
33 /**
34 * @file
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>
47 #define NAME "redir"
49 static void usage(void)
51 fprintf(stderr, "Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
52 NAME);
55 static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
57 if (fclose(*stream))
58 return;
60 *stream = NULL;
62 int oldfd = open(path, flags);
63 if (oldfd < 0)
64 return;
66 if (oldfd != fd) {
67 if (dup2(oldfd, fd) != fd)
68 return;
70 if (close(oldfd))
71 return;
74 *stream = fdopen(fd, mode);
77 static task_id_t spawn(int argc, char *argv[])
79 const char **args;
80 task_id_t id = 0;
81 int rc;
83 args = (const char **) calloc(argc + 1, sizeof(char *));
84 if (!args) {
85 fprintf(stderr, "No memory available\n");
86 return 0;
89 int i;
90 for (i = 0; i < argc; i++)
91 args[i] = argv[i];
93 args[argc] = NULL;
95 rc = task_spawnv(&id, argv[0], args);
97 free(args);
99 if (rc != EOK) {
100 fprintf(stderr, "%s: Error spawning %s (%s)\n", NAME, argv[0],
101 str_error(rc));
102 return 0;
105 return id;
108 int main(int argc, char *argv[])
110 if (argc < 3) {
111 usage();
112 return -1;
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;
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;
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;
137 reopen(&stderr, 2, argv[i], O_WRONLY | O_CREAT, "w");
138 } else if (str_cmp(argv[i], "--") == 0) {
139 i++;
140 break;
144 if (i >= argc) {
145 usage();
146 return -5;
150 * FIXME: fdopen() should actually detect that we are opening a console
151 * and it should set line-buffering mode automatically.
153 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
155 task_id_t id = spawn(argc - i, argv + i);
157 if (id != 0) {
158 task_exit_t texit;
159 int retval;
160 task_wait(id, &texit, &retval);
162 return retval;
165 return -6;
168 /** @}