home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libdbz / fake.c < prev    next >
C/C++ Source or Header  |  1994-11-30  |  3KB  |  141 lines

  1. /*
  2.  * fake - make up random lines resembling history-file entries, reproducibly
  3.  *
  4.  * -Log-
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. #define    MAXSTR    500        /* For sizing strings -- DON'T use BUFSIZ! */
  14. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  15.  
  16. #ifndef lint
  17. static char RCSid[] = "$Header$";
  18. #endif
  19.  
  20. int midonly = 0;        /* just message ids, rest not realistic */
  21. int tag = 0;            /* tag lines with random digit for later use */
  22. int expired = -1;        /* percentage of lines to be expired */
  23.  
  24. int debug = 0;
  25. char *progname;
  26.  
  27. char *inname;                /* filename for messages etc. */
  28. long lineno;                /* line number for messages etc. */
  29.  
  30. void doline();
  31. void addchars();
  32. void seed();
  33.  
  34. /*
  35.  - main - parse arguments and handle options
  36.  */
  37. main(argc, argv)
  38. int argc;
  39. char *argv[];
  40. {
  41.     int c;
  42.     int errflg = 0;
  43.     FILE *in;
  44.     struct stat statbuf;
  45.     extern int optind;
  46.     extern char *optarg;
  47.     void process();
  48.     register long no;
  49.     char line[MAXSTR];
  50.  
  51.     progname = argv[0];
  52.  
  53.     while ((c = getopt(argc, argv, "ms:te:d")) != EOF)
  54.         switch (c) {
  55.         case 'm':    /* message-ids only */
  56.             midonly = 1;
  57.             break;
  58.         case 's':    /* seed */
  59.             seed(atol(optarg));
  60.             break;
  61.         case 't':    /* tag lines with a random digit */
  62.             tag = 1;
  63.             break;
  64.         case 'e':    /* percentage to be expired */
  65.             expired = atoi(optarg);
  66.             break;
  67.         case 'd':    /* Debugging. */
  68.             debug++;
  69.             break;
  70.         case '?':
  71.         default:
  72.             errflg++;
  73.             break;
  74.         }
  75.     if (errflg || optind != argc - 1) {
  76.         fprintf(stderr, "usage: %s ", progname);
  77.         fprintf(stderr, "[-m] [-s seed] length\n");
  78.         exit(2);
  79.     }
  80.  
  81.     for (no = atol(argv[optind]); no > 0; no--) {
  82.         doline(line);
  83.         puts(line);
  84.     }
  85.     exit(0);
  86. }
  87.  
  88. /*
  89.  - doline - generate random history pseudo-line
  90.  */
  91. void
  92. doline(buf)
  93. char *buf;
  94. {
  95.     char tagch[2];
  96.  
  97.     (void) strcpy(buf, "<");
  98.     addchars(buf, range(4, 20));
  99.     (void) strcat(buf, "@");
  100.     addchars(buf, range(8, 20));
  101.     if (midonly)
  102.         (void) strcat(buf, ">\tx");
  103.     else {
  104.         if (tag) {
  105.             tagch[0] = "1234567890"[range(0,9)];
  106.             tagch[1] = '\0';
  107.             (void) strcat(buf, ">\t");
  108.             (void) strcat(buf, tagch);
  109.             (void) strcat(buf, "00000000~-");
  110.         } else
  111.             (void) strcat(buf, ">\t1234567890~-");
  112.     }
  113.     if (range(1, 100) > expired) {
  114.         if (midonly)
  115.             (void) strcat(buf, "\tx");
  116.         else {
  117.             (void) strcat(buf, "\t");
  118.             addchars(buf, range(10, 30));
  119.         }
  120.     }
  121. }
  122.  
  123. /*
  124.  - addchars - generate n random characters suitable for history file
  125.  */
  126. void
  127. addchars(buf, len)
  128. char *buf;
  129. int len;
  130. {
  131.     register int i;
  132.     register char *p = buf + strlen(buf);
  133.     static char vocab[] = "1234567890.abcde.fghij.klmno.pqrst.uvwxyz.\
  134. 1234567890.ABCDE.FGHIJ.KLMNO.PQRST.UVWXYZ.1234567890.\
  135. 1234567890.abcde.fghij.klmno.pqrst.uvwxyz.1234567890";
  136.  
  137.     for (i = len; i > 0; i--)
  138.         *p++ = vocab[range(0, sizeof(vocab)-2)];
  139.     *p++ = '\0';
  140. }
  141.