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