strings.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. Biobuf *fin;
  5. Biobuf fout;
  6. #define MINSPAN 6 /* Min characters in string */
  7. #define BUFSIZE 70
  8. void stringit(char *);
  9. int isprint(Rune);
  10. void
  11. main(int argc, char **argv)
  12. {
  13. int i;
  14. Binit(&fout, 1, OWRITE);
  15. if(argc < 2) {
  16. stringit("/fd/0");
  17. exits(0);
  18. }
  19. for(i = 1; i < argc; i++) {
  20. if(argc > 2)
  21. print("%s:\n", argv[i]);
  22. stringit(argv[i]);
  23. }
  24. exits(0);
  25. }
  26. void
  27. stringit(char *str)
  28. {
  29. long posn, start;
  30. int cnt = 0;
  31. long c;
  32. Rune buf[BUFSIZE];
  33. if ((fin = Bopen(str, OREAD)) == 0) {
  34. perror("open");
  35. return;
  36. }
  37. start = 0;
  38. posn = Boffset(fin);
  39. while((c = Bgetrune(fin)) >= 0) {
  40. if(isprint(c)) {
  41. if(start == 0)
  42. start = posn;
  43. buf[cnt++] = c;
  44. if(cnt == BUFSIZE-1) {
  45. buf[cnt] = 0;
  46. Bprint(&fout, "%8ld: %S ...\n", start, buf);
  47. start = 0;
  48. cnt = 0;
  49. }
  50. } else {
  51. if(cnt >= MINSPAN) {
  52. buf[cnt] = 0;
  53. Bprint(&fout, "%8ld: %S\n", start, buf);
  54. }
  55. start = 0;
  56. cnt = 0;
  57. }
  58. posn = Boffset(fin);
  59. }
  60. if(cnt >= MINSPAN){
  61. buf[cnt] = 0;
  62. Bprint(&fout, "%8ld: %S\n", start, buf);
  63. }
  64. Bterm(fin);
  65. }
  66. int
  67. isprint(Rune r)
  68. {
  69. if ((r >= ' ' && r <0x7f) || r > 0xA0)
  70. return 1;
  71. else
  72. return 0;
  73. }