Mercurial > hg > toybox
comparison lib/lib.c @ 156:1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
tweaks.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 15 Nov 2007 18:30:30 -0600 |
parents | 5f206fe8d965 |
children | f16c8e5e9435 |
comparison
equal
deleted
inserted
replaced
155:6c7836dbc16e | 156:1e8f4b05cb65 |
---|---|
115 // Die unless we can allocate a copy of this many bytes of string. | 115 // Die unless we can allocate a copy of this many bytes of string. |
116 void *xstrndup(char *s, size_t n) | 116 void *xstrndup(char *s, size_t n) |
117 { | 117 { |
118 void *ret = xmalloc(++n); | 118 void *ret = xmalloc(++n); |
119 strlcpy(ret, s, n); | 119 strlcpy(ret, s, n); |
120 | 120 |
121 return ret; | 121 return ret; |
122 } | 122 } |
123 | 123 |
124 // Die unless we can allocate a copy of this string. | 124 // Die unless we can allocate a copy of this string. |
125 void *xstrdup(char *s) | 125 void *xstrdup(char *s) |
131 char *xmsprintf(char *format, ...) | 131 char *xmsprintf(char *format, ...) |
132 { | 132 { |
133 va_list va, va2; | 133 va_list va, va2; |
134 int len; | 134 int len; |
135 char *ret; | 135 char *ret; |
136 | 136 |
137 va_start(va, format); | 137 va_start(va, format); |
138 va_copy(va2, va); | 138 va_copy(va2, va); |
139 | 139 |
140 // How long is it? | 140 // How long is it? |
141 len = vsnprintf(0, 0, format, va); | 141 len = vsnprintf(0, 0, format, va); |
142 len++; | 142 len++; |
143 va_end(va); | 143 va_end(va); |
144 | 144 |
145 // Allocate and do the sprintf() | 145 // Allocate and do the sprintf() |
146 ret = xmalloc(len); | 146 ret = xmalloc(len); |
147 vsnprintf(ret, len, format, va2); | 147 vsnprintf(ret, len, format, va2); |
148 va_end(va2); | 148 va_end(va2); |
149 | 149 |
150 return ret; | 150 return ret; |
151 } | 151 } |
152 | 152 |
297 continue; | 297 continue; |
298 } | 298 } |
299 | 299 |
300 // Skip duplicate slashes. | 300 // Skip duplicate slashes. |
301 while (*from=='/') from++; | 301 while (*from=='/') from++; |
302 | 302 |
303 // Start of a new filename. Handle . and .. | 303 // Start of a new filename. Handle . and .. |
304 while (*from=='.') { | 304 while (*from=='.') { |
305 // Skip . | 305 // Skip . |
306 if (from[1]=='/') from += 2; | 306 if (from[1]=='/') from += 2; |
307 else if (!from[1]) from++; | 307 else if (!from[1]) from++; |
503 { | 503 { |
504 off_t len; | 504 off_t len; |
505 int fd; | 505 int fd; |
506 char *buf; | 506 char *buf; |
507 | 507 |
508 fd = open(pidfile, O_RDONLY); | 508 fd = open(name, O_RDONLY); |
509 if (fd == -1) return 0; | 509 if (fd == -1) return 0; |
510 len = fdlength(fd); | 510 len = fdlength(fd); |
511 buf = xmalloc(len+1); | 511 buf = xmalloc(len+1); |
512 buf[xread(fd, buf, len)] = 0; | 512 buf[xread(fd, buf, len)] = 0; |
513 | 513 |
547 pid = atoi(spid); | 547 pid = atoi(spid); |
548 if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); | 548 if (fd < 1 || kill(pid, 0) == ESRCH) unlink(pidfile); |
549 | 549 |
550 // An else with more sanity checking might be nice here. | 550 // An else with more sanity checking might be nice here. |
551 } | 551 } |
552 | 552 |
553 if (i == 3) error_exit("xpidfile %s", name); | 553 if (i == 3) error_exit("xpidfile %s", name); |
554 | 554 |
555 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); | 555 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
556 close(fd); | 556 close(fd); |
557 } | 557 } |