/* * strings: show strings inside of a file */ #include #include extern int getopt(); extern int opterr, optind; extern char *optarg; main(argc, argv) char **argv; { register FILE *fd; FILE *fopen(); register c; register char *cp; char line[180]; register len=2; opterr = 1; while ((c=getopt(argc, argv, "l:")) != EOF) switch (c) { case 'l': len = atoi(optarg); if (len < 1) { fprintf(stderr, "strings: -l length must be > 0\n"); exit(2); } break; default: fprintf(stderr, "usages: strings [-l length] [file]\n"); exit(255); } if (argc-optind < 1) fd = stdin; else { if ((fd=fopen(argv[optind],"rb")) == NULL) { fprintf(stderr, "strings: cannot open %s\n", argv[optind]); exit(1); } } cp = NULL; while ((c=getc(fd)) != EOF) { if ((c == 0 || c == '\n') && cp && cp >= len+line) { *cp = 0; puts(line); } else if (c >= ' ' && c <= '~') { if (cp == NULL) cp = line; else if (cp >= line+sizeof(line)-2) { *cp++ = '\\'; *cp = 0; puts(line); cp = line; } *cp++ = c; continue; } else while ((c=getc(fd)) != 0) if (c == EOF) break; cp = NULL; } } /* strings */