| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 
 |  
      1 /*************************************************************
      2  * Filename: lostrings.c
      3  * Author: Eric Guirbal
      4  * Date: 23/01/2006
      5  *
      6  * Ce programme génère de manière aléatoire une liste de mots
      7  *
      8  * lostrings [arguments]
      9  *   -s <1-65535> nombre de mots
     10  *   -m <1-255> longueur minimale d'un mot
     11  *   -M <1-255> longueur maximale d'un mot
     12  *
     13  */
     14
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <unistd.h>
     18 #include <string.h>
     19 #include <math.h>
     20 #include <time.h>
     21
     22 int
     23 main (int argc, char *argv[])
     24 {
     25
     26   char *options_list = "hs:m:M:";
     27   int option;
     28   unsigned int nb_words = 100;  /* Taille de la liste */
     29   int min_length = 5;           /* Longueur minimale d'un mot */
     30   int max_length = 12;          /* Longueur maximale d'un mot */
     31   int i, j;
     32   int length;                   /* longueur du mot généré */
     33
     34   opterr = 0;                   /* pas de message d'erreur automatique */
     35
     36   while ((option = getopt (argc, argv, options_list)) != -1)
     37     {
     38       switch (option)
     39         {
     40         case 's':
     41           if (atol (optarg) < 1 || atol (optarg) > 65535)
     42             {
     43               fprintf (stderr,
     44                        "%s: le nombre mots doit être compris entre 1 et 65535\n",
     45                        argv[0]);
     46
     47               return EXIT_FAILURE;
     48             }
     49           fprintf (stdout, "nombre de mots: %s\n", optarg);
     50           nb_words = atoi (optarg);
     51           break;
     52         case 'm':
     53           if (atol (optarg) < 1 || atol (optarg) > 255)
     54             {
     55               fprintf (stderr,
     56                        "%s: la longueur minimale doit être comprise entre 1 et 255\n",
     57                        argv[0]);
     58               return EXIT_FAILURE;
     59             }
     60           fprintf (stdout, "longueur minimale d'un mot: %s\n", optarg);
     61           min_length = atoi (optarg);
     62           break;
     63         case 'M':
     64           if (atol (optarg) < 1 || atol (optarg) > 255)
     65             {
     66               fprintf (stderr,
     67                        "%s: la longueur maximale d'un mot doit être comprise entre 1 et 2        55\n",
     68                        argv[0]);
     69               return EXIT_FAILURE;
     70             }
     71           fprintf (stdout, "longueur maximale d'un mot: %s\n", optarg);
     72           max_length = atoi (optarg);
     73           break;
     74         case 'h':
     75           fprintf (stdout, "Usage: %s [options]\n", argv[0]);
     76           fprintf (stdout, "Options:\n");
     77           fprintf (stdout, "  -s <1-65535>     nombre de mots\n");
     78           fprintf (stdout, "  -m <1-255>       longueur minimale d'un mot\n");
     79           fprintf (stdout, "  -M <1-255>       longueur maximale d'un mot\n");
     80           fprintf (stdout, "  -h               affiche cette aide\n");
     81           return EXIT_SUCCESS;
     82         case '?':
     83           fprintf (stderr,
     84                    "%s : argument invalide\nlostrings -h pour plus d'information\n",
     85                    argv[0]);
     86           return EXIT_FAILURE;
     87         }
     88     }
     89   if (max_length < min_length)
     90     {
     91       fprintf (stderr,
     92                "%s: la longueur minimale doit être inférieure ou égale à la longueur maxi        male\n",
     93                argv[0]);
     94       return EXIT_FAILURE;
     95     }
     96
     97
     98   srand (time (NULL));
     99   for (i = 1; i <= nb_words; i++)
    100     {
    101       if (min_length != max_length)
    102         length =
    103           (int) ((double) rand () / ((double) RAND_MAX + 1) *
    104                  (max_length - min_length + 1)) + min_length;
    105       else
    106         length = min_length;
    107       for (j = 1; j <= length; j++)
    108         fprintf (stdout, "%c",
    109                  'a' +
    110                  (int) ((double) rand () / ((double) RAND_MAX + 1) *
    111                         ('z' - 'a' + 1)));
    112       fprintf (stdout, "\n");
    113     }
    114   return EXIT_SUCCESS;
    115 }
~ |