6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
12 static char *rcsid_mr_util_c = "$Header$";
15 #include <mit-copyright.h>
16 #include "mr_server.h"
19 #include <sys/types.h>
24 char *requote(char *buf, register char *cp, int len)
26 register int count = 0;
27 register unsigned char c;
33 for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count)
35 if (c == '\\' || c == '"')
41 sprintf(buf, "\\%03o", c);
42 buf = strchr(buf, '\0');
56 void log_args(char *tag, int version, int argc, char **argv)
63 sprintf(buf, "%s[%d]: ", tag, version);
64 for (bp = buf; *bp; bp++)
67 for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++)
74 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
77 com_err(whoami, 0, "%s", buf);
80 void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
82 extern client *cur_client;
86 fputs(whoami, stderr);
88 fprintf(stderr, "[#%d]", cur_client->id);
92 fputs(error_message(code), stderr);
94 _doprnt(fmt, pvar, stderr);
99 /* mr_trim_args: passed an argument vector, it will trim any trailing
100 * spaces on the args by writing a null into the string. If an argument
101 * appears to be binary instead of ASCII, it will not be trimmed.
104 int mr_trim_args(int argc, char **argv)
107 register unsigned char *p, *lastch;
109 for (arg = argv; argc--; arg++)
111 for (lastch = p = (unsigned char *) *arg; *p; p++)
113 /* If any byte in the string has the high bit set, assume
114 * that it is binary and we do not want to trim it.
115 * Setting p = lastch will cause us not to trim the string
116 * when we break out of this inner loop.
128 if (isspace(*lastch))
139 /* returns a copy of the argv and all of it's strings */
141 char **mr_copy_args(char **argv, int argc)
146 a = malloc(argc * sizeof(char *));
149 for (i = 0; i < argc; i++)
150 a[i] = strsave(argv[i]);