Skip to content

Instantly share code, notes, and snippets.

@kostikbel
Created November 8, 2018 20:24
Show Gist options
  • Save kostikbel/7e4f88d662e5c1e79aaaae3dfba2cf15 to your computer and use it in GitHub Desktop.
Save kostikbel/7e4f88d662e5c1e79aaaae3dfba2cf15 to your computer and use it in GitHub Desktop.
/* $Id: beneath.c,v 1.1 2018/10/13 16:53:02 kostik Exp kostik $ */
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifndef O_BENEATH
#define O_BENEATH 0x00400000 /* Fail if not under cwd */
#define AT_BENEATH 0x1000 /* Fail if not under dirfd */
#endif
int
main(int argc, char *argv[])
{
struct stat st;
char *name;
int error, fd, i;
for (i = 1; i < argc; i++) {
name = argv[i];
fd = open(name, O_RDONLY | O_BENEATH);
if (fd == -1) {
fprintf(stderr, "open(\"%s\") failed, error %d %s\n",
name, errno, strerror(errno));
} else {
fprintf(stderr, "open(\"%s\") succeeded\n", name);
close(fd);
}
error = fstatat(AT_FDCWD, name, &st, AT_BENEATH);
if (error == -1){
fprintf(stderr, "stat(\"%s\") failed, error %d %s\n",
name, errno, strerror(errno));
} else {
fprintf(stderr, "stat(\"%s\") succeeded\n", name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment