summaryrefslogtreecommitdiff
path: root/test/stat/stat.c
blob: c9e063f27a7443645b650290830c42fa11d2308f (plain)
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
71
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

void print_struct_stat(char *msg, struct stat *s)
{
    printf("%s\n", msg);
    /* The casts are because glibc thinks it's cool */
    printf("device    : 0x%llx\n",(long long)s->st_dev);
    printf("inode     : %lld\n",  (long long)s->st_ino);
    printf("mode      : 0x%llx\n",(long long)s->st_mode);
    printf("nlink     : %lld\n",  (long long)s->st_nlink);
    printf("uid       : %lld\n",  (long long)s->st_uid);
    printf("gid       : %lld\n",  (long long)s->st_gid);
    printf("rdev      : 0x%llx\n",(long long)s->st_rdev);
    printf("size      : %lld\n",  (long long)s->st_size);
    printf("blksize   : %lld\n",  (long long)s->st_blksize);
    printf("blocks    : %lld\n",  (long long)s->st_blocks);
    printf("atime     : %lld\n",  (long long)s->st_atime);
    printf("mtime     : %lld\n",  (long long)s->st_mtime);
    printf("ctime     : %lld\n",  (long long)s->st_ctime);
}

int main(int argc,char **argv)
{
    int fd, ret;
    char *file;
    struct stat s;

    if (argc < 2) {
	fprintf(stderr, "Usage: stat FILE\n");
	exit(1);
    }
    file = argv[1];

    memset(&s, 0, sizeof(struct stat));
    ret = stat(file, &s);
    if(ret<0){
	perror("stat");
	exit(1);
    }
    print_struct_stat("\nTesting stat:", &s);

    memset(&s, 0, sizeof(struct stat));
    ret = lstat(file, &s);
    if(ret<0){
	perror("lstat");
	exit(1);
    }
    print_struct_stat("\nTesting lstat:", &s);


    fd = open(file, O_RDONLY);
    if(fd<0){
	perror("open");
	exit(1);
    }
    memset(&s, 0, sizeof(struct stat));
    ret = fstat(fd,&s);
    if(ret<0){
	perror("fstat");
	exit(1);
    }
    print_struct_stat("\nTesting fstat:", &s);

    exit(0);
}