]> ruin.nu Git - popboot.git/blob - simprog.c
intital ci of simprog.c
[popboot.git] / simprog.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5
6 void print_help(char name[])
7 {
8     printf("\n");
9     printf("Simulates execution of an program. \n");
10     printf("\n");
11     printf("Usage: %s \n", name);
12     printf("The default action of the program is to return with 0 (success) \n");
13     printf(" -eNN=MM     Return with value NN with a probability of MM% \n");
14     printf("\n");
15 }
16
17
18 void parse_argv(char argv[], int *retval, int *percentage)
19 {
20     if ( sscanf(argv,"-e %d = %d",retval,percentage)!=2 )
21     {
22         printf("Argument sytax error : %s \n", argv);
23         exit(EXIT_FAILURE);
24     }
25
26     if ( *percentage <= 0 )
27     {
28         printf("Percentage argument must be > 0");
29         exit(EXIT_FAILURE);
30     }
31 }
32
33
34
35 int main (int argc, char** argv)
36 {
37     int i, rand_value, sum;
38     int retvals[100];
39     int percentages[100];
40         
41     if (argc==2 && strcmp("--help",argv[1])==0 )
42     {
43         print_help(argv[0]);
44         return 1;
45     }   
46
47
48     for (i=1; i<argc; i++)
49     {
50         parse_argv(argv[i], retvals+i-1, percentages+i-1);
51     }
52
53         
54     /* Get random number in interval [0,100) */
55     srand(time(0));
56     rand_value = rand() % 100;
57     
58     /* See if we want to fire an "error" */
59     sum = 0;
60     for (i=0; i<argc-1; i++)
61     {
62         sum += percentages[i];
63         if ( rand_value < sum )
64         {
65                 printf("Simulated error : %d \n", retvals[i]);
66                 return retvals[i];
67         }
68     }
69
70     /* No "error" */
71     return 0;
72 }