Back to index

citadel  8.12
support.c
Go to the documentation of this file.
00001 /*
00002  * Server-side utility functions
00003  */
00004 
00005 #include "sysdep.h"
00006 #include <stdlib.h>
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <unistd.h>
00010 #include <ctype.h>
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <libcitadel.h>
00014 #include "citadel.h"
00015 #include "server.h"
00016 #include "support.h"
00017 
00018 /*
00019  * strproc()  -  make a string 'nice'
00020  */
00021 void strproc(char *string)
00022 {
00023        int a, b;
00024 
00025        if (string == NULL) return;
00026        if (IsEmptyStr(string)) return;
00027 
00028        /* Convert non-printable characters to blanks */
00029        for (a=0; !IsEmptyStr(&string[a]); ++a) {
00030               if (string[a]<32) string[a]=32;
00031               if (string[a]>126) string[a]=32;
00032        }
00033 
00034        /* a is now the length of our string. */
00035        /* Remove leading and trailing blanks */
00036        while( (string[a-1]<33) && (!IsEmptyStr(string)) )
00037               string[--a]=0;
00038        b = 0;
00039        while( (string[b]<33) && (!IsEmptyStr(&string[b])) )
00040               b++;
00041        if (b > 0)
00042               memmove(string,&string[b], a - b + 1);
00043 
00044        /* Remove double blanks */
00045        for (a=0; !IsEmptyStr(&string[a]); ++a) {
00046               if ((string[a]==32)&&(string[a+1]==32)) {
00047                      strcpy(&string[a],&string[a+1]);
00048                      a=0;
00049               }
00050        }
00051 
00052        /* remove characters which would interfere with the network */
00053        for (a=0; !IsEmptyStr(&string[a]); ++a) {
00054               while (string[a]=='!') strcpy(&string[a],&string[a+1]);
00055               while (string[a]=='@') strcpy(&string[a],&string[a+1]);
00056               while (string[a]=='_') strcpy(&string[a],&string[a+1]);
00057               while (string[a]==',') strcpy(&string[a],&string[a+1]);
00058               while (string[a]=='%') strcpy(&string[a],&string[a+1]);
00059               while (string[a]=='|') strcpy(&string[a],&string[a+1]);
00060        }
00061 
00062 }
00063 
00064 
00065 
00066 /*
00067  * get a line of text from a file
00068  * ignores lines starting with #
00069  */
00070 int getstring(FILE *fp, char *string)
00071 {
00072        int a,c;
00073        do {
00074               strcpy(string,"");
00075               a=0;
00076               do {
00077                      c=getc(fp);
00078                      if (c<0) {
00079                             string[a]=0;
00080                             return(-1);
00081                             }
00082                      string[a++]=c;
00083                      } while(c!=10);
00084                      string[a-1]=0;
00085               } while(string[0]=='#');
00086        return(strlen(string));
00087 }
00088 
00089 
00090 
00091 
00092 /*
00093  * mesg_locate()  -  locate a message or help file, case insensitive
00094  */
00095 void mesg_locate(char *targ, size_t n, const char *searchfor,
00096                int numdirs, const char * const *dirs)
00097 {
00098        int a;
00099        char buf[SIZ];
00100        struct stat test;
00101 
00102        for (a=0; a<numdirs; ++a) {
00103               snprintf(buf, sizeof buf, "%s/%s", dirs[a], searchfor);
00104               if (!stat(buf, &test)) {
00105                      snprintf(targ, n, "%s/%s", dirs[a], searchfor);
00106                      return;
00107               }
00108        }
00109        strcpy(targ,"");
00110 }
00111 
00112 
00113 #ifndef HAVE_STRERROR
00114 /*
00115  * replacement strerror() for systems that don't have it
00116  */
00117 char *strerror(int e)
00118 {
00119        static char buf[32];
00120 
00121        snprintf(buf,sizeof buf,"errno = %d",e);
00122        return(buf);
00123        }
00124 #endif
00125