e0a098fd427075772dd55daeeee99f1335e02f3c
[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 (string == NULL) return;
27         if (strlen(string)==0) return;
28
29         /* Convert non-printable characters to blanks */
30         for (a=0; a<strlen(string); ++a) {
31                 if (string[a]<32) string[a]=32;
32                 if (string[a]>126) string[a]=32;
33                 }
34
35         /* Remove leading and trailing blanks */
36         while( (string[0]<33) && (strlen(string)>0) )
37                 strcpy(string,&string[1]);
38         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
39                 string[strlen(string)-1]=0;
40
41         /* Remove double blanks */
42         for (a=0; a<strlen(string); ++a) {
43                 if ((string[a]==32)&&(string[a+1]==32)) {
44                         strcpy(&string[a],&string[a+1]);
45                         a=0;
46                         }
47                 }
48
49         /* remove characters which would interfere with the network */
50         for (a=0; a<strlen(string); ++a) {
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                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
57                 }
58
59         }
60
61
62
63 /*
64  * get a line of text from a file
65  * ignores lines starting with #
66  */
67 int getstring(FILE *fp, char *string)
68 {
69         int a,c;
70         do {
71                 strcpy(string,"");
72                 a=0;
73                 do {
74                         c=getc(fp);
75                         if (c<0) {
76                                 string[a]=0;
77                                 return(-1);
78                                 }
79                         string[a++]=c;
80                         } while(c!=10);
81                         string[a-1]=0;
82                 } while(string[0]=='#');
83         return(strlen(string));
84         }
85
86
87 /*
88  * pattern2()  -  searches for patn within search string, returns pos 
89  */ 
90 int pattern2(char *search, char *patn)
91 {
92         int a;
93         for (a=0; a<strlen(search); ++a) {
94                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
95                 }
96         return(-1);
97         }
98
99
100 /*
101  * mesg_locate()  -  locate a message or help file, case insensitive
102  */
103 void mesg_locate(char *targ, char *searchfor, int numdirs, char **dirs)
104 {
105         int a;
106         char buf[SIZ];
107         FILE *ls;
108
109         for (a=0; a<numdirs; ++a) {
110                 sprintf(buf,"cd %s; exec ls",dirs[a]);
111                 ls = (FILE *) popen(buf,"r");
112                 if (ls != NULL) {
113                         while(fgets(buf,sizeof buf,ls)!=NULL) {
114                                 while (isspace(buf[strlen(buf)-1]))
115                                         buf[strlen(buf)-1] = 0;
116                                 if (!strcasecmp(buf,searchfor)) {
117                                         pclose(ls);
118                                         sprintf(targ,"%s/%s",dirs[a],buf);
119                                         return;
120                                         }
121                                 }
122                         pclose(ls);
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         sprintf(buf,"errno = %d",e);
138         return(buf);
139         }
140 #endif
141