* The size constant "256" which shows up everywhere as a buffer size has 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 <unistd.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include "citadel.h"
15 #include "server.h"
16 #include "support.h"
17
18
19 /*
20  * strproc()  -  make a string 'nice'
21  */
22 void strproc(char *string)
23 {
24         int a;
25
26         if (strlen(string)==0) return;
27
28         /* Convert non-printable characters to blanks */
29         for (a=0; a<strlen(string); ++a) {
30                 if (string[a]<32) string[a]=32;
31                 if (string[a]>126) string[a]=32;
32                 }
33
34         /* Remove leading and trailing blanks */
35         while( (string[0]<33) && (strlen(string)>0) )
36                 strcpy(string,&string[1]);
37         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
38                 string[strlen(string)-1]=0;
39
40         /* Remove double blanks */
41         for (a=0; a<strlen(string); ++a) {
42                 if ((string[a]==32)&&(string[a+1]==32)) {
43                         strcpy(&string[a],&string[a+1]);
44                         a=0;
45                         }
46                 }
47
48         /* remove characters which would interfere with the network */
49         for (a=0; a<strlen(string); ++a) {
50                 while (string[a]=='!') strcpy(&string[a],&string[a+1]);
51                 while (string[a]=='@') strcpy(&string[a],&string[a+1]);
52                 while (string[a]=='_') strcpy(&string[a],&string[a+1]);
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                 }
57
58         }
59
60
61
62 /*
63  * get a line of text from a file
64  * ignores lines starting with #
65  */
66 int getstring(FILE *fp, char *string)
67 {
68         int a,c;
69         do {
70                 strcpy(string,"");
71                 a=0;
72                 do {
73                         c=getc(fp);
74                         if (c<0) {
75                                 string[a]=0;
76                                 return(-1);
77                                 }
78                         string[a++]=c;
79                         } while(c!=10);
80                         string[a-1]=0;
81                 } while(string[0]=='#');
82         return(strlen(string));
83         }
84
85
86 /*
87  * pattern2()  -  searches for patn within search string, returns pos 
88  */ 
89 int pattern2(char *search, char *patn)
90 {
91         int a;
92         for (a=0; a<strlen(search); ++a) {
93                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
94                 }
95         return(-1);
96         }
97
98
99 /*
100  * mesg_locate()  -  locate a message or help file, case insensitive
101  */
102 void mesg_locate(char *targ, char *searchfor, int numdirs, char **dirs)
103 {
104         int a;
105         char buf[SIZ];
106         FILE *ls;
107
108         for (a=0; a<numdirs; ++a) {
109                 sprintf(buf,"cd %s; exec ls",dirs[a]);
110                 ls = (FILE *) popen(buf,"r");
111                 if (ls != NULL) {
112                         while(fgets(buf,255,ls)!=NULL) {
113                                 while (isspace(buf[strlen(buf)-1]))
114                                         buf[strlen(buf)-1] = 0;
115                                 if (!strcasecmp(buf,searchfor)) {
116                                         pclose(ls);
117                                         sprintf(targ,"%s/%s",dirs[a],buf);
118                                         return;
119                                         }
120                                 }
121                         pclose(ls);
122                         }
123                 }
124         strcpy(targ,"");
125         }
126
127
128 #ifndef HAVE_STRERROR
129 /*
130  * replacement strerror() for systems that don't have it
131  */
132 char *strerror(int e)
133 {
134         static char buf[32];
135
136         sprintf(buf,"errno = %d",e);
137         return(buf);
138         }
139 #endif
140