1 /* Copyright (c) 2000, Len Budney. See COPYING for details. */
2
3 #include "hostname.h"
4 #include "stralloc.h"
5 #include "strerr.h"
6 #include "tai.h"
7 #include "taia.h"
8 #include "fmt.h"
9 #include <unistd.h>
10
11 static
die_nomem()12 void die_nomem() { strerr_die2x(111,"safecat: fatal: ","out of memory"); }
13
14 /* ****************************************************************** */
mk_tempfile(stralloc * tmpf)15 void mk_tempfile(stralloc *tmpf) {
16 char host[256];
17 char secbuf[11];
18 char atto[TAIA_FMTFRAC];
19 char pidbuf[6];
20 struct taia now;
21 struct tai sec;
22 unsigned long int pid = (unsigned long)getpid();
23
24 /* Get a microsecond timestamp with which to build a filename. */
25 taia_now(&now);
26 taia_tai(&now,&sec);
27
28 /* Record the second timestamp on the string. */
29 secbuf[fmt_uint64(secbuf,(uint64) sec.x - 4611686018427387904ULL)] = '\0';
30 if (!stralloc_cats(tmpf, secbuf)) die_nomem();
31
32 /* Append the microsecond timestamp to the string. */
33 if (!stralloc_cats(tmpf, ".M")) die_nomem();
34 taia_fmtfrac(atto,&now);
35 atto[6] = '\0'; /* truncate at microsecond */
36 if (!stralloc_cats(tmpf, atto)) die_nomem();
37
38 /* Append the PID to the string. */
39 if (!stralloc_append(tmpf, "P")) die_nomem();
40 pidbuf[fmt_uint64(pidbuf, pid)] = '\0';
41 if (!stralloc_cats(tmpf, pidbuf)) die_nomem();
42
43 /* Copy the hostname to the buffer. */
44 if (!stralloc_append(tmpf, ".")) die_nomem();
45 get_hostname(host,sizeof(host));
46 if (!stralloc_cats(tmpf, host)) die_nomem();
47 if (!stralloc_0(tmpf)) die_nomem();
48 }
49 /* ****************************************************************** */
50
51
52