10 #include "output-rait.h"
12 #define tape_open rait_open
13 #define tapefd_read rait_read
14 #define tapefd_write rait_write
15 #define tapefd_setinfo_length(outfd, length)
16 #define tapefd_close rait_close
25 static int debug_amdd = 0;
26 static char *pgm = NULL;
31 fprintf(stderr, "usage: %s ", pgm);
32 fprintf(stderr, " [-d]");
33 fprintf(stderr, " [-l length]");
34 fprintf(stderr, " [if=input]");
35 fprintf(stderr, " [of=output]");
36 fprintf(stderr, " [bs=blocksize]");
37 fprintf(stderr, " [count=count]");
38 fprintf(stderr, " [skip=count]");
39 fprintf(stderr, "\n");
44 main(int argc, char **argv) {
45 int infd = 0; /* stdin */
46 int outfd = 1; /* stdout */
50 int pread, fread, pwrite, fwrite;
60 ssize_t (*read_func)(int, void *, size_t);
61 ssize_t (*write_func)(int, const void *, size_t);
63 if((pgm = strrchr(argv[0], '/')) != NULL) {
68 while(-1 != (ch = getopt(argc, argv, "hdl:"))) {
72 fprintf(stderr, "debug mode!\n");
76 length = atoi(optarg);
79 switch(optarg[len-1] ) {
81 case 'b': length /= 2; break;
82 case 'M': length *= 1024; break;
83 default: length /= 1024; break;
98 for( ; optind < argc; optind++) {
99 if(0 == (eq = strchr(argv[optind], '='))) {
103 len = eq - argv[optind];
104 if(0 == strncmp("if", argv[optind], len)) {
105 if((infd = tape_open(eq + 1, O_RDONLY)) < 0) {
107 fprintf(stderr, "%s: %s: ", pgm, eq + 1);
112 read_func = tapefd_read;
114 fprintf(stderr, "input opened \"%s\", got fd %d\n",
117 } else if(0 == strncmp("of", argv[optind], len)) {
118 if((outfd = tape_open(eq + 1, O_RDWR|O_CREAT|O_TRUNC, 0644)) < 0) {
120 fprintf(stderr, "%s: %s: ", pgm, eq + 1);
125 write_func = tapefd_write;
127 fprintf(stderr, "output opened \"%s\", got fd %d\n",
132 fprintf(stderr, "length set to %d\n", length);
134 tapefd_setinfo_length(outfd, length);
136 } else if(0 == strncmp("bs", argv[optind], len)) {
137 blocksize = atoi(eq + 1);
138 len = strlen(argv[optind]);
140 switch(argv[optind][len-1] ) {
141 case 'k': blocksize *= 1024; break;
142 case 'b': blocksize *= 512; break;
143 case 'M': blocksize *= 1024 * 1024; break;
147 fprintf(stderr, "blocksize set to %d\n", blocksize);
149 } else if(0 == strncmp("count", argv[optind], len)) {
150 count = atoi(eq + 1);
153 fprintf(stderr, "count set to %d\n", count);
155 } else if(0 == strncmp("skip", argv[optind], len)) {
158 fprintf(stderr, "skip set to %d\n", skip);
161 fprintf(stderr, "%s: bad argument: \"%s\"\n", pgm, argv[optind]);
166 if(0 == (buf = malloc(blocksize))) {
168 fprintf(stderr, "%s: ", pgm);
170 perror("malloc error");
175 pread = fread = pwrite = fwrite = 0;
176 while(0 < (len = (*read_func)(infd, buf, blocksize))) {
180 if(len == blocksize) {
185 len = (*write_func)(outfd, buf, len);
189 } else if(len == blocksize) {
203 fprintf(stderr, "%s: ", pgm);
208 fprintf(stderr, "%d+%d in\n%d+%d out\n", fread, pread, fwrite, pwrite);
209 if(read_func == tapefd_read) {
210 if(0 != tapefd_close(infd)) {
212 fprintf(stderr, "%s: ", pgm);
214 perror("input close");
218 if(write_func == tapefd_write) {
219 if(0 != tapefd_close(outfd)) {
221 fprintf(stderr, "%s: ", pgm);
223 perror("output close");