8745b3be21d2fc3ebda66ce18e033d18d0c1481d
[oweals/busybox.git] / console-tools / setconsole.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  setconsole.c - redirect system console output
4  *
5  *  Copyright (C) 2004,2005  Enrik Berkhan <Enrik.Berkhan@inka.de>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <getopt.h>
11 #include "libbb.h"
12
13 #if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
14 static const struct option setconsole_long_options[] = {
15         { "reset", 0, NULL, 'r' },
16         { 0, 0, 0, 0 }
17 };
18 #endif
19
20 #define OPT_SETCONS_RESET 1
21
22 int setconsole_main(int argc, char **argv);
23 int setconsole_main(int argc, char **argv)
24 {
25         unsigned long flags;
26         const char *device = CURRENT_TTY;
27
28 #if ENABLE_FEATURE_SETCONSOLE_LONG_OPTIONS
29         applet_long_options = setconsole_long_options;
30 #endif
31         flags = getopt32(argc, argv, "r");
32
33         if (argc - optind > 1)
34                 bb_show_usage();
35
36         if (argc - optind == 1) {
37                 if (flags & OPT_SETCONS_RESET)
38                         bb_show_usage();
39                 device = argv[optind];
40         } else {
41                 if (flags & OPT_SETCONS_RESET)
42                         device = DEV_CONSOLE;
43         }
44
45         xioctl(xopen(device, O_RDONLY), TIOCCONS, NULL);
46         return EXIT_SUCCESS;
47 }