1178620Smarcel/*-
2178620Smarcel * Copyright (c) 2000 Sheldon Hearn <sheldonh@FreeBSD.org>.
3178620Smarcel * All rights reserved.
4178620Smarcel *
5178620Smarcel * Redistribution and use in source and binary forms, with or without
6178620Smarcel * modification, are permitted provided that the following conditions
7178620Smarcel * are met:
8178620Smarcel * 1. Redistributions of source code must retain the above copyright
9178620Smarcel *    notice, this list of conditions and the following disclaimer.
10178620Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11178620Smarcel *    notice, this list of conditions and the following disclaimer in the
12178620Smarcel *    documentation and/or other materials provided with the distribution.
13178620Smarcel *
14178620Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15178620Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16178620Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17178620Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18178620Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19178620Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20178620Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21178620Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22178620Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23178620Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24178620Smarcel * SUCH DAMAGE.
25178620Smarcel *
26178620Smarcel */
27178620Smarcel
28178620Smarcel#ifndef lint
29178620Smarcelstatic const char rcsid[] =
30178620Smarcel    "$FreeBSD: releng/10.2/usr.bin/truncate/truncate.c 275585 2014-12-07 22:30:54Z jilles $";
31178620Smarcel#endif
32178620Smarcel
33178620Smarcel#include <sys/stat.h>
34178620Smarcel
35178620Smarcel#include <ctype.h>
36178620Smarcel#include <err.h>
37178620Smarcel#include <errno.h>
38178620Smarcel#include <fcntl.h>
39178620Smarcel#include <stdio.h>
40178620Smarcel#include <stdlib.h>
41178620Smarcel#include <unistd.h>
42178620Smarcel
43178620Smarcel#include <libutil.h>
44178620Smarcel
45178620Smarcelstatic void	usage(void);
46178620Smarcel
47178620Smarcelstatic int	no_create;
48178620Smarcelstatic int	do_relative;
49178620Smarcelstatic int	do_refer;
50178620Smarcelstatic int	got_size;
51178620Smarcel
52178620Smarcelint
53178620Smarcelmain(int argc, char **argv)
54178620Smarcel{
55178620Smarcel	struct stat	sb;
56178620Smarcel	mode_t	omode;
57178620Smarcel	off_t	oflow, rsize, sz, tsize;
58178620Smarcel	uint64_t usz;
59178620Smarcel	int	ch, error, fd, oflags;
60178620Smarcel	char   *fname, *rname;
61178620Smarcel
62178620Smarcel	fd = -1;
63178620Smarcel	rsize = tsize = sz = 0;
64178620Smarcel	error = 0;
65178620Smarcel	rname = NULL;
66178620Smarcel	while ((ch = getopt(argc, argv, "cr:s:")) != -1)
67178620Smarcel		switch (ch) {
68178620Smarcel		case 'c':
69178620Smarcel			no_create = 1;
70178620Smarcel			break;
71178620Smarcel		case 'r':
72178620Smarcel			do_refer = 1;
73178620Smarcel			rname = optarg;
74178620Smarcel			break;
75178620Smarcel		case 's':
76178620Smarcel			do_relative = *optarg == '+' || *optarg == '-';
77178620Smarcel			if (expand_number(do_relative ? optarg + 1 : optarg,
78178620Smarcel			    &usz) == -1 || (off_t)usz < 0)
79178620Smarcel				errx(EXIT_FAILURE,
80178620Smarcel				    "invalid size argument `%s'", optarg);
81178620Smarcel
82178620Smarcel			sz = (*optarg == '-') ? -(off_t)usz : (off_t)usz;
83178620Smarcel			got_size = 1;
84178620Smarcel			break;
85178620Smarcel		default:
86178620Smarcel			usage();
87178620Smarcel			/* NOTREACHED */
88178620Smarcel		}
89178620Smarcel
90178620Smarcel	argv += optind;
91178620Smarcel	argc -= optind;
92178620Smarcel
93	/*
94	 * Exactly one of do_refer or got_size must be specified.  Since
95	 * do_relative implies got_size, do_relative and do_refer are
96	 * also mutually exclusive.  See usage() for allowed invocations.
97	 */
98	if (do_refer + got_size != 1 || argc < 1)
99		usage();
100	if (do_refer) {
101		if (stat(rname, &sb) == -1)
102			err(EXIT_FAILURE, "%s", rname);
103		tsize = sb.st_size;
104	} else if (do_relative)
105		rsize = sz;
106	else
107		tsize = sz;
108
109	if (no_create)
110		oflags = O_WRONLY;
111	else
112		oflags = O_WRONLY | O_CREAT;
113	omode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
114
115	while ((fname = *argv++) != NULL) {
116		if (fd != -1)
117			close(fd);
118		if ((fd = open(fname, oflags, omode)) == -1) {
119			if (errno != ENOENT) {
120				warn("%s", fname);
121				error++;
122			}
123			continue;
124		}
125		if (do_relative) {
126			if (fstat(fd, &sb) == -1) {
127				warn("%s", fname);
128				error++;
129				continue;
130			}
131			oflow = sb.st_size + rsize;
132			if (oflow < (sb.st_size + rsize)) {
133				errno = EFBIG;
134				warn("%s", fname);
135				error++;
136				continue;
137			}
138			tsize = oflow;
139		}
140		if (tsize < 0)
141			tsize = 0;
142
143		if (ftruncate(fd, tsize) == -1) {
144			warn("%s", fname);
145			error++;
146			continue;
147		}
148	}
149	if (fd != -1)
150		close(fd);
151
152	return error ? EXIT_FAILURE : EXIT_SUCCESS;
153}
154
155static void
156usage(void)
157{
158	fprintf(stderr, "%s\n%s\n",
159	    "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g|T|t] file ...",
160	    "       truncate [-c] -r rfile file ...");
161	exit(EXIT_FAILURE);
162}
163