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