xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/file/file_check.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2716fd348SMartin Matuska /*
3716fd348SMartin Matuska  * CDDL HEADER START
4716fd348SMartin Matuska  *
5716fd348SMartin Matuska  * The contents of this file are subject to the terms of the
6716fd348SMartin Matuska  * Common Development and Distribution License (the "License").
7716fd348SMartin Matuska  * You may not use this file except in compliance with the License.
8716fd348SMartin Matuska  *
9716fd348SMartin Matuska  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
11716fd348SMartin Matuska  * See the License for the specific language governing permissions
12716fd348SMartin Matuska  * and limitations under the License.
13716fd348SMartin Matuska  *
14716fd348SMartin Matuska  * When distributing Covered Code, include this CDDL HEADER in each
15716fd348SMartin Matuska  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16716fd348SMartin Matuska  * If applicable, add the following below this CDDL HEADER, with the
17716fd348SMartin Matuska  * fields enclosed by brackets "[]" replaced with your own identifying
18716fd348SMartin Matuska  * information: Portions Copyright [yyyy] [name of copyright owner]
19716fd348SMartin Matuska  *
20716fd348SMartin Matuska  * CDDL HEADER END
21716fd348SMartin Matuska  */
22716fd348SMartin Matuska 
23716fd348SMartin Matuska /*
24716fd348SMartin Matuska  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25716fd348SMartin Matuska  * Use is subject to license terms.
26716fd348SMartin Matuska  */
27716fd348SMartin Matuska 
28716fd348SMartin Matuska #include "file_common.h"
29716fd348SMartin Matuska 
30716fd348SMartin Matuska static unsigned char bigbuffer[BIGBUFFERSIZE];
31716fd348SMartin Matuska 
32716fd348SMartin Matuska /*
33716fd348SMartin Matuska  * Given a filename, check that the file consists entirely
34716fd348SMartin Matuska  * of a particular pattern. If the pattern is not specified a
35716fd348SMartin Matuska  * default will be used. For default values see file_common.h
36716fd348SMartin Matuska  */
37716fd348SMartin Matuska int
main(int argc,char ** argv)38716fd348SMartin Matuska main(int argc, char **argv)
39716fd348SMartin Matuska {
40716fd348SMartin Matuska 	int		bigfd;
41716fd348SMartin Matuska 	long		i, n;
42716fd348SMartin Matuska 	unsigned char	fillchar = DATA;
43716fd348SMartin Matuska 	int		bigbuffersize = BIGBUFFERSIZE;
44716fd348SMartin Matuska 
45716fd348SMartin Matuska 	/*
46716fd348SMartin Matuska 	 * Validate arguments
47716fd348SMartin Matuska 	 */
48716fd348SMartin Matuska 	if (argc < 2) {
49716fd348SMartin Matuska 		(void) printf("Usage: %s filename [pattern]\n",
50716fd348SMartin Matuska 		    argv[0]);
51716fd348SMartin Matuska 		exit(1);
52716fd348SMartin Matuska 	}
53716fd348SMartin Matuska 
54716fd348SMartin Matuska 	if (argv[2]) {
55716fd348SMartin Matuska 		fillchar = atoi(argv[2]);
56716fd348SMartin Matuska 	}
57716fd348SMartin Matuska 
58716fd348SMartin Matuska 	/*
59716fd348SMartin Matuska 	 * Read the file contents and check every character
60716fd348SMartin Matuska 	 * against the supplied pattern. Abort if the
61716fd348SMartin Matuska 	 * pattern check fails.
62716fd348SMartin Matuska 	 */
63716fd348SMartin Matuska 	if ((bigfd = open(argv[1], O_RDONLY)) == -1) {
64716fd348SMartin Matuska 		(void) printf("open %s failed %d\n", argv[1], errno);
65716fd348SMartin Matuska 		exit(1);
66716fd348SMartin Matuska 	}
67716fd348SMartin Matuska 
68716fd348SMartin Matuska 	do {
69716fd348SMartin Matuska 		if ((n = read(bigfd, &bigbuffer, bigbuffersize)) == -1) {
70716fd348SMartin Matuska 			(void) printf("read failed (%ld), %d\n", n, errno);
71716fd348SMartin Matuska 			exit(errno);
72716fd348SMartin Matuska 		}
73716fd348SMartin Matuska 
74716fd348SMartin Matuska 		for (i = 0; i < n; i++) {
75716fd348SMartin Matuska 			if (bigbuffer[i] != fillchar) {
76716fd348SMartin Matuska 				(void) printf("error %s: 0x%x != 0x%x)\n",
77716fd348SMartin Matuska 				    argv[1], bigbuffer[i], fillchar);
78716fd348SMartin Matuska 				exit(1);
79716fd348SMartin Matuska 			}
80716fd348SMartin Matuska 		}
81716fd348SMartin Matuska 	} while (n == bigbuffersize);
82716fd348SMartin Matuska 
83716fd348SMartin Matuska 	return (0);
84716fd348SMartin Matuska }
85