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
|
#define _GNU_SOURCE #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <stdarg.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/ipc.h>
void error(const char *file, int line,const char *str, ...) { va_list fmt; va_start(fmt, str); fprintf(stderr, "error:%s %s:%d\n", strerror(errno), file, line); vfprintf(stderr, str, fmt); fprintf(stderr, "\n"); va_end(fmt); }
#define ERROR_EXIT(ret, msg...) do { error(__FILE__, __LINE__, msg); ret } while(0)
#define ERROR(msg...) ERROR_EXIT(exit(1);, msg) #define FAIL(ret, msg...) ERROR_EXIT(ret, msg) #define PRINT_ERROR_STR(msg...) FAIL(;, msg)
key_t _ftok(const char *file, int proj) { struct statx filestat; int dirfd = open(".", O_RDONLY); if(statx(dirfd, file, 0, 0, &filestat) == -1) FAIL(return -1;, ""); close(dirfd); return ((proj&0xff) << 24) | ((filestat.stx_dev_minor & 0xff) << 16) | ((filestat.stx_ino & 0xffff)); }
int main(int argc, char **argv) { char *file = argv[1]; struct statx filestat; int dirfd = open(".", O_RDONLY); if(statx(dirfd, file, 0, 0, &filestat) == -1) ERROR(""); for(int proj = 1; proj <= 255; proj++) { printf("proj:%02X, dev:%02X, i-node:%04X, ", proj, filestat.stx_dev_minor & 0xff, filestat.stx_ino & 0xffff); printf("ftok:%08X\n", ftok(file, proj)); printf("_ftok:%08X\n", _ftok(file, proj)); } close(dirfd); }
|