-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftok.c
More file actions
47 lines (39 loc) · 821 Bytes
/
ftok.c
File metadata and controls
47 lines (39 loc) · 821 Bytes
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
/**
* An implementation of ftok.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <fcntl.h>
#define KEY_FILE "/tmp/ftok_check.sysv"
#define PROJ 1
typedef int mkey_t ;
mkey_t mftok(const char *path, int id) {
struct stat sb;
mkey_t ret;
if (stat(path, &sb) == -1)
return -1;
ret = 0;
ret |= sb.st_ino & 0xFFFF;
ret |= (sb.st_dev & 0xFF) << 16;
ret |= (id & 0xFF) << 24;
return ret;
}
int main(int argc, char **argv) {
struct stat sb;
int fd;
mkey_t mres;
key_t res;
if (stat(KEY_FILE, &sb) == -1) {
fd = open(KEY_FILE, O_CREAT | O_RDWR, S_IRWXU);
close(fd);
}
res = ftok(KEY_FILE, PROJ);
mres = mftok(KEY_FILE, PROJ);
printf("Sys:\t%08lx\n", res);
printf("Mine:\t%08x\n", mres);
exit(EXIT_SUCCESS);
}