-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleep.c
99 lines (85 loc) · 3.4 KB
/
sleep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*####################################################################
#
# SLEEP - Sleep Command Which Supported Non-Integer Numbers
#
# USAGE : sleep seconds
# Args : seconds ... The number of second to sleep for. You can
# give not only an integer number but also a
# non-integer number here.
# Retuen : Return 0 only when succeeded to sleep
#
# How to compile : cc -O3 -o __CMDNAME__ __SRCNAME__
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2024-06-23
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/tokideli
#
####################################################################*/
/*####################################################################
# Initial Configuration
####################################################################*/
/*=== Initial Setting ==============================================*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
char* gpszCmdname;
/*=== Define the functions for printing usage and error ============*/
void print_usage_and_exit(void) {
fprintf(stderr,
"USAGE : %s seconds\n"
"Args : seconds ... The number of second to sleep for. You can\n"
" give not only an integer number but also a\n"
" non-integer number here.\n"
"Retuen : Return 0 only when succeeded to sleep\n"
"Version : 2024-06-23 13:28:01 JST\n"
" (POSIX C language)\n"
"\n"
"Shell-Shoccar Japan (@shellshoccarjpn), No rights reserved.\n"
"This is public domain software. (CC0)\n"
"\n"
"The latest version is distributed at the following page.\n"
"https://github.com/ShellShoccar-jpn/tokideli\n"
,gpszCmdname);
exit(1);
}
void error_exit(int iErrno, const char* szFormat, ...) {
va_list va;
va_start(va, szFormat);
fprintf(stderr,"%s: ",gpszCmdname);
vfprintf(stderr,szFormat,va);
va_end(va);
exit(iErrno);
}
/*####################################################################
# Main
####################################################################*/
int main(int argc, char *argv[]) {
/*=== Initial Setting ============================================*/
struct timespec tspcSleeping_time;
double dNum;
int i,iRet;
gpszCmdname = argv[0];
for (i=0; *(gpszCmdname+i)!='\0'; i++) {
if (*(gpszCmdname+i)=='/') {gpszCmdname=gpszCmdname+i+1;}
}
/*=== Parse options ==============================================*/
if (argc != 2 ) {print_usage_and_exit();}
if (sscanf(argv[1], "%lf", &dNum) != 1) {print_usage_and_exit();}
if (dNum > INT_MAX ) {print_usage_and_exit();}
/*=== Sleep ======================================================*/
if (dNum <= 0 ) {exit(0); }
tspcSleeping_time.tv_sec = (time_t)dNum;
tspcSleeping_time.tv_nsec = (dNum - tspcSleeping_time.tv_sec) * 1000000000;
iRet = nanosleep(&tspcSleeping_time, NULL);
if (iRet != 0) {error_exit(iRet,"Error happend while nanosleeping\n");}
/*=== Finish =====================================================*/
return 0;
}