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