Created
March 18, 2021 12:28
-
-
Save kostikbel/35e8084c99fcc9a2af0b1ae79dd845d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* $Id: o_path.c,v 1.4 2021/03/18 12:01:23 kostik Exp $ */ | |
#include <sys/fcntl.h> | |
#include <sys/stat.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#ifdef __FreeBSD__ | |
#ifndef O_PATH | |
#define O_PATH 0x00400000 /* fd is only a path */ | |
#endif | |
#endif | |
static void | |
opath(const char *dirname, const char *filename) | |
{ | |
struct stat st; | |
int dirfd, fd, error; | |
char c[1]; | |
dirfd = open(dirname, O_PATH | O_RDONLY); | |
if (dirfd == -1) { | |
fprintf(stderr, "Cannot open(\"%s\", O_PATH): %d %s", | |
dirname, errno, strerror(errno)); | |
return; | |
} | |
if (fstat(dirfd, &st) == -1) { | |
fprintf(stderr, "Cannot fstat(\"%s\", O_PATH): %d %s", | |
dirname, errno, strerror(errno)); | |
return; | |
} | |
error = read(dirfd, c, sizeof(c)); | |
if (error >= 0) { | |
fprintf(stderr, "read dirfd returned %d\n", error); | |
} else { | |
fprintf(stderr, "read dirfd error %d %s\n", | |
errno, strerror(errno)); | |
} | |
fd = openat(dirfd, filename, O_RDONLY); | |
if (fd == -1) { | |
fprintf(stderr, "Cannot open(\"%s\"): %d %s", | |
filename, errno, strerror(errno)); | |
close(dirfd); | |
return; | |
} | |
if (close(dirfd) == -1) { | |
fprintf(stderr, "Cannot close(\"%s\"): %d %s", | |
dirname, errno, strerror(errno)); | |
} | |
if (close(fd) == -1) { | |
fprintf(stderr, "Cannot close(\"%s\"): %d %s", | |
filename, errno, strerror(errno)); | |
} | |
} | |
int | |
main(int argc, char *argv[]) | |
{ | |
for (int i = 1; i < argc; i += 2) { | |
opath(argv[i], argv[i + 1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment