uint32_t listing(PWIN32_FIND_DATA data, PUSER_DIR_INFO ctx, HANDLE hf, char* buf, uint32_t bufsz, uint32_t depth, uint32_t tab) { WIN32_FIND_DATA innerData = {0}; char innerBuf[MAX_PATH + 1] = {0}; char tabBuf[MAX_PATH] = {0}; HANDLE hInner = INVALID_HANDLE_VALUE; char* tmp = NULL; uint32_t i = 0; if(NULL == data || 0 == ctx->depth) return DIRLIST_NOMOREITEMS; for(; i < MAX_PATH && i < tab; ++i) tabBuf[i] = '\t'; do { if((data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (data->cFileName[0] != '.')) { if(NULL != (tmp = strstr(buf, "*.*"))) { *tmp = '\0'; } _snprintf(innerBuf, MAX_PATH, "%s%s\\*.*", buf, data->cFileName); if (INVALID_HANDLE_VALUE == (hInner = FindFirstFileA(innerBuf, &innerData))) { continue; } fprintf(ctx->hFile, "%s%s", tabBuf, data->cFileName); listing(data, ctx, hInner, innerBuf, MAX_PATH, depth - 1, tab + 1); FindClose(hInner); } else { fprintf(ctx->hFile, "%sFile | size: %s | %d bytes.\n", tabBuf, data->cFileName, data->nFileSizeLow); } } while(FindNextFileA(hf, data)); return DIRLIST_NOMOREITEMS; }
uint32_t __stdcall do_work(PUSER_DIR_INFO ctx) { WIN32_FIND_DATA data = {0}; uint32_t status = 0; char buf[MAX_PATH+1] = {0}; char* dirlist = NULL; HANDLE hFile = NULL; if(NULL == ctx || NULL == ctx->startPath || NULL == ctx->outfile) return DIRLIST_BADPARAM; if(NULL == (ctx->hFile = fopen(ctx->outfile, "w"))) { printf("[x] Failed to open logfile!\n"); return GetLastError(); } _snprintf(buf, MAX_PATH, "%s\\*.*", ctx->startPath); if(INVALID_HANDLE_VALUE == (hFile = FindFirstFileA(buf, &data))) { printf("[x] First file not found!\n"); return GetLastError(); } ctx->depth += 1; if(DIRLIST_NOMOREITEMS != (status = listing(&data, ctx, hFile, buf, MAX_PATH, ctx->depth, 0))) { FindClose(hFile); return status; } FindClose(hFile); return DIRLIST_SUCCESS; }
int compile(const char *program, const char *filename, unsigned options) { file::buffer_t file = file::read_file(filename); if (!file.data || !file.size) { fprintf(stderr, "Can't find file or file is empty.\n"); usage(program); } Reader reader; Object *e = reader.read((const char *)file.data); ScriptCompiler sc(Object::cast_pair(e), true); Code *c = sc.compile_code(); int r = 0; if (!c) { printf("\nCompile failed!\n"); r = -1; } else { FileOutStream out(format::format("%s.sbin", filename).c_str()); c->write(out); out.close(); if ((options & LISTING) != 0) { std::string s = Disassembler::disassemble(c); FileOutStream listing(format::format("%s.lst", filename).c_str()); listing.write(s.data(), s.size()); listing.close(); } } delete [] file.data; return r; }
int main(int argc, char **argv) { int i; buf_str ctx; memset(&ctx, 0, sizeof(buf_str)); ctx.p.period = PERIOD_SIZE; ctx.p.buf_max = 0xFFF;//TODO variable buf ctx.p.mutex = malloc(sizeof(MUTEX_T) * (ctx.p.buf_max + 1)); for (i = 0; i <= ctx.p.buf_max; i++) { MUTEX_INIT(ctx.p.mutex + i); } ctx.p.update = malloc(sizeof(unsigned char) * (ctx.p.buf_max + 1)); memset(ctx.p.update, 0, sizeof(unsigned char) * (ctx.p.buf_max + 1)); ctx.p.buf = malloc((ctx.p.buf_max + 1) * ctx.p.period * CHANNELS * BITS / 8); //FIXME memory allocation failed ctx.list = listing(argv + 1); if(!ctx.list) force_exit_signal = 1; else list_shuffle(ctx.list); THREAD_T play, buffer; #ifndef _WIN32 pthread_attr_t rt; struct sched_param p = {}; p.sched_priority = sched_get_priority_max(SCHED_RR); pthread_attr_init(&rt); pthread_attr_setinheritsched(&rt, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&rt, SCHED_RR); pthread_attr_setschedparam(&rt, &p); #endif CREATE_THREAD(buffer, buffer_thread, &ctx); CREATE_THREAD_RT(play, play_thread, &ctx.p, &rt); console(); JOIN_THREAD(buffer); JOIN_THREAD(play); list_full_remove(ctx.list); for (i = 0; i <= ctx.p.buf_max; i++) { MUTEX_DESTROY(ctx.p.mutex + i); } free(ctx.p.mutex); free(ctx.p.update); free(ctx.p.buf); return 0; }
int create_list_of_file(char *dir, t_list *opt) { struct dirent *eof; DIR *ret; t_ls *ls; if ((ret = opendir(dir)) == NULL) return (-1); if ((eof = malloc(sizeof(struct dirent *))) == NULL) return (-1); if ((ls = malloc(sizeof(t_ls))) == NULL) return (-1); ls->first = NULL; ls->last = NULL; while ((eof = readdir(ret)) != NULL) { if (ls->first == NULL) add_in_void_list(ls, eof->d_name); else add_in_end_list(ls, eof->d_name); } listing(dir, ls, opt); return (0); }
CompositeFoodListing loadCompositeFoodNames (const CompositeFoodLoadRequest& req) { CompositeFoodListing listing(req.includedatesinname()); if (req.omit()) return listing; assert(req.nameandidonly()); if (req.omitexpired()) { listing.setError("Omission of expired composite foods is not yet supported."); } for (int i = 0; i < req.requestedids_size(); ++i) { listing.addObject(CompositeFood::getCompositeFood(req.requestedids(i))); } bool accessViolation = false; for (int i = 0; i < req.requesteduserids_size(); ++i) { if (req.requesteduserids(i) == User::getLoggedInUserId()) { listing.addObjects(CompositeFood::getFoodsForUser(req.requesteduserids(i))); } else { accessViolation = true; } } if (accessViolation) { listing.setError("Some requested food names were omitted because they " "belong to another user."); } return listing; }
int main(void) { int p_pound = 0; // weight of potato int s_pound = 0; // weight of sugarbeet int c_pound = 0; // weight of carrot int weight = 0; // total weight of all goods float total = 0.0; // total cost excluding freight float freight = 0.0; // freight float discount; char ch; // the option printf("\n====================ABC Mail Order Grocery====================\n"); printf("Order List:\n\n"); printf(" a) potato: $%.2f / pound\tb) sugarbeet: $%.2f / pound\n c) carrot: $%.2f / pound\n", POTATO, SUGARBEET, CARROT); printf("\n==============================================================\n"); printf("Select your cargo (q to finish):\n"); // the while loop below controls the ordering flow while ((ch = getchar()) != 'q') { if (ch == '\n') continue; switch (ch) { case 'a': printf("Enter amount(pound): "); p_pound = amount();break; case 'b': printf("Enter amount(pound): "); s_pound = amount();break; case 'c': printf("Enter amount(pound): "); c_pound = amount();break; case 'q': break; default: printf("Invalid input. Please enter again:\n"); break; // used 'continue' and some bad thing happend } while(getchar() != '\n') continue; printf("Keep shopping(choose a) b) c) or q to quit)?\n"); } weight = (p_pound + c_pound + s_pound); if (weight = 0) freight = 0; else if (weight <= P1) freight = F1; else if (weight < P2) freight = F2; else freight = F3 + F4*(weight - P2); total = p_pound*POTATO + s_pound*SUGARBEET +c_pound*CARROT; if (total >= 100) discount = total * DR; else discount = 0; listing(p_pound,s_pound,c_pound,freight,total,discount); return 0; }