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 *cp)
23 char *out = xmalloc(4 * strlen(cp) + 3), *op = out;
29 if (*cp == '\\' || *cp == '"')
41 sprintf(op, "\\%03o", (unsigned char)*cp++);
50 out = realloc(out, len); /* shrinking, so can't fail */
54 void log_args(char *tag, int version, int argc, char **argv)
60 qargv = xmalloc(argc * sizeof(char *));
62 for (i = len = 0; i < argc; i++)
64 qargv[i] = requote(argv[i]);
65 len += strlen(qargv[i]) + 2;
68 buf = xmalloc(len + 1);
70 for (i = 0, *buf = '\0'; i < argc; i++)
74 strcat(buf, qargv[i]);
79 com_err(whoami, 0, "%s[%d]: %s", tag, version, buf);
83 void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
85 extern client *cur_client;
89 fputs(whoami, stderr);
91 fprintf(stderr, "[#%d]", cur_client->id);
95 fputs(error_message(code), stderr);
99 vfprintf(stderr, fmt, pvar);
104 /* mr_trim_args: passed an argument vector, it will trim any trailing
105 * spaces on the args by writing a null into the string. If an argument
106 * appears to be binary instead of ASCII, it will not be trimmed.
109 int mr_trim_args(int argc, char **argv)
112 unsigned char *p, *lastch;
114 for (arg = argv; argc--; arg++)
116 for (lastch = p = (unsigned char *) *arg; *p; p++)
118 /* If any byte in the string has the high bit set, assume
119 * that it is binary and we do not want to trim it.
120 * Setting p = lastch will cause us not to trim the string
121 * when we break out of this inner loop.
133 if (isspace(*lastch))
144 /* returns a copy of the argv and all of it's strings */
146 char **mr_copy_args(char **argv, int argc)
151 a = xmalloc(argc * sizeof(char *));
152 for (i = 0; i < argc; i++)
153 a[i] = xstrdup(argv[i]);
159 void *xmalloc(size_t bytes)
161 void *buf = malloc(bytes);
166 critical_alert("moirad", "Out of memory");
170 void *xrealloc(void *ptr, size_t bytes)
172 void *buf = realloc(ptr, bytes);
177 critical_alert("moirad", "Out of memory");
181 char *xstrdup(char *str)
183 char *buf = strdup(str);
188 critical_alert("moirad", "Out of memory");