]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
a3cf6921 | 2 | * |
7ac48069 | 3 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
4 | * For copying and distribution information, please see the file | |
5 | * <mit-copyright.h>. | |
a3cf6921 | 6 | */ |
7 | ||
c801de4c | 8 | #include <mit-copyright.h> |
d548a4e7 | 9 | #include "mr_server.h" |
7ac48069 | 10 | |
a3cf6921 | 11 | #include <ctype.h> |
85330553 | 12 | #include <stdio.h> |
7ac48069 | 13 | #include <stdlib.h> |
03c05291 | 14 | #include <string.h> |
229feb8a | 15 | |
7ac48069 | 16 | RCSID("$Header$"); |
17 | ||
229feb8a | 18 | extern char *whoami; |
19 | ||
e688520a | 20 | char *requote(char *cp) |
a3cf6921 | 21 | { |
e688520a | 22 | int len = 0; |
23 | char *out = xmalloc(4 * strlen(cp) + 3), *op = out; | |
24 | ||
25 | *op++ = '"'; | |
26 | len++; | |
27 | while (*cp) | |
5eaef520 | 28 | { |
e688520a | 29 | if (*cp == '\\' || *cp == '"') |
30 | { | |
31 | *op++ = '\\'; | |
32 | len++; | |
33 | } | |
34 | if (isprint(*cp)) | |
35 | { | |
36 | *op++ = *cp++; | |
37 | len++; | |
38 | } | |
5eaef520 | 39 | else |
40 | { | |
8192daa7 | 41 | sprintf(op, "\\%03o", (unsigned char)*cp++); |
e688520a | 42 | op += 4; |
43 | len += 4; | |
a3cf6921 | 44 | } |
5eaef520 | 45 | } |
e688520a | 46 | |
47 | strcpy(op, "\""); | |
48 | len += 2; | |
49 | ||
50 | out = realloc(out, len); /* shrinking, so can't fail */ | |
51 | return out; | |
a3cf6921 | 52 | } |
5aa981ce | 53 | |
5eaef520 | 54 | void log_args(char *tag, int version, int argc, char **argv) |
229feb8a | 55 | { |
e688520a | 56 | char *buf, **qargv; |
57 | int i, len; | |
44d12d58 | 58 | char *bp; |
5eaef520 | 59 | |
e688520a | 60 | qargv = xmalloc(argc * sizeof(char *)); |
5eaef520 | 61 | |
e688520a | 62 | for (i = len = 0; i < argc; i++) |
5eaef520 | 63 | { |
e688520a | 64 | qargv[i] = requote(argv[i]); |
65 | len += strlen(qargv[i]) + 2; | |
5eaef520 | 66 | } |
e688520a | 67 | |
68 | buf = xmalloc(len + 1); | |
69 | ||
70 | for (i = 0, *buf = '\0'; i < argc; i++) | |
71 | { | |
72 | if (i) | |
73 | strcat(buf, ", "); | |
74 | strcat(buf, qargv[i]); | |
75 | free(qargv[i]); | |
76 | } | |
77 | free(qargv); | |
78 | ||
79 | com_err(whoami, 0, "%s[%d]: %s", tag, version, buf); | |
80 | free(buf); | |
229feb8a | 81 | } |
5eaef520 | 82 | |
83 | void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar) | |
a3cf6921 | 84 | { |
5eaef520 | 85 | extern client *cur_client; |
86 | ||
87 | if (whoami) | |
88 | { | |
89 | fputs(whoami, stderr); | |
90 | if (cur_client) | |
91 | fprintf(stderr, "[#%d]", cur_client->id); | |
92 | fputs(": ", stderr); | |
93 | } | |
85330553 | 94 | if (code) { |
5eaef520 | 95 | fputs(error_message(code), stderr); |
85330553 | 96 | fputs(" ", stderr); |
97 | } | |
5eaef520 | 98 | if (fmt) |
7ac48069 | 99 | vfprintf(stderr, fmt, pvar); |
5eaef520 | 100 | putc('\n', stderr); |
a3cf6921 | 101 | } |
2423a5db | 102 | |
103 | ||
d548a4e7 | 104 | /* mr_trim_args: passed an argument vector, it will trim any trailing |
e2fe4db4 | 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. | |
2423a5db | 107 | */ |
108 | ||
5eaef520 | 109 | int mr_trim_args(int argc, char **argv) |
2423a5db | 110 | { |
44d12d58 | 111 | char **arg; |
112 | unsigned char *p, *lastch; | |
5eaef520 | 113 | |
114 | for (arg = argv; argc--; arg++) | |
115 | { | |
116 | for (lastch = p = (unsigned char *) *arg; *p; p++) | |
117 | { | |
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. | |
122 | */ | |
123 | if (*p >= 0x80) | |
124 | { | |
125 | p = lastch; | |
126 | break; | |
e2fe4db4 | 127 | } |
5eaef520 | 128 | if (!isspace(*p)) |
129 | lastch = p; | |
e2fe4db4 | 130 | } |
5eaef520 | 131 | if (p != lastch) |
132 | { | |
133 | if (isspace(*lastch)) | |
134 | *lastch = '\0'; | |
135 | else | |
136 | if (*(++lastch)) | |
137 | *lastch = '\0'; | |
2423a5db | 138 | } |
139 | } | |
5eaef520 | 140 | return 0; |
2423a5db | 141 | } |
71007162 | 142 | |
143 | ||
144 | /* returns a copy of the argv and all of it's strings */ | |
145 | ||
5eaef520 | 146 | char **mr_copy_args(char **argv, int argc) |
71007162 | 147 | { |
5eaef520 | 148 | char **a; |
149 | int i; | |
150 | ||
e688520a | 151 | a = xmalloc(argc * sizeof(char *)); |
5eaef520 | 152 | for (i = 0; i < argc; i++) |
e688520a | 153 | a[i] = xstrdup(argv[i]); |
5eaef520 | 154 | return a; |
71007162 | 155 | } |
85330553 | 156 | |
157 | ||
158 | /* malloc or die! */ | |
159 | void *xmalloc(size_t bytes) | |
160 | { | |
161 | void *buf = malloc(bytes); | |
162 | ||
163 | if (buf) | |
164 | return buf; | |
165 | ||
166 | critical_alert("moirad", "Out of memory"); | |
167 | exit(1); | |
168 | } | |
169 | ||
170 | void *xrealloc(void *ptr, size_t bytes) | |
171 | { | |
172 | void *buf = realloc(ptr, bytes); | |
173 | ||
174 | if (buf) | |
175 | return buf; | |
176 | ||
177 | critical_alert("moirad", "Out of memory"); | |
178 | exit(1); | |
179 | } | |
e688520a | 180 | |
181 | char *xstrdup(char *str) | |
182 | { | |
183 | char *buf = strdup(str); | |
184 | ||
185 | if (buf) | |
186 | return buf; | |
187 | ||
188 | critical_alert("moirad", "Out of memory"); | |
189 | exit(1); | |
190 | } |