]> andersk Git - moira.git/blame - server/mr_util.c
If creating a list which has the same name as a username, prompt and ask
[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"
a3cf6921 17
18#include <ctype.h>
c93d352e 19#include <sys/types.h>
229feb8a 20
21extern char *whoami;
22
229feb8a 23char *
5aa981ce 24requote(buf, cp, len)
229feb8a 25 char *buf;
a3cf6921 26 register char *cp;
27{
5aa981ce 28 register int count = 0;
caeb1b25 29 register unsigned char c;
5aa981ce 30 if (len <= 2) return buf;
31 *buf++ = '"'; count++; len--;
32 for(; (count < 40) && (len > 1) && (c = *cp);
33 cp++, --len, ++count) {
229feb8a 34 if (c == '\\' || c == '"') *buf++ = '\\';
35 if (isprint(c)) *buf++ = c;
36 else {
37 sprintf(buf, "\\%03o", c);
38 buf = index(buf, '\0');
39 }
a3cf6921 40 }
5aa981ce 41 if (len > 1) { *buf++ = '"'; count++; len--; }
69f63d24 42 if (len > 3 && count >= 40) {
5aa981ce 43 *buf++ = '.'; count++; len--;
44 *buf++ = '.'; count++; len--;
45 *buf++ = '.'; count++; len--;
46 }
47 if (len > 1) *buf = '\0';
229feb8a 48 return buf;
a3cf6921 49}
5aa981ce 50
2423a5db 51log_args(tag, version, argc, argv)
ac65c1d1 52 char *tag;
2423a5db 53 int version;
229feb8a 54 int argc;
55 char **argv;
56{
57 char buf[BUFSIZ];
58 register int i;
2423a5db 59 register char *bp;
229feb8a 60
ac65c1d1 61 i = strlen(tag);
2423a5db 62 sprintf(buf, "%s[%d]: ", tag, version);
63 for (bp = buf; *bp; bp++);
ac65c1d1 64
5aa981ce 65 for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) {
229feb8a 66 if (i != 0) {
67 *bp++ = ',';
68 *bp++ = ' ';
69 }
5aa981ce 70 bp = requote(bp, argv[i], (buf - bp) + 1024);
229feb8a 71 }
72 *bp = '\0';
5cc7d26c 73 com_err(whoami, 0, "%s", buf);
229feb8a 74}
75
d548a4e7 76void mr_com_err(whoami, code, fmt, pvar)
a3cf6921 77 char *whoami;
78 int code;
b4182127 79 char *fmt;
80 caddr_t pvar;
a3cf6921 81{
82 extern char *error_message();
83 extern client *cur_client;
84
a3cf6921 85 if (whoami) {
b4182127 86 fputs(whoami, stderr);
87 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
88 fputs(": ", stderr);
a3cf6921 89 }
90 if (code) {
b4182127 91 fputs(error_message(code), stderr);
92 }
93 if (fmt) {
94 _doprnt(fmt, pvar, stderr);
a3cf6921 95 }
b4182127 96 putc('\n', stderr);
a3cf6921 97}
2423a5db 98
99
d548a4e7 100/* mr_trim_args: passed an argument vector, it will trim any trailing
e2fe4db4 101 * spaces on the args by writing a null into the string. If an argument
102 * appears to be binary instead of ASCII, it will not be trimmed.
2423a5db 103 */
104
d548a4e7 105int mr_trim_args(argc, argv)
2423a5db 106int argc;
107char **argv;
108{
109 register char **arg;
30517356 110 register unsigned char *p, *lastch;
2423a5db 111
112 for (arg = argv; argc--; arg++) {
245f8a92 113 for (lastch = p = (unsigned char *) *arg; *p; p++) {
e2fe4db4 114 /* If any byte in the string has the high bit set, assume
115 * that it is binary and we do not want to trim it.
116 * Setting p = lastch will cause us not to trim the string
117 * when we break out of this inner loop.
118 */
119 if (*p >= 0x80) {
120 p = lastch;
121 break;
122 }
123 if (!isspace(*p))
124 lastch = p;
125 }
2423a5db 126 if (p != lastch) {
127 if (isspace(*lastch))
128 *lastch = 0;
129 else
130 *(++lastch) = 0;
131 }
132 }
133 return(0);
134}
71007162 135
136
137/* returns a copy of the argv and all of it's strings */
138
139char **mr_copy_args(argv, argc)
140char **argv;
141int argc;
142{
143 char **a;
144 int i;
145
146 a = (char **) malloc(argc * sizeof(char *));
147 if (a == 0)
148 return(a);
149 for (i = 0; i < argc; i++)
150 a[i] = strsave(argv[i]);
151 return(a);
152}
This page took 0.21702 seconds and 5 git commands to generate.