]> andersk Git - moira.git/blob - server/mr_util.c
9b9e67d40feb170809aee5efd54cc1001604635d
[moira.git] / server / mr_util.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_mr_util_c = "$Header$";
13 #endif lint
14
15 #include <mit-copyright.h>
16 #include "mr_server.h"
17 #include <com_err.h>
18 #include <ctype.h>
19 #include <sys/types.h>
20 #include <string.h>
21
22 extern char *whoami;
23
24 char *requote(char *buf, register char *cp, int len)
25 {
26   register int count = 0;
27   register unsigned char c;
28   if (len <= 2)
29     return buf;
30   *buf++ = '"';
31   count++;
32   len--;
33   for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count)
34     {
35       if (c == '\\' || c == '"')
36         *buf++ = '\\';
37       if (isprint(c))
38         *buf++ = c;
39       else
40         {
41           sprintf(buf, "\\%03o", c);
42           buf = strchr(buf, '\0');
43         }
44     }
45   if (len > 1)
46     {
47       *buf++ = '"';
48       count++;
49       len--;
50     }
51   if (len > 1)
52     *buf = '\0';
53   return buf;
54 }
55
56 void log_args(char *tag, int version, int argc, char **argv)
57 {
58   char buf[BUFSIZ];
59   register int i;
60   register char *bp;
61
62   i = strlen(tag);
63   sprintf(buf, "%s[%d]: ", tag, version);
64   for (bp = buf; *bp; bp++)
65     ;
66
67   for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++)
68     {
69       if (i != 0)
70         {
71           *bp++ = ',';
72           *bp++ = ' ';
73         }
74       bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
75     }
76   *bp = '\0';
77   com_err(whoami, 0, "%s", buf);
78 }
79
80 void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
81 {
82   extern client *cur_client;
83
84   if (whoami)
85     {
86       fputs(whoami, stderr);
87       if (cur_client)
88         fprintf(stderr, "[#%d]", cur_client->id);
89       fputs(": ", stderr);
90     }
91   if (code)
92     fputs(error_message(code), stderr);
93   if (fmt)
94     _doprnt(fmt, pvar, stderr);
95   putc('\n', stderr);
96 }
97
98
99 /* mr_trim_args: passed an argument vector, it will trim any trailing
100  * spaces on the args by writing a null into the string.  If an argument
101  * appears to be binary instead of ASCII, it will not be trimmed.
102  */
103
104 int mr_trim_args(int argc, char **argv)
105 {
106   register char **arg;
107   register unsigned char *p, *lastch;
108
109   for (arg = argv; argc--; arg++)
110     {
111       for (lastch = p = (unsigned char *) *arg; *p; p++)
112         {
113           /* If any byte in the string has the high bit set, assume
114            * that it is binary and we do not want to trim it.
115            * Setting p = lastch will cause us not to trim the string
116            * when we break out of this inner loop.
117            */
118           if (*p >= 0x80)
119             {
120               p = lastch;
121               break;
122             }
123           if (!isspace(*p))
124             lastch = p;
125         }
126       if (p != lastch)
127         {
128           if (isspace(*lastch))
129             *lastch = '\0';
130           else
131             if (*(++lastch))
132               *lastch = '\0';
133         }
134     }
135   return 0;
136 }
137
138
139 /* returns a copy of the argv and all of it's strings */
140
141 char **mr_copy_args(char **argv, int argc)
142 {
143   char **a;
144   int i;
145
146   a = malloc(argc * sizeof(char *));
147   if (!a)
148     return a;
149   for (i = 0; i < argc; i++)
150     a[i] = strsave(argv[i]);
151   return a;
152 }
This page took 0.035379 seconds and 3 git commands to generate.