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