]> Dogcows Code - chaz/tar/blob - tests/mksparse.c
Merge recent gnulib changes, and remove some lint.
[chaz/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 static void die (char const *, ...) __attribute__ ((noreturn,
34 format (printf, 1, 2)));
35 static void
36 die (char const *fmt, ...)
37 {
38 va_list ap;
39
40 fprintf (stderr, "%s: ", progname);
41 va_start (ap, fmt);
42 vfprintf (stderr, fmt, ap);
43 va_end (ap);
44 fprintf (stderr, "\n");
45 exit (1);
46 }
47
48 static void
49 mkhole (int fd, off_t displ)
50 {
51 if (lseek (fd, displ, SEEK_CUR) == -1)
52 {
53 perror ("lseek");
54 exit (1);
55 }
56 ftruncate (fd, lseek (fd, 0, SEEK_CUR));
57 }
58
59 static void
60 mksparse (int fd, off_t displ, char *marks)
61 {
62 for (; *marks; marks++)
63 {
64 memset (buffer, *marks, buffer_size);
65 if (write(fd, buffer, buffer_size) != buffer_size)
66 {
67 perror ("write");
68 exit (1);
69 }
70
71 if (lseek (fd, displ, SEEK_CUR) == -1)
72 {
73 perror ("lseek");
74 exit (1);
75 }
76 }
77 }
78
79 static void usage (void) __attribute__ ((noreturn));
80 static void
81 usage (void)
82 {
83 printf ("Usage: mksparse filename blocksize disp letters [disp letters...] [disp]\n");
84 exit (1);
85 }
86
87 static int
88 xlat_suffix (off_t *vp, char *p)
89 {
90 if (p[1])
91 return 1;
92 switch (p[0])
93 {
94 case 'g':
95 case 'G':
96 *vp *= 1024;
97
98 case 'm':
99 case 'M':
100 *vp *= 1024;
101
102 case 'k':
103 case 'K':
104 *vp *= 1024;
105 break;
106
107 default:
108 return 1;
109 }
110 return 0;
111 }
112
113 int
114 main (int argc, char **argv)
115 {
116 int i;
117 int fd;
118 char *p;
119 off_t n;
120
121 progname = strrchr (argv[0], '/');
122 if (progname)
123 progname++;
124 else
125 progname = argv[0];
126
127 if (argc < 4)
128 usage ();
129
130 fd = open (argv[1], O_CREAT|O_TRUNC|O_RDWR, 0644);
131 if (fd < 0)
132 die ("cannot open %s", argv[1]);
133
134 n = strtoul (argv[2], &p, 0);
135 if (n <= 0 || (*p && xlat_suffix (&n, p)))
136 die ("Invalid buffer size: %s", argv[2]);
137 buffer_size = n;
138 buffer = malloc (buffer_size);
139 if (!buffer)
140 die ("Not enough memory");
141
142 for (i = 3; i < argc; i += 2)
143 {
144 off_t displ;
145
146 displ = strtoul (argv[i], &p, 0);
147 if (displ < 0 || (*p && xlat_suffix (&displ, p)))
148 die ("Invalid displacement: %s", argv[i]);
149
150 if (i == argc-1)
151 {
152 mkhole (fd, displ);
153 break;
154 }
155 else
156 mksparse (fd, displ, argv[i+1]);
157 }
158
159 close(fd);
160 return 0;
161 }
This page took 0.038098 seconds and 4 git commands to generate.