]> ruin.nu Git - popboot.git/commitdiff
intital ci of simprog.c
authorMårten Dolk <dolk@dtek.chalmers.se>
Thu, 19 May 2005 13:30:24 +0000 (13:30 +0000)
committerMårten Dolk <dolk@dtek.chalmers.se>
Thu, 19 May 2005 13:30:24 +0000 (13:30 +0000)
simprog.c [new file with mode: 0644]

diff --git a/simprog.c b/simprog.c
new file mode 100644 (file)
index 0000000..a713847
--- /dev/null
+++ b/simprog.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+void print_help(char name[])
+{
+    printf("\n");
+    printf("Simulates execution of an program. \n");
+    printf("\n");
+    printf("Usage: %s \n", name);
+    printf("The default action of the program is to return with 0 (success) \n");
+    printf(" -eNN=MM     Return with value NN with a probability of MM% \n");
+    printf("\n");
+}
+
+
+void parse_argv(char argv[], int *retval, int *percentage)
+{
+    if ( sscanf(argv,"-e %d = %d",retval,percentage)!=2 )
+    {
+       printf("Argument sytax error : %s \n", argv);
+       exit(EXIT_FAILURE);
+    }
+
+    if ( *percentage <= 0 )
+    {
+       printf("Percentage argument must be > 0");
+       exit(EXIT_FAILURE);
+    }
+}
+
+
+
+int main (int argc, char** argv)
+{
+    int i, rand_value, sum;
+    int retvals[100];
+    int percentages[100];
+       
+    if (argc==2 && strcmp("--help",argv[1])==0 )
+    {
+       print_help(argv[0]);
+       return 1;
+    }  
+
+
+    for (i=1; i<argc; i++)
+    {
+       parse_argv(argv[i], retvals+i-1, percentages+i-1);
+    }
+
+        
+    /* Get random number in interval [0,100) */
+    srand(time(0));
+    rand_value = rand() % 100;
+    
+    /* See if we want to fire an "error" */
+    sum = 0;
+    for (i=0; i<argc-1; i++)
+    {
+       sum += percentages[i];
+       if ( rand_value < sum )
+       {
+               printf("Simulated error : %d \n", retvals[i]);
+               return retvals[i];
+       }
+    }
+
+    /* No "error" */
+    return 0;
+}