]> ruin.nu Git - popboot.git/blob - simprog.c
delete remaining actions
[popboot.git] / simprog.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
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     struct timeval td;
41         
42     if (argc==2 && strcmp("--help",argv[1])==0 )
43     {
44         print_help(argv[0]);
45         return 1;
46     }   
47
48
49     for (i=1; i<argc; i++)
50     {
51         parse_argv(argv[i], retvals+i-1, percentages+i-1);
52     }
53
54         
55     /* Get random number in interval [0,100) */
56     gettimeofday(&td,0);
57     srand(td.tv_usec);
58     rand_value = rand() % 100;
59     
60     /* See if we want to fire an "error" */
61     sum = 0;
62     for (i=0; i<argc-1; i++)
63     {
64         sum += percentages[i];
65         if ( rand_value < sum )
66         {
67                 printf("Simulated error : %d \n", retvals[i]);
68                 return retvals[i];
69         }
70     }
71
72     /* No "error" */
73     return 0;
74 }