]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/pread.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/
39 static cmdinfo_t pread_cmd
;
46 " reads a range of bytes in a specified block size from the given offset\n"
49 " 'read -v 512 20' - dumps 20 bytes read from 512 bytes into the file\n"
51 " Reads a segment of the currently open file, optionally dumping it to the\n"
52 " standard output stream (with -v option) for subsequent inspection.\n"
53 " The reads are performed in sequential blocks starting at offset, with the\n"
54 " blocksize tunable using the -b option (default blocksize is 4096 bytes).\n"
66 if (bsize
> buffersize
) {
69 buffer
= memalign(getpagesize(), buffersize
= bsize
);
76 memset(buffer
, seed
, buffersize
);
88 for (i
= 0, p
= (char *)buffer
; i
< len
; i
+= 16) {
91 printf("%08llx: ", (off64_t
)i
+ offset
);
92 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
95 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
118 bytes
= pread64(fd
, buffer
, min(count
,buffersize
), offset
);
126 dump_buffer(offset
, bytes
);
128 if (onlyone
|| bytes
< count
)
142 ssize_t count
, total
;
143 unsigned int bsize
= 4096;
148 while ((c
= getopt(argc
, argv
, "b:v")) != EOF
) {
151 bsize
= strtoul(optarg
, &sp
, 0);
152 if (!sp
|| sp
== optarg
) {
153 printf(_("non-numeric bsize -- %s\n"), optarg
);
161 printf("%s %s\n", pread_cmd
.name
, pread_cmd
.oneline
);
165 if (optind
!= argc
- 2) {
166 printf("%s %s\n", pread_cmd
.name
, pread_cmd
.oneline
);
169 offset
= strtoul(argv
[optind
], &sp
, 0);
170 if (!sp
|| sp
== argv
[optind
]) {
171 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
175 count
= strtoul(argv
[optind
], &sp
, 0);
176 if (!sp
|| sp
== argv
[optind
]) {
177 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
181 if (!alloc_buffer(bsize
, 0xabababab))
184 if (!read_buffer(fdesc
, offset
, count
, &total
, vflag
, 0))
187 printf(_("read %u/%u bytes at offset %llu\n"), total
, count
, offset
);
194 pread_cmd
.name
= _("pread");
195 pread_cmd
.altname
= _("r");
196 pread_cmd
.cfunc
= pread_f
;
197 pread_cmd
.argmin
= 2;
198 pread_cmd
.argmax
= -1;
199 pread_cmd
.args
= _("[-b bs] [-v] off len");
200 pread_cmd
.oneline
= _("reads a number of bytes at a specified offset");
201 pread_cmd
.help
= pread_help
;
203 add_command(&pread_cmd
);