redir.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Martin Decky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <sys/types.h>
00038 #include <stdlib.h>
00039 #include <fcntl.h>
00040 #include <unistd.h>
00041 #include <str.h>
00042 #include <stdio.h>
00043 #include <task.h>
00044 #include <str_error.h>
00045 #include <errno.h>
00046 
00047 #define NAME  "redir"
00048 
00049 static void usage(void)
00050 {
00051         printf("Usage: %s [-i <stdin>] [-o <stdout>] [-e <stderr>] -- <cmd> [args ...]\n",
00052             NAME);
00053 }
00054 
00055 static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
00056 {
00057         if (fclose(*stream))
00058                 return;
00059         
00060         *stream = NULL;
00061         
00062         int oldfd = open(path, flags);
00063         if (oldfd < 0)
00064                 return;
00065         
00066         if (oldfd != fd) {
00067                 if (dup2(oldfd, fd) != fd)
00068                         return;
00069                 
00070                 if (close(oldfd))
00071                         return;
00072         }
00073         
00074         *stream = fdopen(fd, mode);
00075 }
00076 
00077 static task_id_t spawn(int argc, char *argv[])
00078 {
00079         const char **args;
00080         task_id_t id = 0;
00081         int rc;
00082 
00083         args = (const char **) calloc(argc + 1, sizeof(char *));
00084         if (!args) {
00085                 printf("No memory available\n");
00086                 return 0;
00087         }
00088         
00089         int i;
00090         for (i = 0; i < argc; i++)
00091                 args[i] = argv[i];
00092         
00093         args[argc] = NULL;
00094         
00095         rc = task_spawnv(&id, argv[0], args);
00096         
00097         free(args);
00098         
00099         if (rc != EOK) {
00100                 printf("%s: Error spawning %s (%s)\n", NAME, argv[0],
00101                     str_error(rc));
00102         }
00103         
00104         return id;
00105 }
00106 
00107 int main(int argc, char *argv[])
00108 {
00109         if (argc < 3) {
00110                 usage();
00111                 return -1;
00112         }
00113         
00114         int i;
00115         for (i = 1; i < argc; i++) {
00116                 if (str_cmp(argv[i], "-i") == 0) {
00117                         i++;
00118                         if (i >= argc) {
00119                                 usage();
00120                                 return -2;
00121                         }
00122                         reopen(&stdin, 0, argv[i], O_RDONLY, "r");
00123                 } else if (str_cmp(argv[i], "-o") == 0) {
00124                         i++;
00125                         if (i >= argc) {
00126                                 usage();
00127                                 return -3;
00128                         }
00129                         reopen(&stdout, 1, argv[i], O_WRONLY | O_CREAT, "w");
00130                 } else if (str_cmp(argv[i], "-e") == 0) {
00131                         i++;
00132                         if (i >= argc) {
00133                                 usage();
00134                                 return -4;
00135                         }
00136                         reopen(&stderr, 2, argv[i], O_WRONLY | O_CREAT, "w");
00137                 } else if (str_cmp(argv[i], "--") == 0) {
00138                         i++;
00139                         break;
00140                 }
00141         }
00142         
00143         if (i >= argc) {
00144                 usage();
00145                 return -5;
00146         }
00147         
00148         /*
00149          * FIXME: fdopen() should actually detect that we are opening a console
00150          * and it should set line-buffering mode automatically.
00151          */
00152         setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
00153         
00154         task_id_t id = spawn(argc - i, argv + i);
00155         
00156         if (id != 0) {
00157                 task_exit_t texit;
00158                 int retval;
00159                 task_wait(id, &texit, &retval);
00160                 
00161                 return retval;
00162         }
00163         
00164         return -6;
00165 }
00166 

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7