From:	CRDGW2::CRDGW2::MRGATE::"SMTP::KSUVAX1.CIS.KSU.EDU::MCCALL!NEWSMGR" 26-JUL-1990 21:06:01.58
To:	MRGATE::"ARISIA::EVERHART"
CC:	
Subj:	vaxc cron part 2 of 4

Received:  by crdgw1.ge.com (5.57/GE 1.70)
	 id AA28020; Thu, 26 Jul 90 14:21:49 EDT
Resent-From: mccall!newsmgr@ksuvax1.cis.ksu.edu
Return-Path: mccall!newsmgr@ksuvax1.cis.ksu.edu
Received: from harris.cis.ksu.edu by DRYCAS.CLUB.CC.CMU.EDU; Thu, 26 Jul 90
 14:13 EST
Received: from ksuvax1.cis.ksu.edu by harris.cis.ksu.edu (5.58/SWH-2.03); id
 AA03291; Thu, 26 Jul 90 13:12:59 CDT
Received: by ksuvax1.cis.ksu.edu (5.59++/CIS1.1) id AA05256; Thu, 26 Jul 90
 13:12:50 CDT
Date: 26 Jul 90 11:09:59 GMT
From: lrb@rrivax.rri.uwo.ca
Sender: news@RIA.CCS.UWO.CA
Subject: vaxc cron part 2 of 4
To: vmsnet@DRYCAS.CLUB.CC.CMU.EDU
Message-Id: <706@ria.ccs.uwo.ca>
Resent-Date: Thu, 26 Jul 90 14:17 EST
Resent-To: VMSNET-LIST@DRYCAS.CLUB.CC.CMU.EDU
Resent-Message-Id: <5BF567A2F1AF006944@DRYCAS.CLUB.CC.CMU.EDU>
Organization: Robarts Research Institute -- London Canada Earth
X-Envelope-To: everhart%arisia.decnet@CRDGW1.GE.COM
X-Vms-To: vmsnet@DRYCAS.CLUB.CC.CMU.EDU

=======================================================================
clontab.c
-----------------------------------------------------------------------
/*
 * clontab.c
 *
 * clontab (not clontab, cLontab!)
 *
 */

#include <stdio>
#include <stdlib>
#include <types>
#include <stat>
#include <unixio>
#include <file>

#include "mycron.h"
#define unlink delete
#define link rename

extern struct passwd *getpwuid();

char (userlist[40])[100];
int numusers;
char CWD[80];

main(argc,argv)
    int argc;
    char *argv[];
{   int fd;
    char user[80];
    struct passwd *pw;

    getcwd(CWD,80);
    chdir(cronarea);

    argv++;
    argc--;

    if (argc != 2)
    {   fprintf(stderr,"usage: %s [-l|-d|filename]\n",argv[0]);
        exit(1);
    }

    strcpy(user,getenv("USER"));
    lower(user);

    if (!strcmp(argv[1],"-l"))
        list(user);
    else if (!strcmp(argv[1],"-d"))
    {   if (delete_ent(user))
            fprintf(stderr,"%s not found in entries\n",user);
    }
    else
        add(user,argv[1]);

    exit(127);
}




list(who)
    char *who;
{   FILE *cronlisting;
    char dummy[256];

    cronlisting = fopen(who,"r");

    if (!cronlisting)
    {   fprintf(stderr,"cannot open crontab file for %s\n",who);
        return;
    }

    fgets(dummy,256,cronlisting);

    while (!feof(cronlisting))
    {   fputs(dummy,stdout);
        fgets(dummy,256,cronlisting);
    }

    fclose(cronlisting);
}




delete_ent(who)
    char *who;
{   FILE *currentlist,*newlist;
    char name[40];
    int notdone = 1;

    currentlist = fopen(cronlist,"r+");
    newlist = fopen("newlisting","w");

/*printf("deleting <%s>\n",who);*/

    fgets(name,40,currentlist);
    while(!feof(currentlist))
    {   name[strlen(name)-1]=0; /* remove nl */
/*printf("comparing to <%s>\n",name);*/
        lower(name);

        if(strcmp(name,who))
            fprintf(newlist,"%s\n",name);
        else
        {   notdone = 0;
            unlink(who);
        }

        fgets(name,40,currentlist);
    }

    fclose(currentlist);
    fclose(newlist);
    unlink(cronlist);
    if (link("newlisting",cronlist))
        perror("link fail");

    chmod(cronlist,0777);
/*  unlink("newlisting");
  */
    close(open(newcronlist,O_CREAT,0770));

    return notdone;
}




