Skip to content

Instantly share code, notes, and snippets.

@nonchip
Last active March 13, 2018 09:38

Revisions

  1. nonchip revised this gist Mar 13, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions bvirtpkg_read.c
    Original file line number Diff line number Diff line change
    @@ -13,11 +13,12 @@ zhash_t *bvirtpkg_read(const char * path) {
    if(!filepath) return NULL;
    strcpy(filepath,path);
    strcat(filepath,innerpath);
    FILE *handle = fopen(filepath,"r");
    free(filepath);
    #else
    char *filepath = path;
    FILE *handle = fopen(path,"r");
    #endif

    FILE *handle = fopen(filepath,"r");
    if(!handle) return NULL;

    char line[1025],vpkg[1025],resto[1025];
  2. nonchip revised this gist Mar 13, 2018. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion bvirtpkg_read.c
    Original file line number Diff line number Diff line change
    @@ -56,4 +56,12 @@ zhash_t *bvirtpkg_read(const char * path) {
    return 0;
    }

    #endif
    #endif

    /* tested with:
    $ gcc -DTEST_BUILD bvirtpkg_read.c -lczmq
    $ ./a.out ~/void-packages ntp-daemon
    "ntp-daemon" => "chrony"
    */
  3. nonchip created this gist Mar 13, 2018.
    59 changes: 59 additions & 0 deletions bvirtpkg_read.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #define _POSIX_C_SOURCE 200809L
    #include <czmq.h>
    #include <stdio.h>

    // you'll need to `free` the living shit out of everything in this hash when destroying it!
    zhash_t *bvirtpkg_read(const char * path) {
    zhash_t *retVal = zhash_new();

    // if argument is already a complete path to the file, set to `0` or remove `if` block.
    #if 1
    const char* innerpath = "/etc/defaults.virtual";
    char *filepath = malloc(strlen(path)+strlen(innerpath)+1);
    if(!filepath) return NULL;
    strcpy(filepath,path);
    strcat(filepath,innerpath);
    #else
    char *filepath = path;
    #endif

    FILE *handle = fopen(filepath,"r");
    if(!handle) return NULL;

    char line[1025],vpkg[1025],resto[1025];
    while (fgets(line, sizeof line, handle)) {
    if (*line == '#') continue; /* ignore comment line */
    if (sscanf(line, "%1024s %1024s", vpkg, resto) == 2) {
    char *virtualpkg = malloc(strlen(vpkg)+1);
    char *resolvesto = malloc(strlen(resto)+1);
    strcpy(virtualpkg,vpkg);
    strcpy(resolvesto,resto);
    zhash_insert(retVal, virtualpkg, resolvesto);
    }
    }

    return retVal;
    }

    #ifdef TEST_BUILD

    int main(int argc, char *argv[]) {
    if(argc<2) {
    printf("usage: %s /path/to/void-packages name-of-virtual-package\n",argv[0]);
    return 1;
    }
    zhash_t *hash = bvirtpkg_read(argv[1]);
    if(!hash) {
    printf("bvirtpkg_read(\"%s\") failed.\n",argv[1]);
    return 2;
    }
    char *found = zhash_lookup(hash,argv[2]);
    if(!found) {
    printf("zhash_lookup(\"%s\") failed.\n",argv[2]);
    return 3;
    }
    printf("\"%s\" => \"%s\"\n",argv[2],found);
    return 0;
    }

    #endif