-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-stat-size.c
74 lines (61 loc) · 1.26 KB
/
file-stat-size.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
/*
* Synopsis:
* Write the file size in bytes on standard output
* Usage:
* file-stat-size <path-to-file>
* Exit Status
* 0 size written to standard out
* 1 unexpected error
* Note:
* Why not use GNU "stat --format '%s'" instead of file-stat-size.
*/
#include <sys/errno.h>
#include <string.h>
#include "jmscott/libjmscott.h"
#define EXIT_OK 0
#define EXIT_FAULT 1
extern int errno;
char* jmscott_progname = "file-stat-size";
static void
die(char *msg)
{
jmscott_die(EXIT_FAULT, msg);
}
static void
die2(char *msg1, char *msg2)
{
jmscott_die2(EXIT_FAULT, msg1, msg2);
}
static void
die3(char *msg1, char *msg2, char *msg3)
{
jmscott_die3(EXIT_FAULT, msg1, msg2, msg3);
}
static void
_write(void *p, ssize_t nbytes)
{
if (jmscott_write_all(1, p, nbytes) < 0)
die2("write(stdout) failed", strerror(errno));
}
int
main(int argc, char **argv)
{
char *path;
struct stat st;
char digits[128], *p;
if (argc != 2)
die("wrong number of arguments");
path = argv[1];
/*
* Stat the file to get the size.
*/
if (jmscott_stat(path, &st) != 0)
die3(path, "stat() failed", strerror(errno));
/*
* Convert size to decimal digit string.
*/
p = jmscott_ulltoa((unsigned long long)st.st_size, digits);
*p++ = '\n';
_write(digits, p - digits);
_exit(0);
}