2010-09-01 10 views

Répondre

3

Pour ceux qui souhaitent atteindre le même objectif que

@ScaryAardvark

dépendance:

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

Construire:

gcc -o principale-Cron 3.0pl1.orig main.c/entry.c-Cron 3.0pl1.orig/ENV.C -Cron 3.0pl1.orig/Divers .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; 
} 

Exa mple Crontab

@yearly/home/bcrowhurst/processus annuel de-

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

4

Il y a une bibliothèque pour PHP, Perl, mais je ne ai jamais vu un C++. La bonne chose est que la source de Cron est disponible gratuitement et vous pouvez réutiliser son code pour analyser les entrées au format cron.

La structure de données d'entrée est défini dans le fichier « 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; 

Et il y a deux fonctions dont vous avez besoin de fichier « entry.c » (trop grand pour code postal ici):

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

Vous pouvez compiler ces fichiers dans une bibliothèque partagée ou dans des fichiers objet et les utiliser directement dans votre projet.

Voici un exemple d'obtenir le code source cron dans Debian (Ubuntu):

apt-get source cron 

Vous pouvez également le télécharger à partir http://cron.sourcearchive.com/