2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
41 static char path[128];
43 #define INPUTFILE "/bin/date"
50 sprintf(path,"%s.%05d", getprogname(), getpid());
51 if (mkdir(path, 0770) < 0)
52 err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
54 if (chdir(path) == -1)
55 err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
64 if (rmdir(path) == -1) {
65 warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
81 for (i = 0; i < 100 && done_testing == 0; i++) {
82 sprintf(file,"p%05d.%05d", pid, i);
84 if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
87 if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
90 if (fstat(fdin, &statbuf) < 0)
91 err(1, "fstat error");
93 if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
94 err(1, "lseek error");
96 /* write a dummy byte at the last location */
97 if (write(fdout, "", 1) != 1)
98 err(1, "write error");
100 if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
102 err(1, "mmap error for input");
104 if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
105 MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
106 err(1, "mmap error for output");
108 memcpy(dst, src, statbuf.st_size);
111 if (munmap(src, statbuf.st_size) == -1)
115 if (munmap(dst, statbuf.st_size) == -1)
119 if (unlink(file) == -1)
120 err(3, "unlink(%s)", file);