3 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
4 * For copying and distribution information, please see the file
8 #include <mit-copyright.h>
20 char *requote(char *buf, char *cp, int len)
29 for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count)
31 if (c == '\\' || c == '"')
37 sprintf(buf, "\\%03o", c);
38 buf = strchr(buf, '\0');
52 void log_args(char *tag, int version, int argc, char **argv)
59 sprintf(buf, "%s[%d]: ", tag, version);
60 for (bp = buf; *bp; bp++)
63 for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++)
70 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
73 com_err(whoami, 0, "%s", buf);
76 void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
78 extern client *cur_client;
82 fputs(whoami, stderr);
84 fprintf(stderr, "[#%d]", cur_client->id);
88 fputs(error_message(code), stderr);
92 vfprintf(stderr, fmt, pvar);
97 /* mr_trim_args: passed an argument vector, it will trim any trailing
98 * spaces on the args by writing a null into the string. If an argument
99 * appears to be binary instead of ASCII, it will not be trimmed.
102 int mr_trim_args(int argc, char **argv)
105 unsigned char *p, *lastch;
107 for (arg = argv; argc--; arg++)
109 for (lastch = p = (unsigned char *) *arg; *p; p++)
111 /* If any byte in the string has the high bit set, assume
112 * that it is binary and we do not want to trim it.
113 * Setting p = lastch will cause us not to trim the string
114 * when we break out of this inner loop.
126 if (isspace(*lastch))
137 /* returns a copy of the argv and all of it's strings */
139 char **mr_copy_args(char **argv, int argc)
144 a = malloc(argc * sizeof(char *));
147 for (i = 0; i < argc; i++)
148 a[i] = strdup(argv[i]);
154 void *xmalloc(size_t bytes)
156 void *buf = malloc(bytes);
161 critical_alert("moirad", "Out of memory");
165 void *xrealloc(void *ptr, size_t bytes)
167 void *buf = realloc(ptr, bytes);
172 critical_alert("moirad", "Out of memory");