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