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