int gen_keys(DArray * keys, int num_keys) { int i = 0; FILE *urand = fopen("/dev/urandom", "r"); check(urand != NULL, "Failed to open /dev/urandom"); int result = -1; // default to failure condition int rc = 0; struct bStream *stream = bsopen((bNread) fread, urand); check(stream != NULL, "Failed to open /dev/urandom"); bstring key = bfromcstr(""); // FNV1a histogram for (i = 0; i < num_keys; i++) { rc = bsread(key, stream, BUFFER_LEN); check(rc >= 0, "Failed to read from /dev/urandom."); DArray_push(keys, bstrcpy(key)); } result = 0; // all good error: // fallthrough if(stream) bsclose(stream); if(urand) fclose(urand); if(key) bdestroy(key); return result; }
int gen_keys(DArray *keys, int num_keys) { int i = 0; FILE *urand = fopen("/dev/urandom", "r"); check(urand != NULL, "Failed to open /dev/urandom"); struct bStream *stream = bsopen((bNread)fread, urand); check(stream != NULL, "Failed to open /dev/urandom"); bstring key = bfromcstr(""); int rc = 0; // FNVla histogram for(i = 0; i < num_keys; i++) { rc = bsread(key, stream, BUFFER_LEN); check(rc >= 0, "Failed to read from /dev/urandom."); DArray_push(keys, bstrcpy(key)); } bsclose(stream); fclose(urand); return 0; error: return -1; }
bool do_install_all(CURL* curl) { // define used variables DIR* dir; FILE* fp; CURLcode res; bool printed; bool install_status; bool something_errored; bool if_something_was_installed; list_t installed; long httpcode = 0; struct dirent* entry; struct bStream* stream; bstring buffer, fname, sstr; bstring modpath = osutil_getmodulepath(); bstring ext = bfromcstr(".lua"); bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/list"); list_init(&installed); list_attributes_copy(&installed, list_meter_string, 1); list_attributes_comparator(&installed, list_comparator_string); // Attempt to open the modules directory. dir = opendir(modpath->data); if (dir == NULL) { printd(LEVEL_ERROR, "unable to query local repository.\n"); return 1; } // add the filename we wish to query to the modules folder path bcatcstr(modpath, "/.all_avail"); // Open the file and do the cURL transfer. printd(LEVEL_DEFAULT, "loading a list of all the modules...\n"); fp = fopen(modpath->data, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url->data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode); if (res != 0 || httpcode != 200) { bdestroy(url); bdestroy(modpath); printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode); return 1; } fclose(fp); // create a list of already installed modules while ((entry = readdir(dir)) != NULL) { fname = bfromcstr(&entry->d_name[0]); if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR) { bdestroy(fname); continue; } if (entry->d_type != DT_REG) { bdestroy(fname); continue; } sstr = bmidstr(fname, 0, blength(fname) - 4); list_append(&installed, sstr->data); bdestroy(sstr); bdestroy(fname); } printd(LEVEL_DEFAULT, "\n"); // Print the names of the modules, and install them through the do_install function fp = fopen(modpath->data, "r"); stream = bsopen(&read_data, fp); buffer = bfromcstr(""); printed = false; if_something_was_installed = false; something_errored = false; while (bsreadln(buffer, stream, '\n') != BSTR_ERR) { btrimws(buffer); sstr = bmidstr(buffer, 0, blength(buffer) - 4); // if the module is not already installed if (!list_contains(&installed, sstr->data)) { install_status = do_install(curl, bfromcstr(sstr->data)); if_something_was_installed = true; // check whether the installation was successful if (install_status != 0) { printd(LEVEL_DEFAULT, " %s failed to install.\n", sstr->data); something_errored = true; } printd(LEVEL_DEFAULT, "\n"); } printed = true; bdestroy(sstr); } if (!printed) printd(LEVEL_DEFAULT, " <no modules available>\n"); if (something_errored) printd(LEVEL_DEFAULT, "errors occured\n"); if (!if_something_was_installed) printd(LEVEL_DEFAULT, "no changes were made\n"); bsclose(stream); fclose(fp); // Clean up. curl_easy_cleanup(curl); return 0; }
bool do_search(CURL* curl, bstring name, bool all) { DIR* dir; bool printed; CURLcode res; FILE* fp; list_t installed; struct bStream* stream; long httpcode = 0; bstring buffer, fname, sstr; bstring ext = bfromcstr(".lua"); bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/search?q="); bstring modpath = osutil_getmodulepath(); struct dirent* entry; list_init(&installed); list_attributes_copy(&installed, list_meter_string, 1); list_attributes_comparator(&installed, list_comparator_string); // Attempt to open the modules directory. dir = opendir(modpath->data); if (dir == NULL) { printd(LEVEL_ERROR, "unable to query local repository.\n"); return 1; } // Append the temporary search file name. bcatcstr(modpath, "/.search"); bconcat(url, name); // Open the file and do the cURL transfer. printd(LEVEL_DEFAULT, "querying module repository...\n"); fp = fopen(modpath->data, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url->data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode); if (res != 0 || httpcode != 200) { bdestroy(url); bdestroy(name); bdestroy(modpath); printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode); return 1; } fclose(fp); // Print the local results. if (all) printd(LEVEL_DEFAULT, "all modules:\n"); else printd(LEVEL_DEFAULT, "search results for %s:\n", name->data); while ((entry = readdir(dir)) != NULL) { fname = bfromcstr(&entry->d_name[0]); if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR) { bdestroy(fname); continue; } if (binstr(fname, 0, name) == BSTR_ERR) { bdestroy(fname); continue; } if (entry->d_type != DT_REG) { bdestroy(fname); continue; } sstr = bmidstr(fname, 0, blength(fname) - 4); printd(LEVEL_DEFAULT, " %s (installed)\n", sstr->data); list_append(&installed, sstr->data); bdestroy(sstr); bdestroy(fname); } // Print the online results. fp = fopen(modpath->data, "r"); stream = bsopen(&read_data, fp); buffer = bfromcstr(""); printed = false; while (bsreadln(buffer, stream, '\n') != BSTR_ERR) { btrimws(buffer); sstr = bmidstr(buffer, 0, blength(buffer) - 4); if (!list_contains(&installed, sstr->data)) printd(LEVEL_DEFAULT, " %s\n", sstr->data); printed = true; bdestroy(sstr); } if (!printed) printd(LEVEL_DEFAULT, " <no online results>\n"); bsclose(stream); fclose(fp); // Clean up. curl_easy_cleanup(curl); return 0; }
int main (int argc, char * argv[]) { bstring b = bfromcstr ("Hello WORLDDDD"); if (!b) { fprintf (stderr, "Out of memory"); } else { puts ((char *) b->data); } bdestroy (b); b = bfromcstralloc (64, "123456789012345678901234567890123456789012345678901234567890123"); if (b) { b->data[63] = 'x'; puts ((char *) b->data); printf("dump is %s\n", dumpBstring(b)); } else { puts("bfromcstralloc failed"); } bdestroy (b); b = blk2bstr ("AWHOLEnewworld", sizeof("wholenewworld")-3); puts ((char *) b->data); char *cstr = bstr2cstr(b, '\0'); puts(cstr); free(cstr); cstr = bstr2cstr(b, '0'); puts(cstr); free(cstr); bstring copy = bstrcpy(b); printf("copy is %s\n", (char *) copy->data); bdestroy (b); b = bfromcstralloc (64, "123456789012345678901234567890123456789012345678901234567890123"); bassign(copy, b); printf("copy is %s\n", (char *) copy->data); printf("b is %s\n", (char *) b->data); bdestroy (b); b = blk2bstr ("AWHOLEnewworld", sizeof("wholenewworld")); bassign(copy, b); printf("copy is %s\n", (char *) copy->data); printf("b is %s\n", (char *) b->data); bdestroy(b); bdestroy(copy); int i = 0; b = bfromcstralloc (64, "123456789012345678901234567890123456789012345678901234567890123"); struct bstrList *blist = bsplit(b, '0'); printf("made blist. qty is %d, mlen is %d. entry[0] is %s\n", blist->qty, blist->mlen, blist->entry[0]->data); for(i=0; i<blist->qty; i++) { printf("entry[%d] is %s\n", i, blist->entry[i]->data); } bstrListDestroy(blist); blist = bsplit(b, '\0'); printf("made blist. qty is %d, mlen is %d. entry[0] is %s\n", blist->qty, blist->mlen, blist->entry[0]->data); for(i=0; i<blist->qty; i++) { printf("entry[%d] is %s\n", i, blist->entry[i]->data); } bstrListDestroy(blist); struct tagbstring split = bsStatic ("123"); blist = bsplitstr(b, &split); printf("made blist. qty is %d, mlen is %d. entry[0] is %s\n", blist->qty, blist->mlen, blist->entry[0]->data); for(i=0; i<blist->qty; i++) { printf("entry[%d] is %s\n", i, blist->entry[i]->data); } bdestroy(b); /* * *#define CHUNK 1024 [> read 1024 bytes at a time <] * char buf[CHUNK]; * FILE *file; * size_t nread; * * file = fopen("./LICENSE", "r"); * if (file) { * while ((nread = fread(buf, 1, sizeof buf, file)) > 0) * fwrite(buf, 1, nread, stdout); * if (ferror(file)) { * [> deal with error <] * } * fclose(file); * } */ FILE *lic_fd = fopen("./LICENSE", "r"); struct bStream *bslic_fd = bsopen((bNread) fread, lic_fd); bstring lic_bstr = bread( (bNread) fread, lic_fd); printf("bstring is %s", lic_bstr->data); return 0; }