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