]>
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
) {
67 buffer
= realloc(buffer
, buffersize
= bsize
);
74 memset(buffer
, seed
, buffersize
);
86 for (i
= 0, p
= (char *)buffer
; i
< len
; i
+= 16) {
89 printf("%08llx: ", (off64_t
)i
+ offset
);
90 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
93 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
116 bytes
= pread64(fd
, buffer
, min(count
,buffersize
), offset
);
124 dump_buffer(offset
, bytes
);
126 if (onlyone
|| bytes
< count
)
140 ssize_t count
, total
;
141 unsigned int bsize
= 4096;
146 while ((c
= getopt(argc
, argv
, "b:v")) != EOF
) {
149 bsize
= strtoul(optarg
, &sp
, 0);
150 if (!sp
|| sp
== optarg
) {
151 printf(_("non-numeric bsize -- %s\n"), optarg
);
159 printf("%s %s\n", pread_cmd
.name
, pread_cmd
.oneline
);
163 if (optind
!= argc
- 2) {
164 printf("%s %s\n", pread_cmd
.name
, pread_cmd
.oneline
);
167 offset
= strtoul(argv
[optind
], &sp
, 0);
168 if (!sp
|| sp
== argv
[optind
]) {
169 printf(_("non-numeric offset argument -- %s\n"), argv
[optind
]);
173 count
= strtoul(argv
[optind
], &sp
, 0);
174 if (!sp
|| sp
== argv
[optind
]) {
175 printf(_("non-numeric length argument -- %s\n"), argv
[optind
]);
179 if (!alloc_buffer(bsize
, 0xabababab))
182 if (!read_buffer(fdesc
, offset
, count
, &total
, vflag
, 0))
185 printf(_("read %u/%u bytes at offset %llu\n"), total
, count
, offset
);
192 pread_cmd
.name
= _("pread");
193 pread_cmd
.altname
= _("r");
194 pread_cmd
.cfunc
= pread_f
;
195 pread_cmd
.argmin
= 2;
196 pread_cmd
.argmax
= -1;
197 pread_cmd
.args
= _("[-b bs] [-v] off len");
198 pread_cmd
.oneline
= _("reads a number of bytes at a specified offset");
199 pread_cmd
.help
= pread_help
;
201 add_command(&pread_cmd
);