source: mainline/uspace/app/getterm/getterm.c@ d52b0044

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d52b0044 was d52b0044, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Update copyright year in getterm. Allow getterm to pass arguments to the command being executed.

  • Property mode set to 100644
File size: 3.1 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 getterm GetTerm
30 * @brief Console initialization task.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <sys/types.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <stdio.h>
41#include <task.h>
42#include <str_error.h>
43#include <errno.h>
44#include "version.h"
45
46#define APP_NAME "getterm"
47
48static void usage(void)
49{
50 printf("Usage: %s <terminal> <command> [<arguments...>]\n", APP_NAME);
51}
52
53static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
54{
55 if (fclose(*stream))
56 return;
57
58 *stream = NULL;
59
60 int oldfd = open(path, flags);
61 if (oldfd < 0)
62 return;
63
64 if (oldfd != fd) {
65 if (dup2(oldfd, fd) != fd)
66 return;
67
68 if (close(oldfd))
69 return;
70 }
71
72 *stream = fdopen(fd, mode);
73}
74
75int main(int argc, char *argv[])
76{
77 int rc;
78 task_exit_t texit;
79 int retval;
80 task_id_t id;
81 char *fname;
82
83 if (argc < 3) {
84 usage();
85 return -1;
86 }
87
88 reopen(&stdin, 0, argv[1], O_RDONLY, "r");
89 reopen(&stdout, 1, argv[1], O_WRONLY, "w");
90 reopen(&stderr, 2, argv[1], O_WRONLY, "w");
91
92 /*
93 * FIXME: fdopen() should actually detect that we are opening a console
94 * and it should set line-buffering mode automatically.
95 */
96 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
97
98 if (stdin == NULL)
99 return -2;
100
101 if (stdout == NULL)
102 return -3;
103
104 if (stderr == NULL)
105 return -4;
106
107 version_print(argv[1]);
108 fname = argv[2];
109
110 rc = task_spawnv(&id, fname, (const char * const *) &argv[2]);
111 if (rc != EOK) {
112 printf("%s: Error spawning %s (%s)\n", APP_NAME, fname,
113 str_error(rc));
114 return -5;
115 }
116
117 rc = task_wait(id, &texit, &retval);
118 if (rc != EOK) {
119 printf("%s: Error waiting for %s (%s)\n", APP_NAME, fname,
120 str_error(rc));
121 return -6;
122 }
123
124 return 0;
125}
126
127/** @}
128 */
Note: See TracBrowser for help on using the repository browser.