3 * Useful very-low-level utility routines, used in both client and daemon.
4 * These do not (and cannot) depend on infrastructure eg syscallerror,
5 * because these are not the same.
8 * Copyright 1996-2017 Ian Jackson <ian@davenant.greenend.org.uk>.
9 * Copyright 2000 Ben Harris <bjh21@cam.ac.uk>
10 * Copyright 2016-2017 Peter Benie <pjb1008@cam.ac.uk>
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with userv; if not, see <http://www.gnu.org/licenses/>.
26 /* Some nasty people can return 0/EOF + EINTR from stdio !
27 * These functions attempt to work around this braindamage by retrying
28 * the call after clearerr. If this doesn't work then clearly your
29 * libc is _completely_ fubar rather than just somewhat fubar.
40 void *xmalloc(size_t s) {
42 p= malloc(s?s:1); if (!p) syscallerror("malloc");
46 void *xrealloc(void *p, size_t s) {
48 if (!p) syscallerror("realloc");
52 char *xstrsave(const char *s) {
55 r= xmalloc(strlen(s)+1);
61 int working_getc(FILE *file) {
66 if (c != EOF || errno != EINTR) return c;
71 size_t working_fread(void *ptr, size_t sz, FILE *file) {
76 nr= fread((char*)ptr + done, 1, sz-done, file);
78 if (done == sz || !ferror(file) || errno != EINTR) return done;