xref: /aosp_15_r20/external/ltp/testcases/lib/tst_sleep.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
7*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
10*49cdfc7eSAndroid Build Coastguard Worker 
print_help(void)11*49cdfc7eSAndroid Build Coastguard Worker static void print_help(void)
12*49cdfc7eSAndroid Build Coastguard Worker {
13*49cdfc7eSAndroid Build Coastguard Worker 	printf("Usage: tst_usleep interval[s|ms|us]\n\n");
14*49cdfc7eSAndroid Build Coastguard Worker 	printf("       If no unit is specified the interval is in seconds\n");
15*49cdfc7eSAndroid Build Coastguard Worker }
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker static struct unit {
18*49cdfc7eSAndroid Build Coastguard Worker 	const char *unit;
19*49cdfc7eSAndroid Build Coastguard Worker 	long mul;
20*49cdfc7eSAndroid Build Coastguard Worker } units[] = {
21*49cdfc7eSAndroid Build Coastguard Worker 	{"",   1000000},
22*49cdfc7eSAndroid Build Coastguard Worker 	{"s",  1000000},
23*49cdfc7eSAndroid Build Coastguard Worker 	{"ms", 1000},
24*49cdfc7eSAndroid Build Coastguard Worker 	{"us", 1},
25*49cdfc7eSAndroid Build Coastguard Worker };
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static unsigned int units_len = sizeof(units) / sizeof(*units);
28*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])29*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	int opt;
32*49cdfc7eSAndroid Build Coastguard Worker 	long interval, secs = 0, usecs = 0;
33*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
34*49cdfc7eSAndroid Build Coastguard Worker 	char *end;
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	while ((opt = getopt(argc, argv, ":h")) != -1) {
37*49cdfc7eSAndroid Build Coastguard Worker 		switch (opt) {
38*49cdfc7eSAndroid Build Coastguard Worker 		case 'h':
39*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
40*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
41*49cdfc7eSAndroid Build Coastguard Worker 		default:
42*49cdfc7eSAndroid Build Coastguard Worker 			print_help();
43*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
44*49cdfc7eSAndroid Build Coastguard Worker 		}
45*49cdfc7eSAndroid Build Coastguard Worker 	}
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (optind >= argc) {
48*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Expected interval argument\n\n");
49*49cdfc7eSAndroid Build Coastguard Worker 		print_help();
50*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
51*49cdfc7eSAndroid Build Coastguard Worker 	}
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	interval = strtol(argv[optind], &end, 10);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	if (argv[optind] == end) {
56*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Invalid interval '%s'\n\n",
57*49cdfc7eSAndroid Build Coastguard Worker 		        argv[optind]);
58*49cdfc7eSAndroid Build Coastguard Worker 		print_help();
59*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
60*49cdfc7eSAndroid Build Coastguard Worker 	}
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < units_len; i++) {
63*49cdfc7eSAndroid Build Coastguard Worker 		if (!strcmp(units[i].unit, end))
64*49cdfc7eSAndroid Build Coastguard Worker 			break;
65*49cdfc7eSAndroid Build Coastguard Worker 	}
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	if (i >= units_len) {
68*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "ERROR: Invalid interval unit '%s'\n\n", end);
69*49cdfc7eSAndroid Build Coastguard Worker 		print_help();
70*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
71*49cdfc7eSAndroid Build Coastguard Worker 	}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	if (units[i].mul == 1000000)
74*49cdfc7eSAndroid Build Coastguard Worker 		secs = interval;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	if (units[i].mul == 1000) {
77*49cdfc7eSAndroid Build Coastguard Worker 		secs = interval / 1000;
78*49cdfc7eSAndroid Build Coastguard Worker 		usecs = (interval % 1000) * 1000;
79*49cdfc7eSAndroid Build Coastguard Worker 	}
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	if (units[i].mul == 1) {
82*49cdfc7eSAndroid Build Coastguard Worker 		secs = interval / 1000000;
83*49cdfc7eSAndroid Build Coastguard Worker 		usecs = interval % 1000000;
84*49cdfc7eSAndroid Build Coastguard Worker 	}
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (secs)
87*49cdfc7eSAndroid Build Coastguard Worker 		sleep(secs);
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	if (usecs)
90*49cdfc7eSAndroid Build Coastguard Worker 		usleep(usecs);
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
93*49cdfc7eSAndroid Build Coastguard Worker }
94