be48c6fedc71f6005d4ef2404a18f8c46848b1d2
[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
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 (strlen(string)==0) return;
30
31         /* Convert non-printable characters to blanks */
32         for (a=0; a<strlen(string); ++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) && (strlen(string)>0) )
39                 strcpy(string,&string[1]);
40         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
41                 string[strlen(string)-1]=0;
42
43         /* Remove double blanks */
44         for (a=0; a<strlen(string); ++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; a<strlen(string); ++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;
95         for (a=0; a<strlen(search); ++a) {
96                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
97                 }
98         return(-1);
99         }
100
101
102 /*
103  * mesg_locate()  -  locate a message or help file, case insensitive
104  */
105 void mesg_locate(char *targ, size_t n, const char *searchfor,
106                  int numdirs, const char * const *dirs)
107 {
108         int a;
109         char buf[SIZ];
110         struct stat test;
111
112         for (a=0; a<numdirs; ++a) {
113                 snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
114                 if (!stat(buf, &test)) {
115                         snprintf(targ, n, "%s/%s", dirs[a], searchfor);
116                         return;
117                 }
118         }
119         strcpy(targ,"");
120 }
121
122
123 #ifndef HAVE_STRERROR
124 /*
125  * replacement strerror() for systems that don't have it
126  */
127 char *strerror(int e)
128 {
129         static char buf[32];
130
131         snprintf(buf,sizeof buf,"errno = %d",e);
132         return(buf);
133         }
134 #endif
135