e9d04a078f842617b4ead0ac21ef18c8f32ee08f
[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 #ifdef HAVE_PTHREAD_H
9 #include <pthread.h>
10 #endif
11 #include "citadel.h"
12 #include "server.h"
13 #include "support.h"
14
15
16 /*
17  * strproc()  -  make a string 'nice'
18  */
19 void strproc(char *string)
20 {
21         int a;
22
23         if (strlen(string)==0) return;
24
25         /* Convert non-printable characters to blanks */
26         for (a=0; a<strlen(string); ++a) {
27                 if (string[a]<32) string[a]=32;
28                 if (string[a]>126) string[a]=32;
29                 }
30
31         /* Remove leading and trailing blanks */
32         while( (string[0]<33) && (strlen(string)>0) )
33                 strcpy(string,&string[1]);
34         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
35                 string[strlen(string)-1]=0;
36
37         /* Remove double blanks */
38         for (a=0; a<strlen(string); ++a) {
39                 if ((string[a]==32)&&(string[a+1]==32)) {
40                         strcpy(&string[a],&string[a+1]);
41                         a=0;
42                         }
43                 }
44
45         /* remove characters which would interfere with the network */
46         for (a=0; a<strlen(string); ++a) {
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                 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                 }
54
55         }
56
57
58
59 /*
60  * get a line of text from a file
61  * ignores lines starting with #
62  */
63 int getstring(FILE *fp, char *string)
64 {
65         int a,c;
66         do {
67                 strcpy(string,"");
68                 a=0;
69                 do {
70                         c=getc(fp);
71                         if (c<0) {
72                                 string[a]=0;
73                                 return(-1);
74                                 }
75                         string[a++]=c;
76                         } while(c!=10);
77                         string[a-1]=0;
78                 } while(string[0]=='#');
79         return(strlen(string));
80         }
81
82
83 /*
84  * pattern2()  -  searches for patn within search string, returns pos 
85  */ 
86 int pattern2(char *search, char *patn)
87 {
88         int a;
89         for (a=0; a<strlen(search); ++a) {
90                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
91                 }
92         return(-1);
93         }
94
95
96 /*
97  * mesg_locate()  -  locate a message or help file, case insensitive
98  */
99 void mesg_locate(char *targ, char *searchfor, int numdirs, char **dirs)
100 {
101         int a;
102         char buf[256];
103         FILE *ls;
104
105         for (a=0; a<numdirs; ++a) {
106                 sprintf(buf,"cd %s; exec ls",dirs[a]);
107                 ls = (FILE *) popen(buf,"r");
108                 if (ls != NULL) {
109                         while(fgets(buf,255,ls)!=NULL) {
110                                 while (isspace(buf[strlen(buf)-1]))
111                                         buf[strlen(buf)-1] = 0;
112                                 if (!strcasecmp(buf,searchfor)) {
113                                         pclose(ls);
114                                         sprintf(targ,"%s/%s",dirs[a],buf);
115                                         return;
116                                         }
117                                 }
118                         pclose(ls);
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         sprintf(buf,"errno = %d",e);
134         return(buf);
135         }
136 #endif
137