-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathnctrunc.c
57 lines (52 loc) · 1.11 KB
/
nctrunc.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
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "nc_tests.h"
#define BUFLEN 100000
int
main(int argc, char** argv)
{
unsigned char buffer[BUFLEN];
size_t count, red, avail, trunc;
unsigned char* p = buffer;
size_t i;
FILE* input = stdin;
if(argc > 1) {
input = NCfopen(argv[1],"r");
}
/* Read the whole file */
p = buffer;
red = 0;
avail = BUFLEN;
for(;;) {
count = fread(p,1,avail,input);
p += count;
red += count;
avail -= count;
if(feof(input)) break;
}
trunc = red;
for(i=(red-1);i>=0;i--) {
if(buffer[i] != '\0') {
trunc = i + 1;
break;
}
}
p = buffer;
avail = trunc;
for(;;) {
count = fwrite(p,1,avail,stdout);
if(count == 0) break;
p += count;
avail -= count;
if(avail == 0) break;
}
if(avail > 0)
exit(1);
else
exit(0);
}