add(who,what)
    char *who, *what;
{   FILE *tasks, *cstream;
    char dummy[256];

    chdir(CWD);

    tasks = fopen(what,"r");

    chdir(cronarea);

    if (!tasks)
    {   fprintf(stderr,"cannot open file %s\n",what);
        return;
    }

    delete_ent(who);
/*
fclose(tasks);
return;
*/

    cstream = fopen(cronlist,"a+");
    fprintf(cstream,"%s\n",who);
    fclose(cstream);

    cstream = fopen(who,"w");

    fgets(dummy,256,tasks);

    while (!feof(tasks))
    {   fputs(dummy,cstream);
        fgets(dummy,256,tasks);
    }

    fclose(cstream);
    fclose(tasks);

    chmod(who,0777);
    close(open(newcronlist,O_CREAT,0770));
}




lower(str)
    char *str;
{   while (*str)
    {   *str=tolower(*str);
        ++str;
    }
}
/*
 * EOF
 */
=======================================================================
mycron.c
-----------------------------------------------------------------------
/*
 * mycron.c
 *
 * mycron (get it? My-Cron. sigh)
 *
 * daemon details based on a glorious article by Dave Lennert
 * in HP Communicator, May 1988
 * daemon details mostly ignored on the VMS implementation. sigh.
 */

#include <stdio>
#include <signal>
#include <file>
#include <stat>
#include <descrip>
#include <ssdef>
#include <time>

#include "mycron"

#define unlink delete

char * uniquename();

FILE *alloutput;
long NOW;

struct cronentry cronentries[100];
int currentry = 0, lastentry = 0;
struct cronentry *currententry;

main()
{   char commandline[200];
    char mailline[200];
    char lettername[80];
    int status;

/*
 * daemon stuff
 */

#ifdef SIGTTOU
    signal(SIGTTOU,SIG_IGN);
#endif

#ifdef SIGTTIN
    signal(SIGTTIN,SIG_IGN);
#endif

#ifdef SIGTTSTP
    signal(SIGTTSTP,SIG_IGN);
#endif

    signal(SIGHUP,SIG_IGN); /* immune form pgrp leader death */

    umask(0); /* or whatever is appropriate */


/*
 * and awaaaaaaaaaaaaaaaaaaaay we go!
 */

#define InitCurrentEntry  currentry = 0;
#define EndOfList  (currentry == lastentry)
#define NextTask (cronentries + currentry++)

    chdir(cronarea);

    printf("reading first entries\n");
    readtables(cronlist);

    while(1)
    {   NOW = time((long *)0);

        if (newcron())
        {   lastentry = 0;
            printf("updating entries\n");
            readtables(cronlist);
            unlink(newcronlist);
        }

        InitCurrentEntry;
        while (!EndOfList)
        {   currententry=NextTask;

            if (!timecheck(currententry))
                continue;

            strcpy(commandline,"submit ");
            strcat(commandline,currententry->ce_command);
            strcat(commandline,"/noprint/user=");
            strcat(commandline,currententry->ce_user);

            if (currententry->ce_log)
            {   strcat(commandline,"/LOG=cron$lib:[000000]");
                strcpy(lettername,uniquename());
                strcat(commandline,lettername);
                strcpy(mailline,
                  "submit/nolog cron$lib:[000000]mailresults.com/param=(" );
                strcat(mailline,currententry->ce_user);
                strcat(mailline,"," );
                strcat(mailline,lettername );
                strcat(mailline,",\"" );
                strcat(mailline,currententry->ce_command);
                strcat(mailline,"\")" );
            }

            printf("executing <%s>\n",commandline);
            status = system(commandline);

            if (currententry->ce_log )
            {   printf("mailing results with <%s>\n",mailline);
                status = system(mailline);
            }

        }

        sleep( 60 - (time((long *)0)%60) );
    }
}



newcron()
{   struct stat statbuf;

    return(!stat(newcronlist,&statbuf));
}




