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