]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/pwrite.c
2 * Copyright (c) 2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
38 static cmdinfo_t pwrite_cmd
;
45 " writes a range of bytes (in block size increments) from the given offset\n"
48 " 'write 512 20' - writes 20 bytes at 512 bytes into the open file\n"
50 " Writes into a segment of the currently open file, using either a buffer\n"
51 " filled with a set pattern (0xcdcdcdcd) or data read from an input file.\n"
52 " -S -- use an alternate seed number\n"
53 " -i -- specifies an input file from which to source data to write\n"
54 " -d -- open the input file for direct IO\n"
55 " -s -- skip a number of bytes at the start of the input file\n"
56 " The writes are performed in sequential blocks starting at offset, with the\n"
57 " blocksize tunable using the -b option (default blocksize is 4096 bytes).\n"
70 ssize_t bytes
, itotal
= min(bs
,count
);
74 if (fd
> 0) { /* input file given, read buffer first */
75 if (!read_buffer(fd
, skip
+ *total
, bs
, &itotal
, 0, 1))
78 bytes
= pwrite64(fdesc
, buffer
, min(itotal
,count
), offset
);
99 off64_t offset
, skip
= 0;
100 ssize_t count
, total
;
101 unsigned int seed
= 0xcdcdcdcd;
102 unsigned int bsize
= 4096;
103 char *sp
, *infile
= NULL
;
104 int c
, fd
= -1, dflag
= 0;
106 while ((c
= getopt(argc
, argv
, "b:df:i:s:S:")) != EOF
) {
109 bsize
= strtoul(optarg
, &sp
, 0);
110 if (!sp
|| sp
== optarg
) {
111 printf(_("non-numeric bsize -- %s\n"), optarg
);
123 skip
= strtoul(optarg
, &sp
, 0);
124 if (!sp
|| sp
== optarg
) {
125 printf(_("non-numeric skip -- %s\n"), optarg
);
130 seed
= strtoul(optarg
, &sp
, 0);
131 if (!sp
|| sp
== optarg
) {
132 printf(_("non-numeric seed -- %s\n"), optarg
);
137 printf("%s %s\n", pwrite_cmd
.name
, pwrite_cmd
.oneline
);
141 if ( ((skip
|| dflag
) && !infile
) || (optind
!= argc
- 2)) {
142 printf("%s %s\n", pwrite_cmd
.name
, pwrite_cmd
.oneline
);
145 offset
= strtoul(argv
[optind
], &sp
, 0);
146 if (!sp
|| sp
== argv
[optind
]) {
147 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
151 count
= strtoul(argv
[optind
], &sp
, 0);
152 if (!sp
|| sp
== argv
[optind
]) {
153 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
157 if (!alloc_buffer(bsize
, seed
))
160 if (infile
&& ((fd
= openfile(infile
, 0, 0, dflag
, 1, 0, 0, 0)) < 0))
163 if (!write_buffer(offset
, count
, bsize
, fd
, skip
, &total
)) {
167 printf(_("wrote %u/%u bytes at offset %llu\n"), count
, total
, offset
);
175 pwrite_cmd
.name
= _("pwrite");
176 pwrite_cmd
.altname
= _("w");
177 pwrite_cmd
.cfunc
= pwrite_f
;
178 pwrite_cmd
.argmin
= 2;
179 pwrite_cmd
.argmax
= -1;
181 _("[-i infile [-d] [-s skip]] [-b bs] [-S seed] off len");
183 _("writes a number of bytes at a specified offset");
184 pwrite_cmd
.help
= pwrite_help
;
186 add_command(&pwrite_cmd
);