]> andersk Git - moira.git/blame - server/mr_util.c
don't overwrite '\0' with '\0' in mr_trim_args. lets us trim constant
[moira.git] / server / mr_util.c
CommitLineData
a3cf6921 1/*
2 * $Source$
3 * $Author$
4 * $Header$
5 *
6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
c801de4c 7 * For copying and distribution information, please see the file
8 * <mit-copyright.h>.
a3cf6921 9 */
10
11#ifndef lint
d548a4e7 12static char *rcsid_mr_util_c = "$Header$";
a3cf6921 13#endif lint
14
c801de4c 15#include <mit-copyright.h>
d548a4e7 16#include "mr_server.h"
960b073b 17#include <com_err.h>
a3cf6921 18#include <ctype.h>
c93d352e 19#include <sys/types.h>
03c05291 20#include <string.h>
229feb8a 21
22extern char *whoami;
23
229feb8a 24char *
5aa981ce 25requote(buf, cp, len)
229feb8a 26 char *buf;
a3cf6921 27 register char *cp;
28{
5aa981ce 29 register int count = 0;
caeb1b25 30 register unsigned char c;
5aa981ce 31 if (len <= 2) return buf;
32 *buf++ = '"'; count++; len--;
3b2aae4c 33 for(; (count < 258) && (len > 1) && (c = *cp);
5aa981ce 34 cp++, --len, ++count) {
229feb8a 35 if (c == '\\' || c == '"') *buf++ = '\\';
36 if (isprint(c)) *buf++ = c;
37 else {
38 sprintf(buf, "\\%03o", c);
03c05291 39 buf = strchr(buf, '\0');
229feb8a 40 }
a3cf6921 41 }
5aa981ce 42 if (len > 1) { *buf++ = '"'; count++; len--; }
5aa981ce 43 if (len > 1) *buf = '\0';
229feb8a 44 return buf;
a3cf6921 45}
5aa981ce 46
03c05291 47void log_args(tag, version, argc, argv)
ac65c1d1 48 char *tag;
2423a5db 49 int version;
229feb8a 50 int argc;
51 char **argv;
52{
53 char buf[BUFSIZ];
54 register int i;
2423a5db 55 register char *bp;
229feb8a 56
ac65c1d1 57 i = strlen(tag);
2423a5db 58 sprintf(buf, "%s[%d]: ", tag, version);
59 for (bp = buf; *bp; bp++);
ac65c1d1 60
3b2aae4c 61 for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++) {
229feb8a 62 if (i != 0) {
63 *bp++ = ',';
64 *bp++ = ' ';
65 }
3b2aae4c 66 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
229feb8a 67 }
68 *bp = '\0';
5cc7d26c 69 com_err(whoami, 0, "%s", buf);
229feb8a 70}
71
d548a4e7 72void mr_com_err(whoami, code, fmt, pvar)
98a7b0ee 73 const char *whoami;
74 long code;
75 const char *fmt;
76 va_list pvar;
a3cf6921 77{
a3cf6921 78 extern client *cur_client;
79
a3cf6921 80 if (whoami) {
b4182127 81 fputs(whoami, stderr);
82 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
83 fputs(": ", stderr);
a3cf6921 84 }
85 if (code) {
b4182127 86 fputs(error_message(code), stderr);
87 }
88 if (fmt) {
89 _doprnt(fmt, pvar, stderr);
a3cf6921 90 }
b4182127 91 putc('\n', stderr);
a3cf6921 92}
2423a5db 93
94
d548a4e7 95/* mr_trim_args: passed an argument vector, it will trim any trailing
e2fe4db4 96 * spaces on the args by writing a null into the string. If an argument
97 * appears to be binary instead of ASCII, it will not be trimmed.
2423a5db 98 */
99
d548a4e7 100int mr_trim_args(argc, argv)
2423a5db 101int argc;
102char **argv;
103{
104 register char **arg;
30517356 105 register unsigned char *p, *lastch;
2423a5db 106
107 for (arg = argv; argc--; arg++) {
245f8a92 108 for (lastch = p = (unsigned char *) *arg; *p; p++) {
e2fe4db4 109 /* If any byte in the string has the high bit set, assume
110 * that it is binary and we do not want to trim it.
111 * Setting p = lastch will cause us not to trim the string
112 * when we break out of this inner loop.
113 */
114 if (*p >= 0x80) {
115 p = lastch;
116 break;
117 }
118 if (!isspace(*p))
119 lastch = p;
120 }
2423a5db 121 if (p != lastch) {
122 if (isspace(*lastch))
123 *lastch = 0;
124 else
73d8ff1e 125 if (*(++lastch)) *lastch = 0;
2423a5db 126 }
127 }
128 return(0);
129}
71007162 130
131
132/* returns a copy of the argv and all of it's strings */
133
134char **mr_copy_args(argv, argc)
135char **argv;
136int argc;
137{
138 char **a;
139 int i;
140
141 a = (char **) malloc(argc * sizeof(char *));
142 if (a == 0)
143 return(a);
144 for (i = 0; i < argc; i++)
145 a[i] = strsave(argv[i]);
146 return(a);
147}
This page took 3.07743 seconds and 5 git commands to generate.