home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 2 / agavol2.iso / software / icons+ / packs / lustrones-matwb / archives / rand100.lha / rand.c < prev    next >
C/C++ Source or Header  |  1995-01-07  |  2KB  |  74 lines

  1. /*
  2.     rand.c - A program that sends random system commands.
  3.     Copyright (C) 1995 Fergus Duniho
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; version 2 of the License.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <fpd/fpdio.h>
  23. #include <fpd/fpdstrm.h>
  24.  
  25. void setseed ();
  26.  
  27. int main (int argc, char **argv) {
  28.     unsigned int i, j, which, maximum;
  29.     char lines[256], line[256], fname[256];
  30.     FILE *fptr;
  31.  
  32.     if (argc < 2)
  33.         fprintf (stderr,
  34.         "Usage: rand <file> <format_string> [<file> <format> ...]\n"
  35.         "$VER: rand v1.00 (7 Jan 1995)\n"
  36.         "Copyright 1995 Fergus Duniho\n");
  37.  
  38.     setseed();
  39.     for (i = 1; i < argc; i+=2) {
  40.         if ((fptr = fopen(argv[i], "r")) == NULL) {
  41.             fprintf (stderr, "Couldn't find %s.\n", argv[i]);
  42.             continue;
  43.         }
  44.         maximum = fgetp(fptr);
  45.         which = rand() % maximum;
  46.         for (j = 0; j < which; j++)
  47.             next (fptr, '\n');
  48.         getline (fptr, fname, 256);
  49.         fclose (fptr);
  50.         repstr (lines, argv[i+1], "[]", fname, -1);
  51.         while ((j = strcspn(lines, ";")) != 0) {
  52.             restring (line, lines, lines, "", 0, j, 1);
  53.             system (line);
  54.         }
  55.     }
  56. }
  57.  
  58. void setseed () {
  59.     FILE *fptr;
  60.     int seed;
  61.  
  62.     if ((fptr = fopen("SYS:Prefs/randseed", "r")) != NULL) {
  63.         seed = fgetp(fptr);
  64.         fclose (fptr);
  65.     }
  66.     else
  67.         seed = 1;
  68.     srand (seed);
  69.     if ((fptr = fopen("SYS:Prefs/randseed", "w")) != NULL) {
  70.         fprintf (fptr, "%d", rand());
  71.         fclose (fptr);
  72.     }
  73. }
  74.