123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /* 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;
- void
- die (char *fmt, ...)
- {
- va_list ap;
- fprintf (stderr, "%s: ", progname);
- va_start (ap, fmt);
- vfprintf (stderr, fmt, ap);
- va_end (ap);
- fprintf (stderr, "\n");
- }
- void
- mkhole (int fd, off_t displ)
- {
- if (displ = lseek (fd, displ, SEEK_CUR) == -1)
- {
- perror ("lseek");
- exit (1);
- }
- ftruncate (fd, lseek (fd, 0, SEEK_CUR));
- }
- void
- mksparse (int fd, off_t displ, char *marks)
- {
- int i;
- 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);
- }
- }
- }
- void
- usage ()
- {
- printf ("Usage: mksparse filename blocksize disp letters [disp letters...] [disp]\n");
- exit (1);
- }
- 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;
- }
|