C / C ++ API for decoding chronological timings

Does anyone know a library that will help in decoding cron style timings, i.e.

30 7 * * 1-5 

It's 7:30 a.m. every Monday, Tuesday, Wednesday, Thursday, Friday.

M.

+3
source share
2 answers

For those who want to achieve the same goal as @ScaryAardvark

Dependence:

http://cron.sourcearchive.com/downloads/3.0pl1/cron_3.0pl1.orig.tar.gz

Build:

gcc -o main main.c cron-3.0pl1.orig / entry.c cron-3.0pl1.orig / env.c cron-3.0pl1.orig / misc.c -I cron-3.0pl1.orig

Source:

#include <pwd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <uuid/uuid.h>

#define MAIN_PROGRAM 1
#include "cron-3.0pl1.orig/cron.h"

void error_handler( char* message )
{
    fprintf( stderr, "Error: %s\n", message );
}

void print_entry( const entry* e )
{
    fprintf( stdout, "uid: %i\n", e->uid );
    fprintf( stdout, "gid: %i\n", e->gid );
    fprintf( stdout, "command: %s\n", e->cmd);
    //etc...
}

int main( int argc, char** argv, char** envp )
{
    const char* filename = "crontab";
    const char* username = "bcrowhurst";

    //Retreive Crontab File
    FILE *file = fopen( filename, "r" );

    if ( file == NULL )
    {
        error_handler( strerror( errno ) );

        return EXIT_FAILURE;
    }

    //Retreive Password Entry
    struct passwd *pw = getpwnam( username );

    if ( pw == NULL )
    {
        error_handler( strerror( errno ) );

        return EXIT_FAILURE;
    }

    //Read Entry
    entry *e = load_entry( file, &error_handler, pw, envp );

    if ( e == NULL )
    {
        error_handler( "No entry found!" );

        return EXIT_FAILURE;
    }

    print_entry( e );

    //Clean-up
    fclose( file );
    free_entry( e );

    return EXIT_SUCCESS;
}

Crontab example

@ yearly / home / bcrowhurst / year-process

* / 10 * * * * / home / bcrowhurst / fschk

+3
source

PHP, Perl, ++. , Cron , cron.

"cron.h":

typedef struct _entry {
        struct _entry   *next;
        uid_t           uid;
        gid_t           gid;
        char            **envp;
        char            *cmd;
        bitstr_t        bit_decl(minute, MINUTE_COUNT);
        bitstr_t        bit_decl(hour,   HOUR_COUNT);
        bitstr_t        bit_decl(dom,    DOM_COUNT);
        bitstr_t        bit_decl(month,  MONTH_COUNT);
        bitstr_t        bit_decl(dow,    DOW_COUNT);
        int             flags;
#define DOM_STAR        0x01
#define DOW_STAR        0x02
#define WHEN_REBOOT     0x04
#define MIN_STAR        0x08
#define HR_STAR         0x10
} entry;

"entry.c" : :

void free_entry (e);
entry *load_entry (file, error_func, pw, envp);

.

cron Debian (Ubuntu):

apt-get source cron

http://cron.sourcearchive.com/

+4

Source: https://habr.com/ru/post/1762662/


All Articles