This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bddiff.c
69 lines (54 loc) · 1.77 KB
/
bddiff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* bddiff Performs a binary difference in double files
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include "utils.h"
#define BUFFER 4096
int main(int argc, char *argv[])
{
if(argc != 2) {
fprintf(stderr, "Performs a binary float subtraction between two datasets\n");
fprintf(stderr, "\n\tDSET1 - DSET2 = RESULT\n");
fprintf(stderr, "\nExample:\n");
fprintf(stderr, " $ %s DSET2.bin < DSET1.bin > RESULT.bin\n", argv[0]);
fprintf(stderr, "\nAlso can be used in a pipe as:\n");
fprintf(stderr, " $ cat DSET1.bin | %s DSET2.bin > RESULT.bin\n", argv[0]);
fprintf(stderr, "\nDSET1 and DSET2 should be of same size if don't the output");
fprintf(stderr, "is clipped on the shorter.\n");
exit(1);
}
if(isatty(fileno(stdin))) {
fprintf(stderr, "STDIN must be a 'pipe' or 'file'\n");
exit(0);
}
if(isatty(fileno(stdout))) {
fprintf(stderr, "STDOUT must be a 'pipe' or 'file'\n");
exit(1);
}
/* filenames */
char filename[PATH_MAX];
/* file buffers */
double buf[BUFFER];
double bufin[BUFFER];
double bufout[BUFFER];
/* Size read */
size_t bytes_read_buf;
size_t bytes_read_stdin;
size_t bytes_read;
strcpy(filename, argv[1]);
FILE *fp = fopen(filename, "rb");
while((bytes_read_buf = fread(buf, sizeof(double), BUFFER, fp)) &&
(bytes_read_stdin =fread(bufin, sizeof(double), BUFFER, stdin)))
{
bytes_read = imin(bytes_read_buf, bytes_read_stdin);
for(size_t i=0; i<bytes_read; i++)
bufout[i] = bufin[i] - buf[i];
fwrite(bufout, sizeof(double), bytes_read, stdout);
}
fclose(fp);
return 0;
}