* strlen holy war: loops. in loops it's very evil. the easy ones go away now.
[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;
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         /* Remove leading and trailing blanks */
38         while( (string[0]<33) && (!IsEmptyStr(string)) )
39                 strcpy(string,&string[1]);
40         while( (string[strlen(string)-1]<33) && (!IsEmptyStr(string)) )
41                 string[strlen(string)-1]=0;
42
43         /* Remove double blanks */
44         for (a=0; !IsEmptyStr(&string[a]); ++a) {
45                 if ((string[a]==32)&&(string[a+1]==32)) {
46                         strcpy(&string[a],&string[a+1]);
47                         a=0;
48                 }
49         }
50
51         /* remove characters which would interfere with the network */
52         for (a=0; !IsEmptyStr(&string[a]); ++a) {
53                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
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         }
60
61 }
62
63
64
65 /*
66  * get a line of text from a file
67  * ignores lines starting with #
68  */
69 int getstring(FILE *fp, char *string)
70 {
71         int a,c;
72         do {
73                 strcpy(string,"");
74                 a=0;
75                 do {
76                         c=getc(fp);
77                         if (c<0) {
78                                 string[a]=0;
79                                 return(-1);
80                                 }
81                         string[a++]=c;
82                         } while(c!=10);
83                         string[a-1]=0;
84                 } while(string[0]=='#');
85         return(strlen(string));
86 }
87
88
89 /*
90  * pattern2()  -  searches for patn within search string, returns pos 
91  */ 
92 int pattern2(char *search, char *patn)
93 {
94         int a, len;
95         
96         len = strlen(patn);
97         for (a=0; !IsEmptyStr(&search[a]); ++a) {
98                 if (!strncasecmp(&search[a],patn, len)) return(a);
99                 }
100         return(-1);
101         }
102
103
104 /*
105  * mesg_locate()  -  locate a message or help file, case insensitive
106  */
107 void mesg_locate(char *targ, size_t n, const char *searchfor,
108                  int numdirs, const char * const *dirs)
109 {
110         int a;
111         char buf[SIZ];
112         struct stat test;
113
114         for (a=0; a<numdirs; ++a) {
115                 snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
116                 if (!stat(buf, &test)) {
117                         snprintf(targ, n, "%s/%s", dirs[a], searchfor);
118                         return;
119                 }
120         }
121         strcpy(targ,"");
122 }
123
124
125 #ifndef HAVE_STRERROR
126 /*
127  * replacement strerror() for systems that don't have it
128  */
129 char *strerror(int e)
130 {
131         static char buf[32];
132
133         snprintf(buf,sizeof buf,"errno = %d",e);
134         return(buf);
135         }
136 #endif
137