Mercurial > hg > toybox
annotate lib/pending.c @ 1172:db517206d752 draft
Cosmetic tweak.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 28 Dec 2013 17:06:55 -0600 |
parents | 80c9df5145fe |
children | d48bdc1cb017 |
rev | line source |
---|---|
949
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* pending.c - reusable stuff awaiting review |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
2 * |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
3 * new lib entries for stuff in toys/pending |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
4 */ |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
6 #include "toys.h" |
59d4d453296b
New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 |
995
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
8 void daemonize(void) |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
9 { |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
10 int fd = open("/dev/null", O_RDWR); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
11 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
12 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
13 pid_t pid = fork(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
14 if (pid < 0) perror_exit("DAEMON: failed to fork"); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
15 if (pid) exit(EXIT_SUCCESS); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
16 |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
17 setsid(); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
18 dup2(fd, 0); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
19 dup2(fd, 1); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
20 dup2(fd, 2); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
21 if (fd > 2) close(fd); |
893c86bbe452
Add daemonize function to lib for klogd and syslogd
Felix Janda <felix.janda@posteo.de>
parents:
949
diff
changeset
|
22 } |
1116 | 23 |
24 char *human_readable(unsigned long long size) | |
25 { | |
26 static char buf[32]; | |
27 char *tmp = (buf+4); //unsigned long long can come in 20byte string. | |
28 int index, sz; | |
29 | |
30 for (index = 0; 1024 < size>>(10*index); index++); | |
31 sz = size>>(10*index); | |
32 if (sz < 10 && index) { | |
33 sprintf(tmp, "%llu", size>>(10*(index-1))); | |
34 sprintf(buf, "%c.%c", tmp[0], tmp[1]); | |
35 } else sprintf(buf, "%u", sz); | |
36 sprintf(buf, "%s%c", buf, " KMGTPE"[index]); | |
37 return buf; | |
38 } |