2 * xcommon.c - various functions put together to avoid basic error checking.
4 * added fcntl locking by Kjetil T. (kjetilho@math.uio.no) - aeb, 950927
6 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
7 * - added Native Language Support
9 * 2006-06-06 Amit Gud <agud@redhat.com>
10 * - Moved code snippets here from mount/sundries.c of util-linux
11 * and merged code from support/nfs/xmalloc.c by Olaf Kirch <okir@monad.swb.de> here.
21 #include "nls.h" /* _() */
23 void (*at_die)(void ) = NULL;
26 xstrndup (const char *s, int n) {
30 die (EX_SOFTWARE, _("bug in xstrndup call"));
40 xstrconcat2 (const char *s, const char *t) {
45 res = xmalloc(strlen(s) + strlen(t) + 1);
51 /* frees its first arg - typical use: s = xstrconcat3(s,t,u); */
53 xstrconcat3 (const char *s, const char *t, const char *u) {
59 res = xmalloc(strlen(s) + strlen(t) + strlen(u) + 1);
67 /* frees its first arg - typical use: s = xstrconcat4(s,t,u,v); */
69 xstrconcat4 (const char *s, const char *t, const char *u, const char *v) {
76 res = xmalloc(strlen(s) + strlen(t) + strlen(u) + strlen(v) + 1);
85 /* Non-fatal error. Print message and return. */
86 /* (print the message in a single printf, in an attempt
87 to avoid mixing output of several threads) */
89 nfs_error (const char *fmt, ...) {
93 fmt2 = xstrconcat2 (fmt, "\n");
95 vfprintf (stderr, fmt2, args);
100 /* Make a canonical pathname from PATH. Returns a freshly malloced string.
101 It is up the *caller* to ensure that the PATH is sensible. i.e.
102 canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.''
103 is not a legal pathname for ``/dev/fd0''. Anything we cannot parse
104 we return unmodified. */
105 char *canonicalize (const char *path) {
106 char canonical[PATH_MAX+2];
112 if (streq(path, "none") ||
113 streq(path, "proc") ||
114 streq(path, "devpts"))
115 return xstrdup(path);
117 if (realpath (path, canonical))
118 return xstrdup(canonical);
120 return xstrdup(path);
123 /* Fatal error. Print message and exit. */
125 die(int err, const char *fmt, ...) {
129 vfprintf(stderr, fmt, args);
130 fprintf(stderr, "\n");
140 die_if_null(void *t) {
142 die(EX_SYSERR, _("not enough memory"));
146 xmalloc (size_t size) {
159 xrealloc (void *p, size_t size) {
162 t = realloc(p, size);
175 xstrdup (const char *s) {