arm start.c: Make runtime function address calculation tolerant for more compilers
[barebox-mini2440.git] / commands / timeout.c
blob5f2ab9a206d9ef12d060d866585c6e67d139b2e7
1 /*
2 * timeout - wait for timeout
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <common.h>
23 #include <command.h>
24 #include <linux/stat.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <clock.h>
29 #define TIMEOUT_RETURN (1 << 0)
30 #define TIMEOUT_CTRLC (1 << 1)
31 #define TIMEOUT_ANYKEY (1 << 2)
32 #define TIMEOUT_SILENT (1 << 3)
34 static int do_timeout(struct command *cmdtp, int argc, char *argv[])
36 int timeout = 3, ret = 1;
37 int flags = 0, opt, countdown;
38 uint64_t start, second;
40 while((opt = getopt(argc, argv, "t:crsa")) > 0) {
41 switch(opt) {
42 case 'r':
43 flags |= TIMEOUT_RETURN;
44 break;
45 case 'c':
46 flags |= TIMEOUT_CTRLC;
47 break;
48 case 'a':
49 flags |= TIMEOUT_ANYKEY;
50 break;
51 case 's':
52 flags |= TIMEOUT_SILENT;
53 break;
54 default:
55 return 1;
59 if (optind == argc)
60 return COMMAND_ERROR_USAGE;
62 timeout = simple_strtoul(argv[optind], NULL, 0);
64 start = get_time_ns();
65 second = start;
67 countdown = timeout;
69 if (!(flags & TIMEOUT_SILENT))
70 printf("%2d", countdown--);
72 do {
73 if (tstc()) {
74 int key = getc();
75 if (flags & TIMEOUT_CTRLC && key == 3)
76 goto out;
77 if (flags & TIMEOUT_ANYKEY)
78 goto out;
79 if (flags & TIMEOUT_RETURN && key == '\n')
80 goto out;
82 if (!(flags & TIMEOUT_SILENT) && is_timeout(second, SECOND)) {
83 printf("\b\b%2d", countdown--);
84 second += SECOND;
86 } while (!is_timeout(start, timeout * SECOND));
88 ret = 0;
89 out:
90 if (!(flags & TIMEOUT_SILENT))
91 printf("\n");
93 return ret;
96 static const __maybe_unused char cmd_timeout_help[] =
97 "Usage: timeout [OPTION]... <timeout>\n"
98 "Wait <timeout> seconds for a timeout. Return 1 if the user intervented\n"
99 "or 0 if a timeout occured\n"
100 " -a interrupt on any key\n"
101 " -c interrupt on ctrl-c\n"
102 " -r interrupt on return\n"
103 " -s silent mode\n";
105 BAREBOX_CMD_START(timeout)
106 .cmd = do_timeout,
107 .usage = "wait for a specified timeout",
108 BAREBOX_CMD_HELP(cmd_timeout_help)
109 BAREBOX_CMD_END