File: testread.c

package info (click to toggle)
avfs 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,916 kB
  • sloc: ansic: 31,364; sh: 6,482; perl: 1,916; makefile: 351
file content (70 lines) | stat: -rw-r--r-- 1,756 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* this is a very simple test tool for reading any file in an archive, just give the name as argument.
 * This tool is not meant to be correct or a good example, it is pretty much only useful for debugging
 * avfs.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <virtual.h>
#include <errno.h>
#include <string.h>

int main( int argc, char **argv )
{
    int fd;
    ssize_t len;
    char buf[128*1024];

    if ( argc < 2 ) {
        return 0;
    }

    struct stat stat_buf;

    if ( virt_stat( argv[1], &stat_buf ) != 0 ) {
        printf( "Could not stat %s (%s)\n", argv[1], strerror( errno ) );
    } else {
        printf( "Size: %lu\n", stat_buf.st_size );
    }
  
    fd = virt_open( argv[1], O_RDONLY, 0 );
    if ( fd >= 0 ) {
        ssize_t total_len = 0;

        for (;;) {
            len = virt_read( fd, buf, sizeof( buf ) );
            total_len += len;

            printf( "Bytes read: %lu\n", len );

            if ( len == 0 ) break;
            else if ( len < 0 ) {
                printf( "Error: %s\n", strerror( errno ) );
                break;
            }
        }

        printf( "Total len:%lu\n", total_len );

        if ( total_len >= sizeof( buf ) ) {
            for (;;) {
                total_len -= sizeof( buf );
                
                virt_lseek( fd, total_len, 0 );
                len = virt_read( fd, buf, sizeof( buf ) );

                printf( "Bytes read by seeking to %lu: %lu\n", total_len, len );

                if ( total_len < sizeof( buf ) ) break;
            }
        }

        virt_close( fd );
    } else {
        printf( "Could not open %s (%s)\n", argv[1], strerror( errno ) );
    }
    return 0;
}