strings.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. Biobuf *fin;
  13. Biobuf fout;
  14. #define MINSPAN 6 /* Min characters in string (default) */
  15. #define BUFSIZE 70
  16. void stringit(char *);
  17. int isprint(Rune);
  18. static int minspan = MINSPAN;
  19. static void
  20. usage(void)
  21. {
  22. fprint(2, "usage: %s [-m min] [file...]\n", argv0);
  23. exits("usage");
  24. }
  25. void
  26. main(int argc, char **argv)
  27. {
  28. int i;
  29. ARGBEGIN{
  30. case 'm':
  31. minspan = atoi(EARGF(usage()));
  32. break;
  33. default:
  34. usage();
  35. break;
  36. }ARGEND
  37. Binit(&fout, 1, OWRITE);
  38. if(argc < 1) {
  39. stringit("/fd/0");
  40. exits(0);
  41. }
  42. for(i = 0; i < argc; i++) {
  43. if(argc > 2)
  44. print("%s:\n", argv[i]);
  45. stringit(argv[i]);
  46. }
  47. exits(0);
  48. }
  49. void
  50. stringit(char *str)
  51. {
  52. int32_t posn, start;
  53. int cnt = 0;
  54. int32_t c;
  55. Rune buf[BUFSIZE];
  56. if ((fin = Bopen(str, OREAD)) == 0) {
  57. perror("open");
  58. return;
  59. }
  60. start = 0;
  61. posn = Boffset(fin);
  62. while((c = Bgetrune(fin)) >= 0) {
  63. if(isprint(c)) {
  64. if(start == 0)
  65. start = posn;
  66. buf[cnt++] = c;
  67. if(cnt == BUFSIZE-1) {
  68. buf[cnt] = 0;
  69. Bprint(&fout, "%8ld: %S ...\n", start, buf);
  70. start = 0;
  71. cnt = 0;
  72. }
  73. } else {
  74. if(cnt >= minspan) {
  75. buf[cnt] = 0;
  76. Bprint(&fout, "%8ld: %S\n", start, buf);
  77. }
  78. start = 0;
  79. cnt = 0;
  80. }
  81. posn = Boffset(fin);
  82. }
  83. if(cnt >= minspan){
  84. buf[cnt] = 0;
  85. Bprint(&fout, "%8ld: %S\n", start, buf);
  86. }
  87. Bterm(fin);
  88. }
  89. int
  90. isprint(Rune r)
  91. {
  92. if (r != Runeerror)
  93. if ((r >= ' ' && r < 0x7F) || r > 0xA0)
  94. return 1;
  95. return 0;
  96. }