static void modinfo(const char *path, const char *version, struct modinfo_env *env) { static const char *const shortcuts[] = { "filename", "description", "author", "license", "vermagic", "parm", }; size_t len; int j, length; char *ptr, *the_module; const char *field = env->field; int tags = env->tags; if (tags & 1) { /* filename */ display(path, shortcuts[0], 1 != tags); } len = MAXINT(ssize_t); the_module = xmalloc_open_zipped_read_close(path, &len); if (!the_module) { if (path[0] == '/') return; /* Newer depmod puts relative paths in modules.dep */ path = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); the_module = xmalloc_open_zipped_read_close(path, &len); free((char*)path); if (!the_module) return; } if (field) tags |= OPT_F; for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) { const char *pattern = field; if ((1<<j) & OPT_TAGS) pattern = shortcuts[j]; if (!((1<<j) & tags)) continue; length = strlen(pattern); ptr = the_module; while (1) { ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module)); if (ptr == NULL) /* no occurance left, done */ break; if (strncmp(ptr, pattern, length) == 0 && ptr[length] == '=') { ptr += length + 1; ptr += display(ptr, pattern, (1<<j) != tags); } ++ptr; } } free(the_module); }
static int one_monitor_on_top_of_another(const struct x_monitor *one, const struct x_monitor *another) { int x = MAXINT(one->x, another->x); int x2 = MININT(one->x + one->width, another->x + another->width); if (x2 > x && one->y < another->y) return 1; return 0; }
void FAST_FUNC sleep_for_duration(duration_t duration) { struct timespec ts; ts.tv_sec = MAXINT(typeof(ts.tv_sec)); ts.tv_nsec = 0; if (duration >= 0 && duration < ts.tv_sec) { ts.tv_sec = duration; ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; } do { errno = 0; nanosleep(&ts, &ts); } while (errno == EINTR); }
static int load_module(const char *fname, const char *options) { #if 1 int r; size_t len = MAXINT(ssize_t); char *module_image; dbg1_error_msg("load_module('%s','%s')", fname, options); module_image = xmalloc_open_zipped_read_close(fname, &len); r = (!module_image || init_module(module_image, len, options ? options : "") != 0); free(module_image); dbg1_error_msg("load_module:%d", r); return r; /* 0 = success */ #else /* For testing */ dbg1_error_msg("load_module('%s','%s')", fname, options); return 1; #endif }
int FAST_FUNC bb_init_module(const char *filename, const char *options) { size_t len = MAXINT(ssize_t); char *image; int rc = ENOENT; #if ENABLE_FEATURE_2_4_MODULES if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) return bb_init_module_24(filename, options); #endif /* Use the 2.6 way */ image = xmalloc_open_zipped_read_close(filename, &len); if (image) { if (init_module(image, len, options) != 0) rc = errno; else rc = 0; free(image); } return rc; }
int readprofile_main(int argc UNUSED_PARAM, char **argv) { FILE *map; const char *mapFile, *proFile; unsigned long indx = 1; size_t len; uint64_t add0 = 0; unsigned int step; unsigned int *buf, total, fn_len; unsigned long long fn_add, next_add; /* current and next address */ char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ char mapline[S_LEN]; char mode[8]; int maplineno = 1; int header_printed; int multiplier = 0; unsigned opt; enum { OPT_M = (1 << 0), OPT_m = (1 << 1), OPT_p = (1 << 2), OPT_n = (1 << 3), OPT_a = (1 << 4), OPT_b = (1 << 5), OPT_s = (1 << 6), OPT_i = (1 << 7), OPT_r = (1 << 8), OPT_v = (1 << 9), }; #define optMult (opt & OPT_M) #define optNative (opt & OPT_n) #define optAll (opt & OPT_a) #define optBins (opt & OPT_b) #define optSub (opt & OPT_s) #define optInfo (opt & OPT_i) #define optReset (opt & OPT_r) #define optVerbose (opt & OPT_v) #define next (current^1) proFile = defaultpro; mapFile = defaultmap; opt_complementary = "M+"; /* -M N */ opt = getopt32(argv, "M:m:p:nabsirv", &multiplier, &mapFile, &proFile); if (opt & (OPT_M|OPT_r)) { /* mult or reset, or both */ int fd, to_write; /* * When writing the multiplier, if the length of the write is * not sizeof(int), the multiplier is not changed */ to_write = sizeof(int); if (!optMult) to_write = 1; /* sth different from sizeof(int) */ fd = xopen(defaultpro, O_WRONLY); xwrite(fd, &multiplier, to_write); close(fd); return EXIT_SUCCESS; } /* * Use an fd for the profiling buffer, to skip stdio overhead */ len = MAXINT(ssize_t); buf = xmalloc_xopen_read_close(proFile, &len); if (!optNative) { int entries = len / sizeof(*buf); int big = 0, small = 0, i; unsigned *p; for (p = buf+1; p < buf+entries; p++) { if (*p & ~0U << (sizeof(*buf)*4)) big++; if (*p & ((1 << (sizeof(*buf)*4))-1)) small++; } if (big > small) { bb_error_msg("assuming revergsed byte order, " "use -n to force native byte order"); for (p = buf; p < buf+entries; p++) for (i = 0; i < sizeof(*buf)/2; i++) { unsigned char *b = (unsigned char *) p; unsigned char tmp; tmp = b[i]; b[i] = b[sizeof(*buf)-i-1]; b[sizeof(*buf)-i-1] = tmp; } } } step = buf[0]; if (optInfo) { printf("Sampling_step: %u\n", step); return EXIT_SUCCESS; } total = 0; map = xfopen_for_read(mapFile); while (fgets(mapline, S_LEN, map)) { if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) bb_error_msg_and_die("%s(%i): wrong map line", mapFile, maplineno); if (!strcmp(fn_name, "_stext")) /* only elf works like this */ { add0 = fn_add; break; } maplineno++; } if (!add0) bb_error_msg_and_die("can't find \"_stext\" in %s", mapFile); /* * Main loop. */ while (fgets(mapline, S_LEN, map)) { unsigned int this = 0; if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) bb_error_msg_and_die("%s(%i): wrong map line", mapFile, maplineno); header_printed = 0; /* ignore any LEADING (before a '[tT]' symbol is found) Absolute symbols */ if ((*mode == 'A' || *mode == '?') && total == 0) continue; if (*mode != 'T' && *mode != 't' && *mode != 'W' && *mode != 'w' ) { break; /* only text is profiled */ } if (indx >= len / sizeof(*buf)) bb_error_msg_and_die("profile address out of range. " "Wrong map file?"); while (indx < (next_add-add0)/step) { if (optBins && (buf[indx] || optAll)) { if (!header_printed) { printf("%s:\n", fn_name); header_printed = 1; } printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]); } this += buf[indx++]; } total += this; if (optBins) { if (optVerbose || this > 0) printf(" total\t\t\t\t%u\n", this); } else if ((this || optAll) && (fn_len = next_add-fn_add) != 0 ) { if (optVerbose) printf("%016llx %-40s %6u %8.4f\n", fn_add, fn_name, this, this/(double)fn_len); else printf("%6u %-40s %8.4f\n", this, fn_name, this/(double)fn_len); if (optSub) { unsigned long long scan; for (scan = (fn_add-add0)/step + 1; scan < (next_add-add0)/step; scan++) { unsigned long long addr; addr = (scan - 1)*step + add0; printf("\t%#llx\t%s+%#llx\t%u\n", addr, fn_name, addr - fn_add, buf[scan]); } } } fn_add = next_add; strcpy(fn_name, next_name); maplineno++; } /* clock ticks, out of kernel text - probably modules */ printf("%6u %s\n", buf[len/sizeof(*buf)-1], "*unknown*"); /* trailer */ if (optVerbose) printf("%016x %-40s %6u %8.4f\n", 0, "total", total, total/(double)(fn_add-add0)); else printf("%6u %-40s %8.4f\n", total, "total", total/(double)(fn_add-add0)); fclose(map); free(buf); return EXIT_SUCCESS; }
int sleep_main(int argc UNUSED_PARAM, char **argv) { #if ENABLE_FEATURE_FLOAT_SLEEP double duration; struct timespec ts; #else unsigned duration; #endif ++argv; if (!*argv) bb_show_usage(); #if ENABLE_FEATURE_FLOAT_SLEEP duration = 0; do { char *arg = *argv; if (strchr(arg, '.')) { double d; char *pp; int len = strspn(arg, "0123456789."); char sv = arg[len]; arg[len] = '\0'; errno = 0; d = strtod(arg, &pp); if (errno || *pp) bb_show_usage(); arg[len] = sv; len--; sv = arg[len]; arg[len] = '1'; duration += d * xatoul_sfx(&arg[len], sfx); arg[len] = sv; } else duration += xatoul_sfx(arg, sfx); } while (*++argv); ts.tv_sec = MAXINT(typeof(ts.tv_sec)); ts.tv_nsec = 0; if (duration >= 0 && duration < ts.tv_sec) { ts.tv_sec = duration; ts.tv_nsec = (duration - ts.tv_sec) * 1000000000; } do { errno = 0; nanosleep(&ts, &ts); } while (errno == EINTR); #elif ENABLE_FEATURE_FANCY_SLEEP duration = 0; do { duration += xatou_range_sfx(*argv, 0, UINT_MAX - duration, sfx); } while (*++argv); sleep(duration); #else /* simple */ duration = xatou(*argv); sleep(duration); // Off. If it's really needed, provide example why //if (sleep(duration)) { // bb_perror_nomsg_and_die(); //} #endif return EXIT_SUCCESS; }
int du_main(int argc, char **argv) { long total; int slink_depth_save; int print_final_total; char *smax_print_depth; unsigned opt; #if ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K if (getenv("POSIXLY_CORRECT")) { /* TODO - a new libbb function? */ #if ENABLE_FEATURE_HUMAN_READABLE disp_hr = 512; #else disp_k = 0; #endif } #endif /* Note: SUSv3 specifies that -a and -s options cannot be used together * in strictly conforming applications. However, it also says that some * du implementations may produce output when -a and -s are used together. * gnu du exits with an error code in this case. We choose to simply * ignore -a. This is consistent with -s being equivalent to -d 0. */ #if ENABLE_FEATURE_HUMAN_READABLE opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s"; opt = getopt32(argc, argv, "aHkLsx" "d:" "lc" "hm", &smax_print_depth); if (opt & (1 << 9)) { /* -h opt */ disp_hr = 0; } if (opt & (1 << 10)) { /* -m opt */ disp_hr = 1024*1024; } if (opt & (1 << 2)) { /* -k opt */ disp_hr = 1024; } #else opt_complementary = "H-L:L-H:s-d:d-s"; opt = getopt32(argc, argv, "aHkLsx" "d:" "lc", &smax_print_depth); #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K if (opt & (1 << 2)) { /* -k opt */ disp_k = 1; } #endif #endif if (opt & (1 << 0)) { /* -a opt */ print_files = INT_MAX; } if (opt & (1 << 1)) { /* -H opt */ slink_depth = 1; } if (opt & (1 << 3)) { /* -L opt */ slink_depth = INT_MAX; } if (opt & (1 << 4)) { /* -s opt */ max_print_depth = 0; } one_file_system = opt & (1 << 5); /* -x opt */ if (opt & (1 << 6)) { /* -d opt */ max_print_depth = xatoi_u(smax_print_depth); } if (opt & (1 << 7)) { /* -l opt */ count_hardlinks = MAXINT(nlink_t); } print_final_total = opt & (1 << 8); /* -c opt */ /* go through remaining args (if any) */ argv += optind; if (optind >= argc) { *--argv = (char*)"."; if (slink_depth == 1) { slink_depth = 0; } } slink_depth_save = slink_depth; total = 0; do { total += du(*argv); slink_depth = slink_depth_save; } while (*++argv); if (ENABLE_FEATURE_CLEAN_UP) reset_ino_dev_hashtable(); if (print_final_total) { print(total, "total"); } fflush_stdout_and_exit(status); }
item_t* dichosolve(int size1, item_t* first, int size2, item_t* second, knint cons, int* rezsize) { if( size1 == -1 && size2 == -1 ){ *rezsize = -1; return NULL; } if ( size1 == -1 ) { *rezsize = size2; return copyhash (second); } if ( size2 == -1 ) { *rezsize = size1; return copyhash (first); } //item_t *its = createitems0 (cons), *fp, *sp; item_t *its = NULL, *fp, *sp, *tmp; knint w, p; int cnt = 0; // real count of @its knint *wp, *pp; // put all elements of first table for ( fp = first ; fp != NULL && *(fp->w) <= cons ; fp = fp->hh.next ) { cnt++; sp = copyitem (fp); HASH_ADD_KEYPTR ( hh, its, sp->w, KNINT_SIZE, sp ); } // put new elements of second table or replace elements having less value for( fp = second ; fp!= NULL && *(fp->w) <= cons ; fp = fp->hh.next ) { HASH_FIND (hh, its, fp->w, KNINT_SIZE, tmp); if( tmp == NULL ){ cnt++; tmp = copyitem (fp); HASH_ADD_KEYPTR (hh, its, tmp->w, KNINT_SIZE, tmp); } else { *(tmp->p) = MAXINT ( *(fp->p) , *(tmp->p) ); } } // pairwise addition for( fp = first ; fp != NULL ; fp = fp->hh.next ){ for( sp = second ; sp != NULL && (p = *(fp->p) + *(sp->p),w = *(fp->w) + *(sp->w), w<=cons) ; sp = sp->hh.next ){ HASH_FIND (hh, its, &w, KNINT_SIZE, tmp); if( tmp == NULL ){ cnt++; tmp = copyitem ( sp ); *(tmp->w) = w; *(tmp->p) = p; HASH_ADD_KEYPTR (hh, its, tmp->w, KNINT_SIZE, tmp); } else { *(tmp->p) = MAXINT( *(tmp->p),p ); } } } // save non-zero elements in solid array if( cnt == 0 ){ *rezsize = -1; return NULL; } *rezsize = cnt; return its; }
static void modinfo(const char *path, const char *version, const struct modinfo_env *env) { static const char *const shortcuts[] = { "filename", "license", "author", "description", "version", "alias", "srcversion", "depends", "uts_release", "vermagic", "parm", "firmware", }; size_t len; int j; char *ptr, *fullpath, *the_module; const char *field = env->field; int tags = env->tags; if (tags & 1) { /* filename */ display(path, shortcuts[0], 1 != tags); } len = MAXINT(ssize_t); the_module = xmalloc_open_zipped_read_close(path, &len); if (!the_module) { if (path[0] == '/') return; /* Newer depmod puts relative paths in modules.dep */ fullpath = xasprintf("%s/%s/%s", CONFIG_DEFAULT_MODULES_DIR, version, path); the_module = xmalloc_open_zipped_read_close(fullpath, &len); #ifdef DONT_USE_UTS_REL_FOLDER if (!the_module) { free((char*)fullpath); fullpath = xasprintf("%s/%s", CONFIG_DEFAULT_MODULES_DIR, path); the_module = xmalloc_open_zipped_read_close(fullpath, &len); } #endif free((char*)fullpath); if (!the_module) { // outputs system error msg bb_perror_msg(""); return; } } if (field) tags |= OPT_F; for (j = 1; (1<<j) & (OPT_TAGS + OPT_F); j++) { const char *pattern; if (!((1<<j) & tags)) continue; pattern = field; if ((1<<j) & OPT_TAGS) pattern = shortcuts[j]; ptr = the_module; while (1) { char *after_pattern; ptr = memchr(ptr, *pattern, len - (ptr - (char*)the_module)); if (ptr == NULL) /* no occurance left, done */ break; after_pattern = is_prefixed_with(ptr, pattern); if (after_pattern && *after_pattern == '=') { /* field prefixes are 0x80 or 0x00 */ if ((ptr[-1] & 0x7F) == 0x00) { ptr = after_pattern + 1; display(ptr, pattern, (1<<j) != tags); ptr += strlen(ptr); } } ++ptr; } } free(the_module); }