/* Module for low level subroutine */
/* */
/********************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
#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;
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* 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);
+ }
+ }
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* 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
+}
#define SUBROU
#include <linux/types.h>
+#include <stdbool.h>
#include <stdarg.h>
+
#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