b650c7ee8c2cfcbb5bbac65b999e08dee7fbcd88
[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 <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <libcitadel.h>
17 #include "citadel.h"
18 #include "server.h"
19 #include "support.h"
20
21 /*
22  * strproc()  -  make a string 'nice'
23  */
24 void strproc(char *string)
25 {
26         int a, b;
27
28         if (string == NULL) return;
29         if (IsEmptyStr(string)) return;
30
31         /* Convert non-printable characters to blanks */
32         for (a=0; !IsEmptyStr(&string[a]); ++a) {
33                 if (string[a]<32) string[a]=32;
34                 if (string[a]>126) string[a]=32;
35         }
36
37         /* a is now the length of our string. */
38         /* Remove leading and trailing blanks */
39         while( (string[a-1]<33) && (!IsEmptyStr(string)) )
40                 string[--a]=0;
41         b = 0;
42         while( (string[b]<33) && (!IsEmptyStr(&string[b])) )
43                 b++;
44         if (b > 0)
45                 memmove(string,&string[b], a - b + 1);
46
47         /* Remove double blanks */
48         for (a=0; !IsEmptyStr(&string[a]); ++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; !IsEmptyStr(&string[a]); ++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
95 /*
96  * mesg_locate()  -  locate a message or help file, case insensitive
97  */
98 void mesg_locate(char *targ, size_t n, const char *searchfor,
99                  int numdirs, const char * const *dirs)
100 {
101         int a;
102         char buf[SIZ];
103         struct stat test;
104
105         for (a=0; a<numdirs; ++a) {
106                 snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
107                 if (!stat(buf, &test)) {
108                         snprintf(targ, n, "%s/%s", dirs[a], searchfor);
109                         return;
110                 }
111         }
112         strcpy(targ,"");
113 }
114
115
116 #ifndef HAVE_STRERROR
117 /*
118  * replacement strerror() for systems that don't have it
119  */
120 char *strerror(int e)
121 {
122         static char buf[32];
123
124         snprintf(buf,sizeof buf,"errno = %d",e);
125         return(buf);
126         }
127 #endif
128