]> andersk Git - moira.git/blame - server/mr_util.c
punt mrgdb
[moira.git] / server / mr_util.c
CommitLineData
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 16RCSID("$Header$");
17
229feb8a 18extern char *whoami;
19
44d12d58 20char *requote(char *buf, char *cp, int len)
a3cf6921 21{
44d12d58 22 int count = 0;
23 unsigned char c;
5eaef520 24 if (len <= 2)
25 return buf;
26 *buf++ = '"';
27 count++;
28 len--;
29 for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count)
30 {
31 if (c == '\\' || c == '"')
32 *buf++ = '\\';
33 if (isprint(c))
34 *buf++ = c;
35 else
36 {
37 sprintf(buf, "\\%03o", c);
38 buf = strchr(buf, '\0');
a3cf6921 39 }
5eaef520 40 }
41 if (len > 1)
42 {
43 *buf++ = '"';
44 count++;
45 len--;
46 }
47 if (len > 1)
48 *buf = '\0';
49 return buf;
a3cf6921 50}
5aa981ce 51
5eaef520 52void log_args(char *tag, int version, int argc, char **argv)
229feb8a 53{
5eaef520 54 char buf[BUFSIZ];
44d12d58 55 int i;
56 char *bp;
5eaef520 57
58 i = strlen(tag);
59 sprintf(buf, "%s[%d]: ", tag, version);
60 for (bp = buf; *bp; bp++)
61 ;
62
63 for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++)
64 {
65 if (i != 0)
66 {
67 *bp++ = ',';
68 *bp++ = ' ';
229feb8a 69 }
5eaef520 70 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
71 }
72 *bp = '\0';
73 com_err(whoami, 0, "%s", buf);
229feb8a 74}
5eaef520 75
76void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
a3cf6921 77{
5eaef520 78 extern client *cur_client;
79
80 if (whoami)
81 {
82 fputs(whoami, stderr);
83 if (cur_client)
84 fprintf(stderr, "[#%d]", cur_client->id);
85 fputs(": ", stderr);
86 }
85330553 87 if (code) {
5eaef520 88 fputs(error_message(code), stderr);
85330553 89 fputs(" ", stderr);
90 }
5eaef520 91 if (fmt)
7ac48069 92 vfprintf(stderr, fmt, pvar);
5eaef520 93 putc('\n', stderr);
a3cf6921 94}
2423a5db 95
96
d548a4e7 97/* mr_trim_args: passed an argument vector, it will trim any trailing
e2fe4db4 98 * spaces on the args by writing a null into the string. If an argument
99 * appears to be binary instead of ASCII, it will not be trimmed.
2423a5db 100 */
101
5eaef520 102int mr_trim_args(int argc, char **argv)
2423a5db 103{
44d12d58 104 char **arg;
105 unsigned char *p, *lastch;
5eaef520 106
107 for (arg = argv; argc--; arg++)
108 {
109 for (lastch = p = (unsigned char *) *arg; *p; p++)
110 {
111 /* If any byte in the string has the high bit set, assume
112 * that it is binary and we do not want to trim it.
113 * Setting p = lastch will cause us not to trim the string
114 * when we break out of this inner loop.
115 */
116 if (*p >= 0x80)
117 {
118 p = lastch;
119 break;
e2fe4db4 120 }
5eaef520 121 if (!isspace(*p))
122 lastch = p;
e2fe4db4 123 }
5eaef520 124 if (p != lastch)
125 {
126 if (isspace(*lastch))
127 *lastch = '\0';
128 else
129 if (*(++lastch))
130 *lastch = '\0';
2423a5db 131 }
132 }
5eaef520 133 return 0;
2423a5db 134}
71007162 135
136
137/* returns a copy of the argv and all of it's strings */
138
5eaef520 139char **mr_copy_args(char **argv, int argc)
71007162 140{
5eaef520 141 char **a;
142 int i;
143
144 a = malloc(argc * sizeof(char *));
145 if (!a)
146 return a;
147 for (i = 0; i < argc; i++)
7ac48069 148 a[i] = strdup(argv[i]);
5eaef520 149 return a;
71007162 150}
85330553 151
152
153/* malloc or die! */
154void *xmalloc(size_t bytes)
155{
156 void *buf = malloc(bytes);
157
158 if (buf)
159 return buf;
160
161 critical_alert("moirad", "Out of memory");
162 exit(1);
163}
164
165void *xrealloc(void *ptr, size_t bytes)
166{
167 void *buf = realloc(ptr, bytes);
168
169 if (buf)
170 return buf;
171
172 critical_alert("moirad", "Out of memory");
173 exit(1);
174}
This page took 0.122639 seconds and 5 git commands to generate.