]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/mkchan.c
Flushing the missing meter.c
[dttsp.git] / jDttSP / mkchan.c
1 /* mkchan.c */
2 /* create a file big enough to accommodate
3    a header + a ringbuffer of a given size */
4
5 #include <sys/types.h>
6 #include <sys/param.h>
7 #include <sys/stat.h>
8 #include <sys/time.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11
12 #include <stdlib.h>
13 #include <values.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <math.h>
18 #include <assert.h>
19 #include <libgen.h>
20
21 #include <datatypes.h>
22 #include <banal.h>
23 #include <ringb.h>
24
25 char *rng_name = 0,
26      *buf_asks = 0;
27 FILE *rng_file = 0;
28 size_t rng_size = 0,
29        buf_size = 0,
30        blk_size = 0,
31        tot_size = 0;
32
33 BOOLEAN verbose = FALSE;
34
35 void
36 execute(void) {
37   int i;
38   rng_size = sizeof(ringb_t);
39   if ((buf_size = atoi(buf_asks)) <= 0) {
40     fprintf(stderr, "buffer size %s?\n", buf_asks);
41     exit(1);
42   }
43   if (!(rng_file = fopen(rng_name, "w"))) {
44     perror(rng_name);
45     exit(1);
46   }
47   blk_size = nblock2(buf_size);
48   tot_size = rng_size + blk_size;
49   for (i = 0; i < tot_size; i++) putc(0, rng_file);
50   fclose(rng_file);
51   if (verbose)
52     fprintf(stderr,
53             "created chan file %s (%d + [%d -> %d] = %d)\n",
54             rng_name, rng_size, buf_size, blk_size, tot_size);
55 }
56
57 void
58 closeup(void) { exit(0); }
59
60 static void
61 usage(void) {
62   fprintf(stderr, "usage:\n");
63   fprintf(stderr, "mkchan [-v] name size\n");
64   exit(1);
65 }
66
67 static void
68 setup(int argc, char **argv) {
69   int i;
70
71   for (i = 1; i < argc; i++)
72     if (argv[i][0] == '-')
73       switch (argv[i][1]) {
74       case 'v': verbose = TRUE; break;
75       default: usage();
76       }
77     else break;
78   if (i < (argc - 2)) usage();
79   rng_name = argv[i++];
80   buf_asks = argv[i++];
81 }
82
83 int
84 main(int argc, char **argv) { setup(argc, argv), execute(), closeup(); }