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 /*
31*2fae26bdSAlan Somers * Create a file with assigned size and then free the specified
32*2fae26bdSAlan Somers * section of the file
33*2fae26bdSAlan Somers */
34*2fae26bdSAlan Somers
35*2fae26bdSAlan Somers static void usage(char *progname);
36*2fae26bdSAlan Somers
37*2fae26bdSAlan Somers static void
usage(char * progname)38*2fae26bdSAlan Somers usage(char *progname)
39*2fae26bdSAlan Somers {
40*2fae26bdSAlan Somers (void) fprintf(stderr,
41*2fae26bdSAlan Somers "usage: %s [-l filesize] [-s start-offset]"
42*2fae26bdSAlan Somers "[-n section-len] filename\n", progname);
43*2fae26bdSAlan Somers exit(1);
44*2fae26bdSAlan Somers }
45*2fae26bdSAlan Somers
46*2fae26bdSAlan Somers int
main(int argc,char * argv[])47*2fae26bdSAlan Somers main(int argc, char *argv[])
48*2fae26bdSAlan Somers {
49*2fae26bdSAlan Somers char *filename, *buf;
50*2fae26bdSAlan Somers size_t filesize;
51*2fae26bdSAlan Somers off_t start_off, off_len;
52*2fae26bdSAlan Somers int fd, ch;
53*2fae26bdSAlan Somers struct flock fl;
54*2fae26bdSAlan Somers
55*2fae26bdSAlan Somers while ((ch = getopt(argc, argv, "l:s:n:")) != EOF) {
56*2fae26bdSAlan Somers switch (ch) {
57*2fae26bdSAlan Somers case 'l':
58*2fae26bdSAlan Somers filesize = atoll(optarg);
59*2fae26bdSAlan Somers break;
60*2fae26bdSAlan Somers case 's':
61*2fae26bdSAlan Somers start_off = atoll(optarg);
62*2fae26bdSAlan Somers break;
63*2fae26bdSAlan Somers case 'n':
64*2fae26bdSAlan Somers off_len = atoll(optarg);
65*2fae26bdSAlan Somers break;
66*2fae26bdSAlan Somers default:
67*2fae26bdSAlan Somers usage(argv[0]);
68*2fae26bdSAlan Somers break;
69*2fae26bdSAlan Somers }
70*2fae26bdSAlan Somers }
71*2fae26bdSAlan Somers
72*2fae26bdSAlan Somers if (optind == argc - 1)
73*2fae26bdSAlan Somers filename = argv[optind];
74*2fae26bdSAlan Somers else
75*2fae26bdSAlan Somers usage(argv[0]);
76*2fae26bdSAlan Somers
77*2fae26bdSAlan Somers buf = (char *)malloc(filesize);
78*2fae26bdSAlan Somers
79*2fae26bdSAlan Somers if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC)) < 0) {
80*2fae26bdSAlan Somers perror("open");
81*2fae26bdSAlan Somers return (1);
82*2fae26bdSAlan Somers }
83*2fae26bdSAlan Somers if (write(fd, buf, filesize) < filesize) {
84*2fae26bdSAlan Somers perror("write");
85*2fae26bdSAlan Somers return (1);
86*2fae26bdSAlan Somers }
87*2fae26bdSAlan Somers #if UNSUPPORTED
88*2fae26bdSAlan Somers fl.l_whence = SEEK_SET;
89*2fae26bdSAlan Somers fl.l_start = start_off;
90*2fae26bdSAlan Somers fl.l_len = off_len;
91*2fae26bdSAlan Somers if (fcntl(fd, F_FREESP, &fl) != 0) {
92*2fae26bdSAlan Somers perror("fcntl");
93*2fae26bdSAlan Somers return (1);
94*2fae26bdSAlan Somers }
95*2fae26bdSAlan Somers #else
96*2fae26bdSAlan Somers fprintf(stderr, "fcntl: F_FREESP not supported\n");
97*2fae26bdSAlan Somers return (1);
98*2fae26bdSAlan Somers #endif
99*2fae26bdSAlan Somers
100*2fae26bdSAlan Somers free(buf);
101*2fae26bdSAlan Somers return (0);
102*2fae26bdSAlan Somers }
103