dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / test / zfs-tests / cmd / randfree_file / randfree_file.c
blobdc9ae8b8a136837d9c0f822cc8f3b1e380d25fbd
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
31 #include "../file_common.h"
34 * Create a file with assigned size and then free the specified
35 * section of the file
38 static void usage(char *progname);
40 static void
41 usage(char *progname)
43 (void) fprintf(stderr,
44 "usage: %s [-l filesize] [-s start-offset]"
45 "[-n section-len] filename\n", progname);
46 exit(1);
49 int
50 main(int argc, char *argv[])
52 char *filename = NULL;
53 char *buf;
54 size_t filesize = 0;
55 off_t start_off = 0;
56 off_t off_len = 0;
57 int fd, ch;
58 struct flock fl;
59 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
61 while ((ch = getopt(argc, argv, "l:s:n:")) != EOF) {
62 switch (ch) {
63 case 'l':
64 filesize = atoll(optarg);
65 break;
66 case 's':
67 start_off = atoll(optarg);
68 break;
69 case 'n':
70 off_len = atoll(optarg);
71 break;
72 default:
73 usage(argv[0]);
74 break;
78 if (optind == argc - 1)
79 filename = argv[optind];
80 else
81 usage(argv[0]);
83 buf = (char *)malloc(filesize);
85 if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, mode)) < 0) {
86 perror("open");
87 return (1);
89 if (write(fd, buf, filesize) < filesize) {
90 perror("write");
91 return (1);
93 fl.l_whence = SEEK_SET;
94 fl.l_start = start_off;
95 fl.l_len = off_len;
96 if (fcntl(fd, F_FREESP, &fl) != 0) {
97 perror("fcntl");
98 return (1);
101 free(buf);
102 return (0);