开发者

C/C++ API to decode cron-style timings

开发者 https://www.devze.com 2023-01-14 04:47 出处:网络
Does anyone know of a library wh开发者_高级运维ich will assist in decoding cron style timings, i.e.

Does anyone know of a library wh开发者_高级运维ich will assist in decoding cron style timings, i.e.

30 7 * * 1-5 

Which is 7:30am every Monday, Tuesday, Wednesday, Thursday, Friday.

M.


There is a library for PHP, Perl but I never saw one for C++. The good thing is that Cron's source is freely available and you can reuse its code to parse entries in the cron format.

The entry data structure is defined in "cron.h" file:

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;

And there are two functions you need from "entry.c" file (too large to post code here):

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

You can compile those files into a shared library or object files and use directly in your project.

This is an example of getting cron source code in Debian (Ubuntu):

apt-get source cron

You can also download it from http://cron.sourcearchive.com/


For those that wish to achieve the same goal as @ScaryAardvark

Dependency:

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;
}

Example Crontab

@yearly /home/bcrowhurst/annual-process

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

0

精彩评论

暂无评论...
验证码 换一张
取 消