-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_ctime.c
75 lines (64 loc) · 1.64 KB
/
c_ctime.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
/*
/usr/include/time.h
extern char *asctime (const struct tm *__tp) __THROW;
Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
that is the representation of TP in this format.
extern char *ctime (const time_t *__timer) __THROW;
Equibufent to `asctime (localtime (timer))'.
Ex: TIM = 1583171848 --> STR = 'Mon Mar 2 18:01:04 2020'
NOTE: ctime may exceed the max-length of PTR. Use strftime instead.
*/
#include <time.h>
#include <string.h>
#include <errno.h>
void c_ctime_ ( time_t *tim, char *buf, size_t *len_buf, size_t *lentrim_buf )
{
char *ptr = ctime ( tim ) ;
if ( ptr == NULL )
{
/*
The provided name does not exists as an env-varable. Exit and Report.
*/
*lentrim_buf = 0 ;
errno = EDOM ;
}
else
{
/*
We need to not only remove
\0 (NULL)
character which marks the end of C-strings, but also to remove
\n (Newline)
character, which is fro mthe output of ctime(). So the output
lentrim_buf
should be subtracted from 1.
*/
size_t dump = strlen ( ptr ) ;
size_t dumv = *len_buf - 1 ;
strncpy ( buf, ptr, dumv ) ;
/*
Perfoming
*lentrim_buf = ( dump > dumv )? dumv : dump ;
*/
if ( dump > dumv )
{
/*
The room of VAL is so small that it cannot store PTR. Increase it.
Report:
errno = ERANGE
*/
*lentrim_buf = dumv - 1 ;
errno = ERANGE ;
}
else
{
/*
Normal exit.
Report:
errno = 0
*/
*lentrim_buf = dump - 1 ;
errno = 0 ;
}
}
}