Project

General

Profile

Bug #3040 ยป teststat4.c

test_stat - just call sys_stat in loop - Vitaliy Gusev, 2012-07-31 12:32 PM

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

    
9
#define TESTFILE "file"
10

    
11
int interrupted;
12

    
13
void sighandler(int sig)
14
{
15
	interrupted = 1;
16
}
17

    
18
int main(int argc, char *argv[])
19
{
20
	int fd, first = 1, error = 0;
21
	const char *sfile = (argc >= 2) ? argv[1] : TESTFILE;
22
	struct stat st;
23

    
24
	fd = open(sfile, O_RDONLY);
25
	if (fd < 0)
26
		err(1, "can't open");
27

    
28
	printf("opened %s\n", sfile);
29
	signal(SIGINT, &sighandler);
30

    
31
	while(!interrupted || error == 0) {
32
		error = fstat(fd, &st);
33
		if (first) {
34
			first = 0;
35
			printf("uid %x gid %x\n", st.st_uid, st.st_gid);
36
		}
37
	}
38
	if (interrupted)
39
		printf("interrupted. exit\n");
40
	else
41
		perror("error during stat");
42

    
43
	close(fd);
44
	return (error);
45
}
    (1-1/1)