]> andersk Git - moira.git/blame - server/mr_util.c
Oracle and Solaris/POSIX changes
[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--;
3b2aae4c 32 for(; (count < 258) && (len > 1) && (c = *cp);
5aa981ce 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--; }
5aa981ce 42 if (len > 1) *buf = '\0';
229feb8a 43 return buf;
a3cf6921 44}
5aa981ce 45
2423a5db 46log_args(tag, version, argc, argv)
ac65c1d1 47 char *tag;
2423a5db 48 int version;
229feb8a 49 int argc;
50 char **argv;
51{
52 char buf[BUFSIZ];
53 register int i;
2423a5db 54 register char *bp;
229feb8a 55
ac65c1d1 56 i = strlen(tag);
2423a5db 57 sprintf(buf, "%s[%d]: ", tag, version);
58 for (bp = buf; *bp; bp++);
ac65c1d1 59
3b2aae4c 60 for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++) {
229feb8a 61 if (i != 0) {
62 *bp++ = ',';
63 *bp++ = ' ';
64 }
3b2aae4c 65 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
229feb8a 66 }
67 *bp = '\0';
5cc7d26c 68 com_err(whoami, 0, "%s", buf);
229feb8a 69}
70
d548a4e7 71void mr_com_err(whoami, code, fmt, pvar)
a3cf6921 72 char *whoami;
73 int code;
b4182127 74 char *fmt;
75 caddr_t pvar;
a3cf6921 76{
77 extern char *error_message();
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
125 *(++lastch) = 0;
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 0.287637 seconds and 5 git commands to generate.