Project

General

Profile

Bug #7367 ยป rdisk.c

Youzhong Yang, 2017-01-26 07:53 PM

 
1
#include <sys/types.h>
2
#include <sys/stat.h>
3
#include <fcntl.h>
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7

    
8
int main(int argc, char **argv)
9
{
10
        char *disk = NULL;
11
        int fd;
12
        uchar_t *buf;
13
        int requested_size = 512;
14
	int offset = 0;
15
        int bytes;
16

    
17
        if (argc < 2) {
18
                printf("Usage: %s /dev/rdsk/... [bytes to read] [offset]\n");
19
                exit(1);
20
        }
21

    
22
        disk = argv[1];
23
        if (argc > 2) requested_size = atoi(argv[2]);
24
        if (argc > 3) offset = atoi(argv[3]);
25
        buf = calloc(requested_size, 1);
26
        if (buf == NULL) {
27
                printf("No memory\n");
28
                exit(1);
29
        }
30

    
31
        fd = open(disk, O_RDONLY);
32
        if (fd == -1) {
33
                perror("open");
34
                free(buf);
35
                exit(1);
36
        }
37
        if (lseek(fd, offset, SEEK_SET) < 0) {
38
                perror("lseek");
39
                close(fd);
40
                free(buf);
41
                exit(1);
42
        }
43
        bytes = read(fd, buf, requested_size);
44
        if (bytes == -1) perror("read");
45
        printf("asked %d, got %d, buf ptr = %p, pid %d\n", requested_size, bytes, buf, getpid());
46
        close(fd);
47
        free(buf);
48
        return(0);
49
}
50

    
    (1-1/1)