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