File: bomstrip.c

package info (click to toggle)
bomstrip 9-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 252 kB
  • sloc: pascal: 41; ansic: 34; cpp: 34; java: 33; python: 31; sh: 24; makefile: 20; perl: 7; php: 6; ruby: 6; haskell: 6; awk: 2; sed: 1
file content (40 lines) | stat: -rwxr-xr-x 725 bytes parent folder | download | duplicates (3)
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

char		 buf[BUFSIZ];
const char	* const utf8bom = "\xef\xbb\xbf";

static void	 usage(const char *);

static void
usage(const char * const prog)
{
	fprintf(stderr, "usage: %s\n", prog);
	exit(1);
}

int
main(const int argc, const char * const argv[])
{
	size_t nread;

	if (argc > 1)
		usage(argv[0]);

	nread = fread(buf, 1, strlen(utf8bom), stdin);
	if (nread == 0)
		return 0;
	if (strcmp(buf, utf8bom) != 0)
		if (fwrite(buf, 1, nread, stdout) < nread)
			exit(1);
	for (;;) {
		nread = fread(buf, 1, sizeof buf, stdin);
		if (nread == 0)
			return ferror(stdin) && 1;
		if (fwrite(buf, 1, nread, stdout) < nread)
			exit(1);
	}
	return 0;
}