]> git.somenet.org - pub/astra/parallel.git/blob - prefix/prefix.c
getopt fix: will now fail when reading more than $file.size from $file.
[pub/astra/parallel.git] / prefix / prefix.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <getopt.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <omp.h>
8 #include "prefix.h"
9
10 /* This one's binary name. */
11 char* binname = "unset";
12
13
14 void usage () {
15         fprintf (stderr, "\nUsage: %s [-f <string>] [-n <number>]\n", binname);
16         fprintf (stderr, "\t -f: set the filename of the randfile. (defaults to \"numlist.bin\")\n");
17         fprintf (stderr, "\t -n: set the number count to read from randfile. (defaults to 0 = read all)\n");
18         fprintf (stderr, "\nThis application prints only TIME-INFO to stdout. Everything else goes to stderr.\n");
19         exit(1);
20 }
21
22 void array_contents(numtype array[], unsigned long size) {
23         fprintf (stderr, "[%li", array[0]);
24         for (unsigned long i = 1; i < size; i++) {
25                 fprintf (stderr, ", %li", array[i]);
26         }
27         fprintf (stderr, "]\n");
28 }
29
30
31 int main (int argc, char* argv[]) {
32         FILE *file = NULL;
33         unsigned long size;
34         numtype *numarray;
35         //unsigned int countarray[NUMBERS];
36         double startTime, endTime;
37
38         /* options */
39         unsigned long n = 0;
40         char *filename = "numlist.bin";
41
42         /* store out name for usage(); */
43         binname = argv[0];
44
45         /* getopt stuff */
46         int c;
47         opterr = 0;
48         while ((c = getopt (argc, argv, "n:f:")) != -1) switch (c){
49                 case 'n':
50                         n = strtoul (optarg,NULL,0);
51                         break;
52                 case 'f':
53                         filename = optarg;
54                         break;
55                 case '?':
56                         if (optopt == 'f' || optopt == 'n')
57                                 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
58                         else if (isprint (optopt))
59                                 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
60                         else
61                                 fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
62                         usage();
63                 default:
64                         usage();
65         }
66
67         /* sanize n. */
68         if ((file = fopen(filename, "r"))) {
69                 fclose(file);
70                 file = NULL;
71                 struct stat st;
72                 stat(filename, &st);
73                 size = st.st_size;
74                 if (n == 0){
75                         n = size;
76                 }
77                 if (n < 0 || n > size){
78                         fprintf (stderr, "Cannot read %li numbers from \"%s\" - only has %li bytes.\n",n,filename,st.st_size);
79                         usage();
80                 }
81         }else{
82                 fprintf (stderr, "File %s does not exist.\n", filename);
83                 usage();
84         }
85
86         /* print info and start reading file. */
87         fprintf(stderr, "Will read %li numbers from \"%s\".\n", n, filename);
88
89         numarray = malloc(n * sizeof(numtype));
90         if(numarray == NULL) {
91                 fprintf(stderr, "malloc for numarray failed.\n");
92                 exit(2);
93         } else {
94                 fprintf(stderr, "malloc for numarray sucessful.\n");
95         }
96
97         /* read file. */
98         if ((file = fopen(filename, "r"))) {
99                 for (size = 0; size < n; size++) {
100                         numarray[size] = fgetc(file);
101                 }
102                 fclose(file);
103         }else{
104                 fprintf (stderr, "File %s could not be read.\n", filename);
105                 usage();
106         }
107
108         fprintf(stderr, "file read - ready.\n");
109
110 #ifdef DEBUG
111         /* might want to comment this out, if numarray is big */
112         array_contents(numarray, size);
113 #endif
114
115         fprintf(stderr, "init done starting algorithm. size: %li\n", size);
116         startTime = omp_get_wtime();
117         algorithm(numarray, size, NULL);
118         endTime = omp_get_wtime();
119         fprintf(stdout, "binname=%s size=%li time=%f result=%li\n", binname, size, endTime-startTime, numarray[size-1]);
120
121 #ifdef DEBUG
122         /* might want to comment this out, if numarray is big */
123         array_contents(numarray, size);
124 #endif
125
126         return 0;
127 }