* Numerous fixups needed for Windows port:
[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 <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include "citadel.h"
21 #include "server.h"
22 #include "support.h"
23
24
25 /*
26  * strproc()  -  make a string 'nice'
27  */
28 void strproc(char *string)
29 {
30         int a;
31
32         if (string == NULL) return;
33         if (strlen(string)==0) return;
34
35         /* Convert non-printable characters to blanks */
36         for (a=0; a<strlen(string); ++a) {
37                 if (string[a]<32) string[a]=32;
38                 if (string[a]>126) string[a]=32;
39                 }
40
41         /* Remove leading and trailing blanks */
42         while( (string[0]<33) && (strlen(string)>0) )
43                 strcpy(string,&string[1]);
44         while( (string[strlen(string)-1]<33) && (strlen(string)>0) )
45                 string[strlen(string)-1]=0;
46
47         /* Remove double blanks */
48         for (a=0; a<strlen(string); ++a) {
49                 if ((string[a]==32)&&(string[a+1]==32)) {
50                         strcpy(&string[a],&string[a+1]);
51                         a=0;
52                         }
53                 }
54
55         /* remove characters which would interfere with the network */
56         for (a=0; a<strlen(string); ++a) {
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                 while (string[a]=='%') strcpy(&string[a],&string[a+1]);
62                 while (string[a]=='|') strcpy(&string[a],&string[a+1]);
63                 }
64
65         }
66
67
68
69 /*
70  * get a line of text from a file
71  * ignores lines starting with #
72  */
73 int getstring(FILE *fp, char *string)
74 {
75         int a,c;
76         do {
77                 strcpy(string,"");
78                 a=0;
79                 do {
80                         c=getc(fp);
81                         if (c<0) {
82                                 string[a]=0;
83                                 return(-1);
84                                 }
85                         string[a++]=c;
86                         } while(c!=10);
87                         string[a-1]=0;
88                 } while(string[0]=='#');
89         return(strlen(string));
90         }
91
92
93 /*
94  * pattern2()  -  searches for patn within search string, returns pos 
95  */ 
96 int pattern2(char *search, char *patn)
97 {
98         int a;
99         for (a=0; a<strlen(search); ++a) {
100                 if (!strncasecmp(&search[a],patn,strlen(patn))) return(a);
101                 }
102         return(-1);
103         }
104
105
106 /*
107  * mesg_locate()  -  locate a message or help file, case insensitive
108  */
109 void mesg_locate(char *targ, size_t n, const char *searchfor,
110                  int numdirs, const char * const *dirs)
111 {
112         int a;
113         char buf[SIZ];
114         struct stat test;
115         FILE *ls;
116
117         for (a=0; a<numdirs; ++a) {
118                 snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
119                 if (!stat(buf, &test)) {
120                         snprintf(targ,n,"%s/%s", dirs[a], searchfor);
121                         return;
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         snprintf(buf,sizeof buf,"errno = %d",e);
137         return(buf);
138         }
139 #endif
140