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.
108 int mr_trim_args(int argc, char **argv)
111 unsigned char *p, *lastch;
113 for (arg = argv; argc--; arg++)
115 for (lastch = p = (unsigned char *) *arg; *p; p++)
122 if (isspace(*lastch))
133 /* returns a copy of the argv and all of it's strings */
135 char **mr_copy_args(char **argv, int argc)
140 a = xmalloc(argc * sizeof(char *));
141 for (i = 0; i < argc; i++)
142 a[i] = xstrdup(argv[i]);
148 void *xmalloc(size_t bytes)
150 void *buf = malloc(bytes);
155 critical_alert(whoami, "moirad", "Out of memory");
159 void *xrealloc(void *ptr, size_t bytes)
161 void *buf = realloc(ptr, bytes);
166 critical_alert(whoami, "moirad", "Out of memory");
170 char *xstrdup(char *str)
172 char *buf = strdup(str);
177 critical_alert(whoami, "moirad", "Out of memory");