00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00037 #include <sys/types.h>
00038 #include <fcntl.h>
00039 #include <unistd.h>
00040 #include <stdio.h>
00041 #include <task.h>
00042 #include <str_error.h>
00043 #include <errno.h>
00044 #include "version.h"
00045 #include "welcome.h"
00046
00047 #define APP_NAME "getterm"
00048
00049 static void usage(void)
00050 {
00051 printf("Usage: %s <terminal> [-w] <command> [<arguments...>]\n", APP_NAME);
00052 }
00053
00054 static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
00055 {
00056 if (fclose(*stream))
00057 return;
00058
00059 *stream = NULL;
00060
00061 int oldfd = open(path, flags);
00062 if (oldfd < 0)
00063 return;
00064
00065 if (oldfd != fd) {
00066 if (dup2(oldfd, fd) != fd)
00067 return;
00068
00069 if (close(oldfd))
00070 return;
00071 }
00072
00073 *stream = fdopen(fd, mode);
00074 }
00075
00076 int main(int argc, char *argv[])
00077 {
00078 int rc;
00079 task_exit_t texit;
00080 int retval;
00081 task_id_t id;
00082 char *fname, *term;
00083 char **cmd_args;
00084 bool print_wmsg;
00085
00086 argv++;
00087 argc--;
00088 if (argc < 1) {
00089 usage();
00090 return -1;
00091 }
00092
00093 if (str_cmp(*argv, "-w") == 0) {
00094 print_wmsg = true;
00095 argv++;
00096 argc--;
00097 } else {
00098 print_wmsg = false;
00099 }
00100
00101 if (argc < 2) {
00102 usage();
00103 return -1;
00104 }
00105
00106 term = *argv++;
00107 fname = *argv;
00108 cmd_args = argv;
00109
00110 reopen(&stdin, 0, term, O_RDONLY, "r");
00111 reopen(&stdout, 1, term, O_WRONLY, "w");
00112 reopen(&stderr, 2, term, O_WRONLY, "w");
00113
00114
00115
00116
00117
00118 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
00119
00120 if (stdin == NULL)
00121 return -2;
00122
00123 if (stdout == NULL)
00124 return -3;
00125
00126 if (stderr == NULL)
00127 return -4;
00128
00129 version_print(term);
00130 if (print_wmsg)
00131 welcome_msg_print();
00132
00133 rc = task_spawnv(&id, fname, (const char * const *) cmd_args);
00134 if (rc != EOK) {
00135 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
00136 str_error(rc));
00137 return -5;
00138 }
00139
00140 rc = task_wait(id, &texit, &retval);
00141 if (rc != EOK) {
00142 printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
00143 str_error(rc));
00144 return -6;
00145 }
00146
00147 return 0;
00148 }
00149