-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlseek_test.c
74 lines (58 loc) · 1.69 KB
/
lseek_test.c
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
72
73
74
/**
* @Author: Izhar Shaikh <izhar>
* @Date: 2017-03-21T14:54:14-04:00
* @Email: [email protected]
* @Filename: ltest.c
* @Last modified by: izhar
* @Last modified time: 2017-03-21T15:20:55-04:00
* @License: MIT
*/
/*
* Keeping track of file position. (Testing application)
@*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
int length = 20, position = 0, fd, rc;
char *message, *nodename = "/dev/mycdrv0";
if(argc == 4) {
nodename = argv[1];
position = atoi(argv[2]);
length = atoi(argv[3]);
}
else {
printf("USAGE:\n\t %s <device-node-name> <position-to-seek> <message-length>\n", argv[0]);
return 0;
}
/* set up the message */
message = malloc(length);
memset(message, 'x', length/2);
memset(message + (length/2), 'y', length/2);
message[length - 1] = '\0'; /* make sure it is null terminated */
/* open the device node */
fd = open(nodename, O_RDWR);
printf(" I opened the device node, file descriptor = %d\n", fd);
/* seek to position */
rc = lseek(fd, position, SEEK_SET);
printf("return code from lseek = %d\n", rc);
/* write to the device node twice */
rc = write(fd, message, length);
printf("return code from write = %d\n", rc);
//rc = write(fd, message, length);
//printf("return code from write = %d\n", rc);
/* reset the message to null */
memset(message, 0, length);
/* seek to position */
rc = lseek(fd, position, SEEK_SET);
printf("return code from lseek = %d\n", rc);
/* read from the device node */
rc = read(fd, message, length);
printf("return code from read = %d\n", rc);
printf("the message read [len: %d]: %s\n", rc, message);
close(fd);
exit(0);
}