1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/* This file is part of GNU tar test suite
Copyright (C) 2004 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
char *progname;
char *buffer;
size_t buffer_size;
static void die (char const *, ...) __attribute__ ((noreturn,
format (printf, 1, 2)));
static void
die (char const *fmt, ...)
{
va_list ap;
fprintf (stderr, "%s: ", progname);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, "\n");
exit (1);
}
static void
mkhole (int fd, off_t displ)
{
if (lseek (fd, displ, SEEK_CUR) == -1)
{
perror ("lseek");
exit (1);
}
ftruncate (fd, lseek (fd, 0, SEEK_CUR));
}
static void
mksparse (int fd, off_t displ, char *marks)
{
for (; *marks; marks++)
{
memset (buffer, *marks, buffer_size);
if (write(fd, buffer, buffer_size) != buffer_size)
{
perror ("write");
exit (1);
}
if (lseek (fd, displ, SEEK_CUR) == -1)
{
perror ("lseek");
exit (1);
}
}
}
static void usage (void) __attribute__ ((noreturn));
static void
usage (void)
{
printf ("Usage: mksparse filename blocksize disp letters [disp letters...] [disp]\n");
exit (1);
}
static int
xlat_suffix (off_t *vp, char *p)
{
if (p[1])
return 1;
switch (p[0])
{
case 'g':
case 'G':
*vp *= 1024;
case 'm':
case 'M':
*vp *= 1024;
case 'k':
case 'K':
*vp *= 1024;
break;
default:
return 1;
}
return 0;
}
int
main (int argc, char **argv)
{
int i;
int fd;
char *p;
off_t n;
progname = strrchr (argv[0], '/');
if (progname)
progname++;
else
progname = argv[0];
if (argc < 4)
usage ();
fd = open (argv[1], O_CREAT|O_TRUNC|O_RDWR, 0644);
if (fd < 0)
die ("cannot open %s", argv[1]);
n = strtoul (argv[2], &p, 0);
if (n <= 0 || (*p && xlat_suffix (&n, p)))
die ("Invalid buffer size: %s", argv[2]);
buffer_size = n;
buffer = malloc (buffer_size);
if (!buffer)
die ("Not enough memory");
for (i = 3; i < argc; i += 2)
{
off_t displ;
displ = strtoul (argv[i], &p, 0);
if (displ < 0 || (*p && xlat_suffix (&displ, p)))
die ("Invalid displacement: %s", argv[i]);
if (i == argc-1)
{
mkhole (fd, displ);
break;
}
else
mksparse (fd, displ, argv[i+1]);
}
close(fd);
return 0;
}
|