xref: /freebsd/tests/sys/cddl/zfs/bin/file_check.c (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
1*2fae26bdSAlan Somers /*
2*2fae26bdSAlan Somers  * CDDL HEADER START
3*2fae26bdSAlan Somers  *
4*2fae26bdSAlan Somers  * The contents of this file are subject to the terms of the
5*2fae26bdSAlan Somers  * Common Development and Distribution License (the "License").
6*2fae26bdSAlan Somers  * You may not use this file except in compliance with the License.
7*2fae26bdSAlan Somers  *
8*2fae26bdSAlan Somers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2fae26bdSAlan Somers  * or http://www.opensolaris.org/os/licensing.
10*2fae26bdSAlan Somers  * See the License for the specific language governing permissions
11*2fae26bdSAlan Somers  * and limitations under the License.
12*2fae26bdSAlan Somers  *
13*2fae26bdSAlan Somers  * When distributing Covered Code, include this CDDL HEADER in each
14*2fae26bdSAlan Somers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2fae26bdSAlan Somers  * If applicable, add the following below this CDDL HEADER, with the
16*2fae26bdSAlan Somers  * fields enclosed by brackets "[]" replaced with your own identifying
17*2fae26bdSAlan Somers  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2fae26bdSAlan Somers  *
19*2fae26bdSAlan Somers  * CDDL HEADER END
20*2fae26bdSAlan Somers  */
21*2fae26bdSAlan Somers 
22*2fae26bdSAlan Somers /*
23*2fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24*2fae26bdSAlan Somers  * Use is subject to license terms.
25*2fae26bdSAlan Somers  */
26*2fae26bdSAlan Somers 
27*2fae26bdSAlan Somers 
28*2fae26bdSAlan Somers #include "file_common.h"
29*2fae26bdSAlan Somers 
30*2fae26bdSAlan Somers static unsigned char bigbuffer[BIGBUFFERSIZE];
31*2fae26bdSAlan Somers 
32*2fae26bdSAlan Somers /*
33*2fae26bdSAlan Somers  * Given a filename, check that the file consists entirely
34*2fae26bdSAlan Somers  * of a particular pattern. If the pattern is not specified a
35*2fae26bdSAlan Somers  * default will be used. For default values see file_common.h
36*2fae26bdSAlan Somers  */
37*2fae26bdSAlan Somers int
main(int argc,char ** argv)38*2fae26bdSAlan Somers main(int argc, char **argv)
39*2fae26bdSAlan Somers {
40*2fae26bdSAlan Somers 	int		bigfd;
41*2fae26bdSAlan Somers 	long		i, n;
42*2fae26bdSAlan Somers 	uint8_t		fillchar = DATA;
43*2fae26bdSAlan Somers 	int		bigbuffersize = BIGBUFFERSIZE;
44*2fae26bdSAlan Somers 	int64_t		read_count = 0;
45*2fae26bdSAlan Somers 
46*2fae26bdSAlan Somers 	/*
47*2fae26bdSAlan Somers 	 * Validate arguments
48*2fae26bdSAlan Somers 	 */
49*2fae26bdSAlan Somers 	if (argc < 2) {
50*2fae26bdSAlan Somers 		(void) printf("Usage: %s filename [pattern]\n",
51*2fae26bdSAlan Somers 		    argv[0]);
52*2fae26bdSAlan Somers 		exit(1);
53*2fae26bdSAlan Somers 	}
54*2fae26bdSAlan Somers 
55*2fae26bdSAlan Somers 	if (argv[2]) {
56*2fae26bdSAlan Somers 		fillchar = atoi(argv[2]);
57*2fae26bdSAlan Somers 	}
58*2fae26bdSAlan Somers 
59*2fae26bdSAlan Somers 	/*
60*2fae26bdSAlan Somers 	 * Read the file contents and check every character
61*2fae26bdSAlan Somers 	 * against the supplied pattern. Abort if the
62*2fae26bdSAlan Somers 	 * pattern check fails.
63*2fae26bdSAlan Somers 	 */
64*2fae26bdSAlan Somers 	if ((bigfd = open(argv[1], O_RDONLY)) == -1) {
65*2fae26bdSAlan Somers 		(void) printf("open %s failed %d\n", argv[1], errno);
66*2fae26bdSAlan Somers 		exit(1);
67*2fae26bdSAlan Somers 	}
68*2fae26bdSAlan Somers 
69*2fae26bdSAlan Somers 	do {
70*2fae26bdSAlan Somers 		if ((n = read(bigfd, &bigbuffer, bigbuffersize)) == -1) {
71*2fae26bdSAlan Somers 			(void) printf("read failed (%ld), %d\n", n, errno);
72*2fae26bdSAlan Somers 			exit(errno);
73*2fae26bdSAlan Somers 		}
74*2fae26bdSAlan Somers 
75*2fae26bdSAlan Somers 		for (i = 0; i < n; i++) {
76*2fae26bdSAlan Somers 			if (bigbuffer[i] != fillchar) {
77*2fae26bdSAlan Somers 				(void) printf("error %s: 0x%x != 0x%x)\n",
78*2fae26bdSAlan Somers 				    argv[1], bigbuffer[i], fillchar);
79*2fae26bdSAlan Somers 				exit(1);
80*2fae26bdSAlan Somers 			}
81*2fae26bdSAlan Somers 		}
82*2fae26bdSAlan Somers 
83*2fae26bdSAlan Somers 		read_count += n;
84*2fae26bdSAlan Somers 	} while (n == bigbuffersize);
85*2fae26bdSAlan Somers 
86*2fae26bdSAlan Somers 	return (0);
87*2fae26bdSAlan Somers }
88