Skip to content

Instantly share code, notes, and snippets.

@johnbartholomew
Created December 21, 2013 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbartholomew/8064239 to your computer and use it in GitHub Desktop.
Save johnbartholomew/8064239 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static void check_access(const char *path) {
int ret = access(path, F_OK);
if (ret == -1) {
int e = errno;
printf("access '%s' failed: %s\n", path, strerror(e));
} else {
printf("access '%s' ok\n", path);
}
}
int main(void) {
check_access(""); /* outputs (for me): access '' failed: No such file or directory */
check_access("/"); /* outputs (for me): access '/' ok */
check_access("/root"); /* outputs (for me): access '/root' ok */
check_access("/root/."); /* outputs (for me): access '/root/.' failed: Permission denied */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment