more sprintf bashing
[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, size_t n, const char *searchfor,
108                  int numdirs, const char * const *dirs)
109 {
110         int a;
111         char buf[SIZ];
112         FILE *ls;
113
114         for (a=0; a<numdirs; ++a) {
115                 snprintf(buf, sizeof buf, "cd %s; exec ls",dirs[a]);
116                 ls = (FILE *) popen(buf,"r");
117                 if (ls != NULL) {
118                         while(fgets(buf,sizeof buf,ls)!=NULL) {
119                                 while (isspace(buf[strlen(buf)-1]))
120                                         buf[strlen(buf)-1] = 0;
121                                 if (!strcasecmp(buf,searchfor)) {
122                                         pclose(ls);
123                                         snprintf(targ,n,"%s/%s",dirs[a],buf);
124                                         return;
125                                         }
126                                 }
127                         pclose(ls);
128                         }
129                 }
130         strcpy(targ,"");
131         }
132
133
134 #ifndef HAVE_STRERROR
135 /*
136  * replacement strerror() for systems that don't have it
137  */
138 char *strerror(int e)
139 {
140         static char buf[32];
141
142         snprintf(buf,sizeof buf,"errno = %d",e);
143         return(buf);
144         }
145 #endif
146