ascii.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2021 Denys Vlasenko <vda.linux@googlemail.com>
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //config:config ASCII
  8. //config: bool "ascii (784 bytes)"
  9. //config: default y
  10. //config: help
  11. //config: Print ascii table.
  12. //config:
  13. //applet:IF_ASCII(APPLET(ascii, BB_DIR_USR_BIN, BB_SUID_DROP))
  14. //kbuild:lib-$(CONFIG_ASCII) += ascii.o
  15. //usage:#define ascii_trivial_usage NOUSAGE_STR
  16. //usage:#define ascii_full_usage ""
  17. #include "libbb.h"
  18. int ascii_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  19. int ascii_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  20. {
  21. const char *ctrl =
  22. "NUL""SOH""STX""ETX""EOT""ENQ""ACK""BEL"
  23. "BS ""HT ""NL ""VT ""FF ""CR ""SO ""SI "
  24. "DLE""DC1""DC2""DC3""DC4""NAK""SYN""ETB"
  25. "CAN""EM ""SUB""ESC""FS ""GS ""RS ""US "
  26. ;
  27. //TODO: od has a similar table, can we reuse it?
  28. char last[2];
  29. unsigned i;
  30. last[1] = '\0';
  31. printf("Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex\n");
  32. for (i = 0; i < 16; i++) {
  33. printf("%3u %02x %.3s%4u %02x %.3s%4u %02x %c%4u %02x %c%4u %02x %c%4u %02x %c%5u %02x %c%5u %02x %s\n",
  34. i+0x00, i+0x00, ctrl + i*3,
  35. i+0x10, i+0x10, ctrl + i*3 + 16*3,
  36. i+0x20, i+0x20, i+0x20,
  37. i+0x30, i+0x30, i+0x30,
  38. i+0x40, i+0x40, i+0x40,
  39. i+0x50, i+0x50, i+0x50,
  40. i+0x60, i+0x60, i+0x60,
  41. i+0x70, i+0x70, (i+0x70 == 0x7f ? "DEL" : (last[0] = i+0x70, last))
  42. );
  43. }
  44. return EXIT_SUCCESS;
  45. }