Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[citadel.git] / citadel / support.c
1 /*
2  * Server-side utility functions
3  */
4
5 #include "sysdep.h"
6 #include <stdlib.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <libcitadel.h>
14 #include "citadel.h"
15 #include "server.h"
16 #include "support.h"
17
18 /*
19  * strproc()  -  make a string 'nice'
20  */
21 void strproc(char *string)
22 {
23         int a, b;
24
25         if (string == NULL) return;
26         if (IsEmptyStr(string)) return;
27
28         /* Convert non-printable characters to blanks */
29         for (a=0; !IsEmptyStr(&string[a]); ++a) {
30                 if (string[a]<32) string[a]=32;
31                 if (string[a]>126) string[a]=32;
32         }
33
34         /* a is now the length of our string. */
35         /* Remove leading and trailing blanks */
36         while( (string[a-1]<33) && (!IsEmptyStr(string)) )
37                 string[--a]=0;
38         b = 0;
39         while( (string[b]<33) && (!IsEmptyStr(&string[b])) )
40                 b++;
41         if (b > 0)
42                 memmove(string,&string[b], a - b + 1);
43
44         /* Remove double blanks */
45         for (a=0; !IsEmptyStr(&string[a]); ++a) {
46                 if ((string[a]==32)&&(string[a+1]==32)) {
47                         strcpy(&string[a],&string[a+1]);
48                         a=0;
49                 }
50         }
51
52         /* remove characters which would interfere with the network */
53         for (a=0; !IsEmptyStr(&string[a]); ++a) {
54                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
55                 while (string[a]=='@') strcpy(&string[a],&string[a+1]);
56                 while (string[a]=='_') strcpy(&string[a],&string[a+1]);
57                 while (string[a]==',') strcpy(&string[a],&string[a+1]);
58                 while (string[a]=='%') strcpy(&string[a],&string[a+1]);
59                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
60         }
61
62 }
63
64
65
66 /*
67  * get a line of text from a file
68  * ignores lines starting with #
69  */
70 int getstring(FILE *fp, char *string)
71 {
72         int a,c;
73         do {
74                 strcpy(string,"");
75                 a=0;
76                 do {
77                         c=getc(fp);
78                         if (c<0) {
79                                 string[a]=0;
80                                 return(-1);
81                                 }
82                         string[a++]=c;
83                         } while(c!=10);
84                         string[a-1]=0;
85                 } while(string[0]=='#');
86         return(strlen(string));
87 }
88
89
90
91
92 /*
93  * mesg_locate()  -  locate a message or help file, case insensitive
94  */
95 void mesg_locate(char *targ, size_t n, const char *searchfor,
96                  int numdirs, const char * const *dirs)
97 {
98         int a;
99         char buf[SIZ];
100         struct stat test;
101
102         for (a=0; a<numdirs; ++a) {
103                 snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
104                 if (!stat(buf, &test)) {
105                         snprintf(targ, n, "%s/%s", dirs[a], searchfor);
106                         return;
107                 }
108         }
109         strcpy(targ,"");
110 }
111
112
113 #ifndef HAVE_STRERROR
114 /*
115  * replacement strerror() for systems that don't have it
116  */
117 char *strerror(int e)
118 {
119         static char buf[32];
120
121         snprintf(buf,sizeof buf,"errno = %d",e);
122         return(buf);
123         }
124 #endif
125