static void conv_c(PR *pr, unsigned char *p) { const char *str = conv_str; char buf[10]; do { if (*p == *str) { ++str; goto strpr; } str += 4; } while (*str); if (isprint_asciionly(*p)) { *pr->cchar = 'c'; printf(pr->fmt, *p); } else { sprintf(buf, "%03o", (int) *p); str = buf; strpr: *pr->cchar = 's'; printf(pr->fmt, str); } }
static void display(priv_dumper_t* dumper) { FS *fs; FU *fu; PR *pr; int cnt; unsigned char *bp, *savebp; off_t saveaddress; unsigned char savech = '\0'; while ((bp = get(dumper)) != NULL) { fs = dumper->pub.fshead; savebp = bp; saveaddress = dumper->address; for (; fs; fs = fs->nextfs, bp = savebp, dumper->address = saveaddress) { for (fu = fs->nextfu; fu; fu = fu->nextfu) { if (fu->flags & F_IGNORE) { break; } for (cnt = fu->reps; cnt; --cnt) { for (pr = fu->nextpr; pr; dumper->address += pr->bcnt, bp += pr->bcnt, pr = pr->nextpr) { if (dumper->eaddress && dumper->address >= dumper->eaddress && !(pr->flags & (F_TEXT | F_BPAD)) ) { bpad(pr); } if (cnt == 1 && pr->nospace) { savech = *pr->nospace; *pr->nospace = '\0'; } /* PRINT; */ switch (pr->flags) { case F_ADDRESS: printf(pr->fmt, (unsigned) dumper->address); break; case F_BPAD: printf(pr->fmt, ""); break; case F_C: conv_c(pr, bp); break; case F_CHAR: printf(pr->fmt, *bp); break; case F_DBL: { double dval; float fval; switch (pr->bcnt) { case 4: memcpy(&fval, bp, sizeof(fval)); printf(pr->fmt, fval); break; case 8: memcpy(&dval, bp, sizeof(dval)); printf(pr->fmt, dval); break; } break; } case F_INT: { int ival; short sval; switch (pr->bcnt) { case 1: printf(pr->fmt, (int) *bp); break; case 2: memcpy(&sval, bp, sizeof(sval)); printf(pr->fmt, (int) sval); break; case 4: memcpy(&ival, bp, sizeof(ival)); printf(pr->fmt, ival); break; } break; } case F_P: printf(pr->fmt, isprint_asciionly(*bp) ? *bp : '.'); break; case F_STR: printf(pr->fmt, (char *) bp); break; case F_TEXT: printf(pr->fmt); break; case F_U: conv_u(pr, bp); break; case F_UINT: { unsigned ival; unsigned short sval; switch (pr->bcnt) { case 1: printf(pr->fmt, (unsigned) *bp); break; case 2: memcpy(&sval, bp, sizeof(sval)); printf(pr->fmt, (unsigned) sval); break; case 4: memcpy(&ival, bp, sizeof(ival)); printf(pr->fmt, ival); break; } break; } } if (cnt == 1 && pr->nospace) { *pr->nospace = savech; } } } } } } if (dumper->endfu) { /* * if eaddress not set, error or file size was multiple * of blocksize, and no partial block ever found. */ if (!dumper->eaddress) { if (!dumper->address) { return; } dumper->eaddress = dumper->address; } for (pr = dumper->endfu->nextpr; pr; pr = pr->nextpr) { switch (pr->flags) { case F_ADDRESS: printf(pr->fmt, (unsigned) dumper->eaddress); break; case F_TEXT: printf(pr->fmt); break; } } } }
static char *get_key(char *str, struct sort_key *key, int flags) { int start = 0, end = 0, len, j; unsigned i; /* Special case whole string, so we don't have to make a copy */ if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3] && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb)) ) { return str; } /* Find start of key on first pass, end on second pass */ len = strlen(str); for (j = 0; j < 2; j++) { if (!key->range[2*j]) end = len; /* Loop through fields */ else { end = 0; for (i = 1; i < key->range[2*j] + j; i++) { if (key_separator) { /* Skip body of key and separator */ while (str[end]) { if (str[end++] == key_separator) break; } } else { /* Skip leading blanks */ while (isspace(str[end])) end++; /* Skip body of key */ while (str[end]) { if (isspace(str[end])) break; end++; } } } } if (!j) start = end; } /* Strip leading whitespace if necessary */ //XXX: skip_whitespace() if (flags & FLAG_b) while (isspace(str[start])) start++; /* Strip trailing whitespace if necessary */ if (flags & FLAG_bb) while (end > start && isspace(str[end-1])) end--; /* Handle offsets on start and end */ if (key->range[3]) { end += key->range[3] - 1; if (end > len) end = len; } if (key->range[1]) { start += key->range[1] - 1; if (start > len) start = len; } /* Make the copy */ if (end < start) end = start; str = xstrndup(str+start, end-start); /* Handle -d */ if (flags & FLAG_d) { for (start = end = 0; str[end]; end++) if (isspace(str[end]) || isalnum(str[end])) str[start++] = str[end]; str[start] = '\0'; } /* Handle -i */ if (flags & FLAG_i) { for (start = end = 0; str[end]; end++) if (isprint_asciionly(str[end])) str[start++] = str[end]; str[start] = '\0'; } /* Handle -f */ if (flags & FLAG_f) for (i = 0; str[i]; i++) str[i] = toupper(str[i]); return str; }
int strings_main(int argc UNUSED_PARAM, char **argv) { int c, status = EXIT_SUCCESS; unsigned n, count; off_t offset; FILE *file; char *string; const char *fmt = "%s: "; const char *n_arg = "4"; getopt32(argv, "afon:", &n_arg); /* -a is our default behaviour */ /*argc -= optind;*/ argv += optind; n = xatou_range(n_arg, 1, INT_MAX); string = xzalloc(n + 1); n--; if (!*argv) { fmt = "{%s}: "; *--argv = (char *)bb_msg_standard_input; } do { file = fopen_or_warn_stdin(*argv); if (!file) { status = EXIT_FAILURE; continue; } offset = 0; count = 0; do { c = fgetc(file); if (isprint_asciionly(c) || c == '\t') { if (count > n) { bb_putchar(c); } else { string[count] = c; if (count == n) { if (option_mask32 & PRINT_NAME) { printf(fmt, *argv); } if (option_mask32 & PRINT_OFFSET) { printf("%7"OFF_FMT"o ", offset - n); } fputs(string, stdout); } count++; } } else { if (count > n) { bb_putchar('\n'); } count = 0; } offset++; } while (c != EOF); fclose_if_not_stdin(file); } while (*++argv); if (ENABLE_FEATURE_CLEAN_UP) free(string); fflush_stdout_and_exit(status); }
static char *get_key(char *str, struct sort_key *key, int flags) { int start = start; /* for compiler */ int end; int len, j; unsigned i; /* Special case whole string, so we don't have to make a copy */ if (key->range[0] == 1 && !key->range[1] && !key->range[2] && !key->range[3] && !(flags & (FLAG_b | FLAG_d | FLAG_f | FLAG_i | FLAG_bb)) ) { return str; } /* Find start of key on first pass, end on second pass */ len = strlen(str); for (j = 0; j < 2; j++) { if (!key->range[2*j]) end = len; /* Loop through fields */ else { unsigned char ch = 0; end = 0; for (i = 1; i < key->range[2*j] + j; i++) { if (key_separator) { /* Skip body of key and separator */ while ((ch = str[end]) != '\0') { end++; if (ch == key_separator) break; } } else { /* Skip leading blanks */ while (isspace(str[end])) end++; /* Skip body of key */ while (str[end] != '\0') { if (isspace(str[end])) break; end++; } } } /* Remove last delim: "abc:def:" => "abc:def" */ if (j && ch) { //if (str[end-1] != key_separator) // bb_error_msg(_and_die("BUG! " // "str[start:%d,end:%d]:'%.*s'", // start, end, (int)(end-start), &str[start]); end--; } } if (!j) start = end; } /* Strip leading whitespace if necessary */ if (flags & FLAG_b) /* not using skip_whitespace() for speed */ while (isspace(str[start])) start++; /* Strip trailing whitespace if necessary */ if (flags & FLAG_bb) while (end > start && isspace(str[end-1])) end--; /* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */ if (key->range[3]) { end = key->range[3]; if (end > len) end = len; } /* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */ if (key->range[1]) { start += key->range[1] - 1; if (start > len) start = len; } /* Make the copy */ if (end < start) end = start; str = xstrndup(str+start, end-start); /* Handle -d */ if (flags & FLAG_d) { for (start = end = 0; str[end]; end++) if (isspace(str[end]) || isalnum(str[end])) str[start++] = str[end]; str[start] = '\0'; } /* Handle -i */ if (flags & FLAG_i) { for (start = end = 0; str[end]; end++) if (isprint_asciionly(str[end])) str[start++] = str[end]; str[start] = '\0'; } /* Handle -f */ if (flags & FLAG_f) for (i = 0; str[i]; i++) str[i] = toupper(str[i]); return str; }