void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile) { #if ENABLE_UNICODE_SUPPORT init_unicode(); p->curfile = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20); #else p->curfile = curfile; #endif p->start_sec = monotonic_sec(); p->last_update_sec = p->start_sec; p->last_change_sec = p->start_sec; p->last_size = 0; }
int expand_main(int argc UNUSED_PARAM, char **argv) { /* Default 8 spaces for 1 tab */ const char *opt_t = "8"; FILE *file; unsigned tab_size; unsigned opt; int exit_status = EXIT_SUCCESS; #if ENABLE_FEATURE_EXPAND_LONG_OPTIONS static const char expand_longopts[] ALIGN1 = /* name, has_arg, val */ "initial\0" No_argument "i" "tabs\0" Required_argument "t" ; #endif #if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS static const char unexpand_longopts[] ALIGN1 = /* name, has_arg, val */ "first-only\0" No_argument "i" "tabs\0" Required_argument "t" "all\0" No_argument "a" ; #endif init_unicode(); if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) { IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts); opt = getopt32(argv, "it:", &opt_t); } else { IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts); /* -t NUM sets also -a */ opt_complementary = "ta"; opt = getopt32(argv, "ft:a", &opt_t); /* -f --first-only is the default */ if (!(opt & OPT_ALL)) opt |= OPT_INITIAL; } tab_size = xatou_range(opt_t, 1, UINT_MAX); argv += optind; if (!*argv) { *--argv = (char*)bb_msg_standard_input; } do { file = fopen_or_warn_stdin(*argv); if (!file) { exit_status = EXIT_FAILURE; continue; } if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) IF_EXPAND(expand(file, tab_size, opt)); else IF_UNEXPAND(unexpand(file, tab_size, opt)); /* Check and close the file */ if (fclose_if_not_stdin(file)) { bb_simple_perror_msg(*argv); exit_status = EXIT_FAILURE; } /* If stdin also clear EOF */ if (file == stdin) clearerr(file); } while (*++argv); /* Now close stdin also */ /* (if we didn't read from it, it's a no-op) */ if (fclose(stdin)) bb_perror_msg_and_die(bb_msg_standard_input); fflush_stdout_and_exit(exit_status); }
int rev_main(int argc UNUSED_PARAM, char **argv) { int retval; size_t bufsize; char *buf; init_unicode(); getopt32(argv, ""); argv += optind; if (!argv[0]) argv = (char **)&bb_argv_dash; retval = EXIT_SUCCESS; bufsize = 256; buf = xmalloc(bufsize); do { size_t pos; FILE *fp; fp = fopen_or_warn_stdin(*argv++); if (!fp) { retval = EXIT_FAILURE; continue; } pos = 0; while (1) { /* Read one line */ buf[bufsize - 1] = 1; /* not 0 */ if (!fgets(buf + pos, bufsize - pos, fp)) break; /* EOF/error */ if (buf[bufsize - 1] == '\0' /* fgets filled entire buffer */ && buf[bufsize - 2] != '\n' /* and did not read '\n' */ && !feof(fp) ) { /* Line is too long, extend buffer */ pos = bufsize - 1; bufsize += 64 + bufsize / 8; buf = xrealloc(buf, bufsize); continue; } /* Process and print it */ #if ENABLE_UNICODE_SUPPORT { wchar_t *tmp = xmalloc(bufsize * sizeof(wchar_t)); /* Convert to wchar_t (might error out!) */ int len = mbstowcs(tmp, buf, bufsize); if (len >= 0) { strrev(tmp, len); /* Convert back to char */ wcstombs(buf, tmp, bufsize); } free(tmp); } #else strrev(buf, strlen(buf)); #endif fputs(buf, stdout); } fclose(fp); } while (*argv); if (ENABLE_FEATURE_CLEAN_UP) free(buf); fflush_stdout_and_exit(retval); }
int fold_main(int argc UNUSED_PARAM, char **argv) { char *line_out = NULL; const char *w_opt = "80"; unsigned width; smallint exitcode = EXIT_SUCCESS; init_unicode(); if (ENABLE_INCLUDE_SUSv2) { /* Turn any numeric options into -w options. */ int i; for (i = 1; argv[i]; i++) { const char *a = argv[i]; if (*a == '-') { a++; if (*a == '-' && !a[1]) /* "--" */ break; if (isdigit(*a)) argv[i] = xasprintf("-w%s", a); } } } getopt32(argv, "bsw:", &w_opt); width = xatou_range(w_opt, 1, 10000); argv += optind; if (!*argv) *--argv = (char*)"-"; do { FILE *istream = fopen_or_warn_stdin(*argv); int c; unsigned column = 0; /* Screen column where next char will go */ unsigned offset_out = 0; /* Index in 'line_out' for next char */ if (istream == NULL) { exitcode = EXIT_FAILURE; continue; } while ((c = getc(istream)) != EOF) { /* We grow line_out in chunks of 0x1000 bytes */ if ((offset_out & 0xfff) == 0) { line_out = xrealloc(line_out, offset_out + 0x1000); } rescan: line_out[offset_out] = c; if (c == '\n') { write2stdout(line_out, offset_out + 1); column = offset_out = 0; continue; } column = adjust_column(column, c); if (column <= width || offset_out == 0) { /* offset_out == 0 case happens * with small width (say, 1) and tabs. * The very first tab already goes to column 8, * but we must not wrap it */ offset_out++; continue; } /* This character would make the line too long. * Print the line plus a newline, and make this character * start the next line */ if (option_mask32 & FLAG_BREAK_SPACES) { unsigned i; unsigned logical_end; /* Look for the last blank. */ for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) { if (!isblank(line_out[logical_end])) continue; /* Found a space or tab. * Output up to and including it, and start a new line */ logical_end++; /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */ write2stdout(line_out, logical_end); putchar('\n'); /* Move the remainder to the beginning of the next line. * The areas being copied here might overlap. */ memmove(line_out, line_out + logical_end, offset_out - logical_end); offset_out -= logical_end; for (column = i = 0; i < offset_out; i++) { column = adjust_column(column, line_out[i]); } goto rescan; } /* No blank found, wrap will split the overlong word */ } /* Output what we accumulated up to now, and start a new line */ line_out[offset_out] = '\n'; write2stdout(line_out, offset_out + 1); column = offset_out = 0; goto rescan; } /* while (not EOF) */ if (offset_out) { write2stdout(line_out, offset_out); } if (fclose_if_not_stdin(istream)) { bb_simple_perror_msg(*argv); exitcode = EXIT_FAILURE; } } while (*++argv); fflush_stdout_and_exit(exitcode); }
int dumpleases_main(int argc UNUSED_PARAM, char **argv) { int fd; int i; unsigned opt; int64_t written_at, curr, expires_abs; const char *file = LEASES_FILE; struct dyn_lease lease; struct in_addr addr; enum { OPT_a = 0x1, // -a OPT_r = 0x2, // -r OPT_f = 0x4, // -f }; #if ENABLE_LONG_OPTS static const char dumpleases_longopts[] ALIGN1 = "absolute\0" No_argument "a" "remaining\0" No_argument "r" "file\0" Required_argument "f" ; applet_long_options = dumpleases_longopts; #endif init_unicode(); opt_complementary = "=0:a--r:r--a"; opt = getopt32(argv, "arf:", &file); fd = xopen(file, O_RDONLY); printf("Mac Address IP Address Host Name Expires %s\n", (opt & OPT_a) ? "at" : "in"); /* "00:00:00:00:00:00 255.255.255.255 ABCDEFGHIJKLMNOPQRS Wed Jun 30 21:49:08 1993" */ /* "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 */ xread(fd, &written_at, sizeof(written_at)); written_at = SWAP_BE64(written_at); curr = time(NULL); if (curr < written_at) written_at = curr; /* lease file from future! :) */ while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { const char *fmt = ":%02x" + 1; for (i = 0; i < 6; i++) { printf(fmt, lease.lease_mac[i]); fmt = ":%02x"; } addr.s_addr = lease.lease_nip; #if ENABLE_UNICODE_SUPPORT { char *uni_name = unicode_conv_to_printable_fixedwidth(/*NULL,*/ lease.hostname, 19); printf(" %-16s%s ", inet_ntoa(addr), uni_name); free(uni_name); } #else /* actually, 15+1 and 19+1, +1 is a space between columns */ /* lease.hostname is char[20] and is always NUL terminated */ printf(" %-16s%-20s", inet_ntoa(addr), lease.hostname); #endif expires_abs = ntohl(lease.expires) + written_at; if (expires_abs <= curr) { puts("expired"); continue; } if (!(opt & OPT_a)) { /* no -a */ unsigned d, h, m; unsigned expires = expires_abs - curr; d = expires / (24*60*60); expires %= (24*60*60); h = expires / (60*60); expires %= (60*60); m = expires / 60; expires %= 60; if (d) printf("%u days ", d); printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); } else { /* -a */ time_t t = expires_abs; fputs(ctime(&t), stdout); } } /* close(fd); */ return 0; }