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