Last active
March 13, 2018 09:38
Revisions
-
nonchip revised this gist
Mar 13, 2018 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 FILE *handle = fopen(path,"r"); #endif if(!handle) return NULL; char line[1025],vpkg[1025],resto[1025]; -
nonchip revised this gist
Mar 13, 2018 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -56,4 +56,12 @@ zhash_t *bvirtpkg_read(const char * path) { return 0; } #endif /* tested with: $ gcc -DTEST_BUILD bvirtpkg_read.c -lczmq $ ./a.out ~/void-packages ntp-daemon "ntp-daemon" => "chrony" */ -
nonchip created this gist
Mar 13, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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