1 | /* $Id: mr_util.c,v 1.34 2009-06-01 21:05:02 zacheiss Exp $ |
---|
2 | * |
---|
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>. |
---|
6 | */ |
---|
7 | |
---|
8 | #include <mit-copyright.h> |
---|
9 | #include "mr_server.h" |
---|
10 | |
---|
11 | #include <ctype.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | |
---|
16 | RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/server/mr_util.c,v 1.34 2009-06-01 21:05:02 zacheiss Exp $"); |
---|
17 | |
---|
18 | extern char *whoami; |
---|
19 | |
---|
20 | char *requote(char *cp) |
---|
21 | { |
---|
22 | int len = 0; |
---|
23 | char *out = xmalloc(4 * strlen(cp) + 3), *op = out; |
---|
24 | |
---|
25 | *op++ = '"'; |
---|
26 | len++; |
---|
27 | while (*cp) |
---|
28 | { |
---|
29 | if (*cp == '\\' || *cp == '"') |
---|
30 | { |
---|
31 | *op++ = '\\'; |
---|
32 | len++; |
---|
33 | } |
---|
34 | if (isprint(*cp)) |
---|
35 | { |
---|
36 | *op++ = *cp++; |
---|
37 | len++; |
---|
38 | } |
---|
39 | else |
---|
40 | { |
---|
41 | sprintf(op, "\\%03o", (unsigned char)*cp++); |
---|
42 | op += 4; |
---|
43 | len += 4; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | strcpy(op, "\""); |
---|
48 | len += 2; |
---|
49 | |
---|
50 | out = realloc(out, len); /* shrinking, so can't fail */ |
---|
51 | return out; |
---|
52 | } |
---|
53 | |
---|
54 | void log_args(char *tag, int version, int argc, char **argv) |
---|
55 | { |
---|
56 | char *buf, **qargv; |
---|
57 | int i, len; |
---|
58 | char *bp; |
---|
59 | |
---|
60 | qargv = xmalloc(argc * sizeof(char *)); |
---|
61 | |
---|
62 | for (i = len = 0; i < argc; i++) |
---|
63 | { |
---|
64 | qargv[i] = requote(argv[i]); |
---|
65 | len += strlen(qargv[i]) + 2; |
---|
66 | } |
---|
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); |
---|
81 | } |
---|
82 | |
---|
83 | void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar) |
---|
84 | { |
---|
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 | } |
---|
94 | if (code) { |
---|
95 | fputs(error_message(code), stderr); |
---|
96 | fputs(" ", stderr); |
---|
97 | } |
---|
98 | if (fmt) |
---|
99 | vfprintf(stderr, fmt, pvar); |
---|
100 | putc('\n', stderr); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
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. |
---|
106 | */ |
---|
107 | |
---|
108 | int mr_trim_args(int argc, char **argv) |
---|
109 | { |
---|
110 | char **arg; |
---|
111 | unsigned char *p, *lastch; |
---|
112 | |
---|
113 | for (arg = argv; argc--; arg++) |
---|
114 | { |
---|
115 | for (lastch = p = (unsigned char *) *arg; *p; p++) |
---|
116 | { |
---|
117 | if (!isspace(*p)) |
---|
118 | lastch = p; |
---|
119 | } |
---|
120 | if (p != lastch) |
---|
121 | { |
---|
122 | if (isspace(*lastch)) |
---|
123 | *lastch = '\0'; |
---|
124 | else |
---|
125 | if (*(++lastch)) |
---|
126 | *lastch = '\0'; |
---|
127 | } |
---|
128 | } |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /* returns a copy of the argv and all of it's strings */ |
---|
134 | |
---|
135 | char **mr_copy_args(char **argv, int argc) |
---|
136 | { |
---|
137 | char **a; |
---|
138 | int i; |
---|
139 | |
---|
140 | a = xmalloc(argc * sizeof(char *)); |
---|
141 | for (i = 0; i < argc; i++) |
---|
142 | a[i] = xstrdup(argv[i]); |
---|
143 | return a; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | /* malloc or die! */ |
---|
148 | void *xmalloc(size_t bytes) |
---|
149 | { |
---|
150 | void *buf = malloc(bytes); |
---|
151 | |
---|
152 | if (buf) |
---|
153 | return buf; |
---|
154 | |
---|
155 | critical_alert(whoami, "moirad", "Out of memory"); |
---|
156 | exit(1); |
---|
157 | } |
---|
158 | |
---|
159 | void *xrealloc(void *ptr, size_t bytes) |
---|
160 | { |
---|
161 | void *buf = realloc(ptr, bytes); |
---|
162 | |
---|
163 | if (buf) |
---|
164 | return buf; |
---|
165 | |
---|
166 | critical_alert(whoami, "moirad", "Out of memory"); |
---|
167 | exit(1); |
---|
168 | } |
---|
169 | |
---|
170 | char *xstrdup(char *str) |
---|
171 | { |
---|
172 | char *buf = strdup(str); |
---|
173 | |
---|
174 | if (buf) |
---|
175 | return buf; |
---|
176 | |
---|
177 | critical_alert(whoami, "moirad", "Out of memory"); |
---|
178 | exit(1); |
---|
179 | } |
---|