From: Jean-Marc Pigeon (Delson) Date: Mon, 8 Jul 2024 19:46:07 +0000 (-0400) Subject: Adding getvers to subrou.c X-Git-Tag: tag-0.1~1 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=dc240b9930b5bab15cf948209c463e5651ce60d7;p=jmp%2Fmailleur Adding getvers to subrou.c --- diff --git a/app/mailled.c b/app/mailled.c index 91af41f..7e0f573 100644 --- a/app/mailled.c +++ b/app/mailled.c @@ -4,8 +4,10 @@ /* Main SMTP Daemon */ /* */ /********************************************************/ -#include #include +#include + +#include "subrou.h" /* @@ -38,10 +40,13 @@ proceed=true; while (proceed==true) { switch (phase) { case 0 : //initialising process + (void) rou_modesubrou(true); break; case 1 : //checking parameters + (void) fprintf(stderr,"JMPDBG Vers='%s'\n",rou_getversion()); break; default : //end of task + (void) rou_modesubrou(false); proceed=false; break; } diff --git a/lib/subrou.c b/lib/subrou.c index c4b862b..f337ed6 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -4,10 +4,189 @@ /* Module for low level subroutine */ /* */ /********************************************************/ +#include +#include +#include +#include #include "subrou.h" //version definition #define VERSION "0.1" -#define RELEASE "4" +#define RELEASE "5" + +//Public variables +PUBLIC int debug=0; //debug level + +PUBLIC _Bool foreground=false; //process is always started in background mode +PUBLIC char *rootdir=(char *)0;//working directory (debugging) +PUBLIC char *appname; //application "official" name + +//static variables +static _Bool modopen; //boolean module open/close + +//Time offset (for debug purpose only) +static time_t off64_time=(time_t)0; +static time_t off_date=(time_t)0; +/* +^L +*/ +/********************************************************/ +/* */ +/* Returne the version number and application */ +/* name. */ +/* */ +/********************************************************/ +const char *rou_getversion() + +{ +return VERSION"."RELEASE; +} +/* + +*/ +/********************************************************/ +/* */ +/* Subroutine to log event on syslog */ +/* */ +/********************************************************/ +void rou_valert(const int dlevel,const char *fmt,va_list ap) + +#define DEBMAX 80 + +{ +if (debug>=dlevel) + { + char *ptr; + char strloc[10000]; + + (void) memset(strloc,'\000',sizeof(strloc)); + (void) vsnprintf(strloc,sizeof(strloc)-1,fmt,ap); + ptr=strloc; + if (foreground==true) + (void) fprintf(stderr,"%s\n",strloc); + else { + while (strlen(ptr)>DEBMAX) { + char locline[DEBMAX+10]; + + (void) strncpy(locline,ptr,DEBMAX); + locline[DEBMAX]='\000'; + (void) syslog(LOG_INFO,"%s",locline); + ptr +=DEBMAX; + } + (void) syslog(LOG_INFO,"%s",ptr); + } + } +} +/* + +*/ +/********************************************************/ +/* */ +/* Subroutine to log event on syslog */ +/* */ +/********************************************************/ +void rou_alert(const int dlevel,const char *fmt,...) + +{ +va_list args; + +va_start(args,fmt); +(void) rou_valert(dlevel,fmt,args); +va_end(args); +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to "open" usager to module. */ +/* homework purpose */ +/* return zero if everything right */ +/* */ +/********************************************************/ +int rou_opensubrou() + +{ +int status; + +status=0; +if (modopen==false) { + debug=0; + foreground=false; + off64_time=(time_t)0; + off_date=(time_t)0; + if (appname!=(char *)0) + (void) free(appname); + appname=strdup(APPNAME); + if (rootdir!=(char *)0) { + (void) free(rootdir); + status=-1; //Rootdir should be found as NULL + } + rootdir=strdup(""); + (void) openlog(APPNAME,LOG_PID,LOG_DAEMON); + (void) rou_alert(0,"Starting: %s-%s",APPNAME,rou_getversion()); + modopen=true; + } +return status; +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to "open/close" module and do */ +/* homework purpose */ +/* return zero if everything right */ +/* */ +/********************************************************/ +int rou_modesubrou(_Bool mode) + +{ +#define OPEP "subrou.c:rou_modesubrou" + +int status; + +status=0; +if (mode!=modopen) { + switch ((int)mode) { + case true : + debug=0; + foreground=false; + off64_time=(time_t)0; + off_date=(time_t)0; + if (appname!=(char *)0) + (void) free(appname); + appname=strdup(APPNAME); + if (rootdir!=(char *)0) { + (void) free(rootdir); + status=-1; //Rootdir should be found as NULL + } + rootdir=strdup(""); + (void) openlog(APPNAME,LOG_PID,LOG_DAEMON); + (void) rou_alert(0,"Starting: %s-%s",APPNAME,rou_getversion()); + break; + case false : + (void) rou_alert(0,"Exiting: %s-%s",APPNAME,rou_getversion()); + (void) closelog(); + if (appname!=(char *)0) { + (void) free(appname); + appname=(char *)0; + } + if (rootdir!=(char *)0) { + (void) free(rootdir); + rootdir=(char *)0; + } + break; + default : + (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):", + OPEP,(int)mode); + status=-1; + break; + } + modopen=mode; + } +return status; +#undef OPEP +} diff --git a/lib/subrou.h b/lib/subrou.h index 8ab1215..5444a60 100644 --- a/lib/subrou.h +++ b/lib/subrou.h @@ -8,8 +8,34 @@ #define SUBROU #include +#include #include + #define APPNAME "mailled" //application name +//--- Variables defined within subrou.c --------- +extern int debug; //application debug mode +extern _Bool foreground; //process is in foreground mode + +extern char *rootdir; //application root directory +extern char *appname; //application "official" name + +//--- Routines implemented within subrou.c --------- + +//to display message on console (verbose mode) or +//via syslog (LOG_INFO) using variable argument list macros +void rou_valert(const int dlevel,const char *fmt,va_list ap); + +//to display message on console (verbose mode) or +//via syslog (LOG_DAEMON) +extern void rou_alert(const int dlevel,const char *fmt,...); + +//return program version +extern const char *rou_getversion(); + +//homework to be done before starting/stoping module. +extern int rou_modesubrou(_Bool mode); + +#define PUBLIC //to specify public variable #endif