File: test-read.c

package info (click to toggle)
lxcfs 6.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 716 kB
  • sloc: ansic: 10,144; sh: 757; makefile: 37; python: 32
file content (51 lines) | stat: -rw-r--r-- 1,016 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
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "config.h"

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

#define BUFSIZE 1025
char buf[BUFSIZE];

int read_count = 2;

int main(int argc, char *argv[]){
	if(argc < 3){
		fprintf(stderr, "usage: %s <file> <count> [buffer|direct]\n", argv[0]);
		exit(1);
	}
	char *file = argv[1];
	read_count = atoi(argv[2]);
	int ret = 0,sum = 0, i = 0, fd = -1;
	if(argc == 4 && strncmp(argv[3], "direct",6) == 0)
		fd = open(file, O_RDONLY|O_DIRECT);
	else
		fd = open(file, O_RDONLY);

	while(i++ < read_count){
		memset(buf, 0, BUFSIZE);
		ret = read(fd, buf, BUFSIZE-1);
		if(ret > 0){
			write(STDOUT_FILENO, buf, ret);
			sum += ret;
		}else if(ret == 0){
			printf("======read end======\n");
			break;
		}else{
			printf("error:%d\n", errno);
			break;
		}
		sleep(1);
	}
	printf("======read sum: %d======\n", sum);
	if (fd >= 0)
		close(fd);
	return 0;
}