-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeliver.c
66 lines (56 loc) · 1.5 KB
/
deliver.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
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "util.h"
static const char *hostname(void) {
static char *hostname;
struct utsname utsname;
struct addrinfo *info;
const struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_flags = AI_CANONNAME
};
if (hostname == NULL && uname(&utsname) == 0) {
if (!strchr(utsname.nodename, '.')
&& !getaddrinfo(utsname.nodename, NULL, &hints, &info)
&& info != NULL) {
hostname = strdup(info->ai_canonname);
freeaddrinfo(info);
} else {
hostname = strdup(utsname.nodename);
}
}
return hostname ? hostname : "localhost";
}
int main(int argc, char **argv) {
char buffer[1 << 20], name[256];
struct timeval now;
size_t length;
int fd;
if (argc > 2 || isatty(0)) {
fprintf(stderr, "Usage: %s [TMPDIR] < MESSAGE\n", argv[0]);
return 64;
}
if (argc > 1 && chdir(argv[1]) < 0)
err(1, "chdir %s", argv[1]);
if (gettimeofday(&now, NULL) < 0)
err(1, "gettimeofday");
snprintf(name, sizeof name, "%ld.M%06ldP%d.%s", now.tv_sec, now.tv_usec,
getpid(), hostname());
if ((fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600)) < 0)
err(1, "open %s", name);
while ((length = get(0, buffer, sizeof(buffer))))
put(fd, buffer, length);
fsync(fd);
close(fd);
if (argc > 1)
printf("%s/", argv[1]);
printf("%s\n", name);
return 0;
}