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
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
24 #include <linux/stat.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) {
43 flags
|= TIMEOUT_RETURN
;
46 flags
|= TIMEOUT_CTRLC
;
49 flags
|= TIMEOUT_ANYKEY
;
52 flags
|= TIMEOUT_SILENT
;
60 return COMMAND_ERROR_USAGE
;
62 timeout
= simple_strtoul(argv
[optind
], NULL
, 0);
64 start
= get_time_ns();
69 if (!(flags
& TIMEOUT_SILENT
))
70 printf("%2d", countdown
--);
75 if (flags
& TIMEOUT_CTRLC
&& key
== 3)
77 if (flags
& TIMEOUT_ANYKEY
)
79 if (flags
& TIMEOUT_RETURN
&& key
== '\n')
82 if (!(flags
& TIMEOUT_SILENT
) && is_timeout(second
, SECOND
)) {
83 printf("\b\b%2d", countdown
--);
86 } while (!is_timeout(start
, timeout
* SECOND
));
90 if (!(flags
& TIMEOUT_SILENT
))
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"
105 BAREBOX_CMD_START(timeout
)
107 .usage
= "wait for a specified timeout",
108 BAREBOX_CMD_HELP(cmd_timeout_help
)