Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/truncate_test.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* This file and its contents are supplied under the terms of the3* Common Development and Distribution License ("CDDL"), version 1.0.4* You may only use this file in accordance with the terms of version5* 1.0 of the CDDL.6*7* A full copy of the text of the CDDL should have accompanied this8* source. A copy of the CDDL is also available via the Internet at9* http://www.illumos.org/license/CDDL.10*/1112/*13* Copyright (c) 2012, 2014 by Delphix. All rights reserved.14* Copyright 2017, loli10K <[email protected]>. All rights reserved.15*/1617#include <fcntl.h>18#include <sys/stat.h>19#include <sys/types.h>20#include <unistd.h>21#include <errno.h>22#include <stdio.h>23#include <stdlib.h>2425#define FSIZE 256*1024*10242627static long fsize = FSIZE;28static int errflag = 0;29static char *filename = NULL;30static int ftruncflag = 0;3132static void parse_options(int argc, char *argv[]);3334static void35usage(char *execname)36{37(void) fprintf(stderr,38"usage: %s [-s filesize] [-f] /path/to/file\n", execname);39(void) exit(1);40}4142int43main(int argc, char *argv[])44{45int fd;4647parse_options(argc, argv);4849if (ftruncflag) {50fd = open(filename, O_RDWR|O_CREAT, 0666);51if (fd < 0) {52perror("open");53return (1);54}55if (ftruncate(fd, fsize) < 0) {56perror("ftruncate");57return (1);58}59if (close(fd)) {60perror("close");61return (1);62}63} else {64if (truncate(filename, fsize) < 0) {65perror("truncate");66return (1);67}68}69return (0);70}7172static void73parse_options(int argc, char *argv[])74{75int c;76extern char *optarg;77extern int optind, optopt;7879while ((c = getopt(argc, argv, "s:f")) != -1) {80switch (c) {81case 's':82fsize = atoi(optarg);83break;84case 'f':85ftruncflag++;86break;87case ':':88(void) fprintf(stderr,89"Option -%c requires an operand\n", optopt);90errflag++;91break;92}93if (errflag) {94(void) usage(argv[0]);95}96}9798if (argc <= optind) {99(void) fprintf(stderr, "No filename specified\n");100usage(argv[0]);101}102filename = argv[optind];103}104105106