readtables(listfile)
    char *listfile;
{   FILE *list, *lstream;
    char user[40], X, dummyline[256];

    lstream = fopen(listfile,"r");

    while (!feof(lstream))
    {   fscanf(lstream,"%s\r\n",user);
        printf("processing a user <%s>\n",user);
        list = fopen(user,"r");

        if (!fscanf(list,"%s ",cronentries[lastentry].ce_minute))
            printf("did not grab ce_minute\n");

        while (!feof(list))
        {   if (*(cronentries[lastentry].ce_minute) == '#') /* comments */
            {   fgets(dummyline,256,list);
                printf("ignoring\n<%s>\n",dummyline);

                if (!fscanf(list,"%s ",cronentries[lastentry].ce_minute))
                    printf("did not grab ce_minute\n");

                continue;
            }

            if (X == '\n') /* empty lines */
            {   /*X=getc(list);*/
                if (!fscanf(list,"%s ",cronentries[lastentry].ce_minute))
                    printf("did not grab ce_minute\n");
                continue;
            }

            fscanf(list,"%s %s %s %s ",cronentries[lastentry].ce_hour,
                                       cronentries[lastentry].ce_mday,
                                       cronentries[lastentry].ce_month,
                                       cronentries[lastentry].ce_wday);

            printf("minutes are %s\n",cronentries[lastentry].ce_minute);
            printf("hours are %s\n",cronentries[lastentry].ce_hour);
            printf("mdays are %s\n",cronentries[lastentry].ce_mday);
            printf("months are %s\n",cronentries[lastentry].ce_month);
            printf("wdays are %s\n",cronentries[lastentry].ce_wday);

            fgets(cronentries[lastentry].ce_command,200,list);
            printf("the command is <%s>\n",cronentries[lastentry].ce_command);
            cronentries[lastentry].
                    ce_command[strlen(cronentries[lastentry].ce_command)-1] = 0;

            raise(cronentries[lastentry].ce_command);
            printf("the command is <%s>\n",cronentries[lastentry].ce_command);
            if (logflag(cronentries[lastentry].ce_command))
                cronentries[lastentry].ce_log = 0;
            else
                cronentries[lastentry].ce_log = 1;

            strcpy(cronentries[lastentry].ce_user,user);
            lastentry++;

            if (!fscanf(list,"%s ",cronentries[lastentry].ce_minute))
                printf("did not grab ce_minute\n");
        }

        fclose(list);
    }

    fclose(lstream);
}




raise(s)
    char *s;
{    if (!s)
         return;

    while (*s)
    {   *s = toupper(*s);
        ++s;
    }

    return;
}




logflag(s)
    char *s;
{   while(s,s=strchr(s,'L'))
    {   s++;

        if (s[0] != 'O')
            continue;

        if (s[1] != 'G')
            continue;

        return 1;
    }

    return 0;
}




char *
uniquename()
{   static char name[100];
    static int number=1;

    sprintf(name,"cronmail.%04d",number);
    ++number;

    return name;
}




timecheck(ct)
    struct cronentry *ct;
{   struct tm *tmstruct;

    tmstruct = localtime(&NOW);
    tmstruct->tm_mon++; 

    if ((*(ct->ce_minute) != '*') && !timematch(tmstruct->tm_min,ct->ce_minute))
        return 0;

    if ((*(ct->ce_hour) != '*') && !timematch(tmstruct->tm_hour,ct->ce_hour))
        return 0;

    if ((*(ct->ce_month) != '*') && !timematch(tmstruct->tm_mon,ct->ce_month))
        return 0;

    if ((*(ct->ce_mday) != '*') && !timematch(tmstruct->tm_mday,ct->ce_mday))
        return 0;

    if ((*(ct->ce_wday) != '*') && !timematch(tmstruct->tm_wday,ct->ce_wday))
        return 0;

    return 1;
}



timematch(value,string)
    char *string;
    int value;
{   char *ptr;
    int extract;

    ptr = string;
    while (*ptr)
    {   extract = strtol(ptr,&ptr,10);
        if (*ptr)
            ++ptr;

        if (extract == value)
            return 1;

    }

    return 0;
}

/*
 * EOF
 */
=======================================================================
mycron.h
-----------------------------------------------------------------------
/*
 * mycron.h
 */
struct cronentry
{   char ce_minute[80];  /* when */
    char ce_hour[40];
    char ce_mday[40];
    char ce_month[40];
    char ce_wday[40];
    char ce_command[80]; /* what */
    char ce_user[10];    /* who */
    int  ce_log;         /* where */
};

#define cronarea "cron$lib:[000000]"
#define newcronlist "newentries"
#define cronlist "entries"
=======================================================================
_________________________________
Lance R. Bailey, Systems Manager | Robarts Research Institute
email: lrb@rri.uwo.ca            | Clinical Trials Resources Group
  vox: 519-663-3787 ext. 4108    | P.O. Box 5015, 100 Perth Dr.
  fax: 519-663-3789              | London, Canada N6A 5K8
