Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.8 KiB

  1. /* STRINGS.C:
  2. * This program dumps all the ascii strings in a
  3. * file.
  4. *
  5. * History:
  6. * 05-Jan-1992 IanJa obtained from \\toolsvr\contrib\petes\slm\src\TOOLS
  7. * 05-Jan-1992 IanJa use buffered reads for small efficiency gain
  8. * 22-Feb-1992 IanJa Unicode strings too if -U used.
  9. * 17-Feb-1997 RajeevD added -a option
  10. */
  11. #include <stdio.h> /* Standard I/O definitions */
  12. #include <stdlib.h> /* Standard atoi types */
  13. #include <process.h>
  14. #define EOL "\n"
  15. #define RDBIN "rb"
  16. #define isascii(c) ((c) >= ' ' && (c) <= '~')
  17. /* True if char is printable ascii */
  18. #define isswitch(s) (((s)[0] == '-') || ((s)[0] == '/'))
  19. /* True if string is switch */
  20. #define swchar(s) ((s)[1]) /* Switch character */
  21. #define MAXLEN 128 /* Max. minimum "string" length */
  22. char prognam[] = "strings";
  23. #define USAGE_FMT "usage: %s [-a][-U][-O][-l <length>]<file list>\n"
  24. #define BUFSZ 512
  25. char inBuf[BUFSZ];
  26. void
  27. __cdecl main(argc,argv)
  28. int argc;
  29. char **argv;
  30. {
  31. FILE *fi;
  32. char save[MAXLEN];
  33. int strlen;
  34. char *p;
  35. int nRead;
  36. int i;
  37. int argi;
  38. long offset = 0L;
  39. int threshold = 4;
  40. int foffset = 0;
  41. int fUnicode = 0;
  42. int fAnnotate = 0;
  43. if(argc == 1)
  44. {
  45. fprintf(stderr,USAGE_FMT,argv[0]);
  46. fprintf(stderr," -a\tannotates each match with filename\n");
  47. exit(1);
  48. }
  49. for(argi = 1,i = 1; argi < argc; ++argi)
  50. {
  51. if(isswitch(argv[argi]))
  52. {
  53. switch(swchar(argv[argi]))
  54. {
  55. case 'U':
  56. fUnicode = 1;
  57. break;
  58. case 'O':
  59. foffset = 1;
  60. break;
  61. case 'l':
  62. if (argv[argi][2])
  63. {
  64. threshold = atoi(&argv[argi][2]);
  65. }
  66. else if(++argi == argc)
  67. {
  68. fprintf(stderr,"%s: missing length\n",argv[0]);
  69. exit(1);
  70. }
  71. else
  72. {
  73. threshold = atoi(argv[argi]);
  74. }
  75. if(threshold > MAXLEN)
  76. {
  77. fprintf(stderr, "%s: length %d should be <= %d\n",
  78. argv[0], threshold, MAXLEN);
  79. exit(1);
  80. }
  81. break;
  82. case 'a':
  83. case 'A':
  84. fAnnotate = 1;
  85. break;
  86. case '?':
  87. fprintf(stderr,USAGE_FMT,argv[0]);
  88. fprintf(stderr," -a\tannotates each match with filename\n");
  89. exit(1);
  90. default:
  91. fprintf(stderr,"%s: unknown switch \"%s\"\n",
  92. argv[0],argv[argi]);
  93. exit(1);
  94. }
  95. }
  96. else argv[i++] = argv[argi];
  97. }
  98. argc = i;
  99. for(i = 1; i < argc; ++i)
  100. {
  101. if((fi = fopen(argv[i],RDBIN)) == NULL)
  102. {
  103. fprintf(stderr,"%s: cannot open \"%s\"\n",argv[0],argv[i]);
  104. continue;
  105. }
  106. if (!fAnnotate)
  107. fprintf(stdout,"%s:\n",argv[i]);
  108. strlen = 0;
  109. save[threshold - 1] = '\0';
  110. while((nRead = fread(inBuf, 1, BUFSZ, fi)) > 0)
  111. {
  112. for (p = inBuf; p < &inBuf[nRead]; p++)
  113. {
  114. if(isascii(*p))
  115. {
  116. if(strlen < threshold - 1) save[strlen++] = *p;
  117. else
  118. {
  119. if(strlen++ == threshold - 1)
  120. {
  121. if (fAnnotate)
  122. fprintf (stdout, "%s: ", argv[i]);
  123. if(foffset) fprintf(stdout,"%06lx: ",
  124. offset - threshold + 1);
  125. fputs(save,stdout);
  126. }
  127. putc(*p,stdout);
  128. }
  129. }
  130. else
  131. {
  132. if(strlen >= threshold) fputs(EOL,stdout);
  133. strlen = 0;
  134. }
  135. ++offset;
  136. }
  137. if (fUnicode)
  138. {
  139. for (p = inBuf; p < &inBuf[nRead-1]; p++)
  140. {
  141. if(isascii(*p) && (p[1] == 0x00))
  142. {
  143. if(strlen < threshold - 1) save[strlen++] = *p;
  144. else
  145. {
  146. if(strlen++ == threshold - 1)
  147. {
  148. if (fAnnotate)
  149. fprintf (stdout, "%s: ", argv[i]);
  150. fprintf(stdout, "U: ");
  151. if(foffset) fprintf(stdout,"%06lx: ",
  152. offset - threshold + 1);
  153. fputs(save,stdout);
  154. }
  155. putc(*p,stdout);
  156. }
  157. ++p;
  158. }
  159. else
  160. {
  161. if(strlen >= threshold) fputs(EOL,stdout);
  162. strlen = 0;
  163. }
  164. ++offset;
  165. }
  166. }
  167. }
  168. if(strlen >= threshold) fputs(EOL,stdout);
  169. if (!feof(fi))
  170. {
  171. fprintf(stderr,"%s: error reading \"%s\"\n",argv[0],argv[i]);
  172. continue;
  173. }
  174. fclose(fi);
  175. }
  176. exit(0);
  177. }