File: nctrunc.c

package info (click to toggle)
netcdf-parallel 1%3A4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 105,352 kB
  • sloc: ansic: 229,114; sh: 11,180; yacc: 2,561; makefile: 1,390; lex: 1,173; xml: 173; awk: 2
file content (56 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download | duplicates (2)
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
/*********************************************************************
 *   Copyright 2018, UCAR/Unidata
 *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
 *********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.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 = fopen(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);
}