Skip to content

Instantly share code, notes, and snippets.

@Alhadis
Last active October 22, 2021 11:41
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 Alhadis/546de1e86ab1f641fabc563d4360b5c6 to your computer and use it in GitHub Desktop.
Save Alhadis/546de1e86ab1f641fabc563d4360b5c6 to your computer and use it in GitHub Desktop.
Existence-Tests
/broken.lnk
/exists
.DS_Store
#include <stdio.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, const char *argv[]){
const char *path;
for(int i = 1; i < argc; ++i){
path = argv[i];
#if defined(faccessat) && defined(AT_FDCWD) && defined(AT_SYMLINK_NOFOLLOW)
if(faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW))
goto nope;
#else
int fd;
if(-1 == open(path, O_RDONLY | O_SYMLINK, &fd))
goto nope;
close(fd);
#endif
printf("%s exists\n", path);
}
return 0;
nope:
perror(argv[0]);
return 1;
}
#!/usr/bin/env node
/**
* @fileoverview fs_usage(1) output
*/
import {existsSync, accessSync, constants, openSync, closeSync} from "fs";
closeSync(openSync("broken.lnk", constants.O_RDONLY | constants.O_SYMLINK));
// open F=20 (R________S_X) broken.lnk
// close F=20
existsSync(process.argv[0]); // access (___F) /usr/local/Cellar/node/16.7.0/bin/node
existsSync("/tmp/naaaah"); // access [ 2] (___F) private/tmp/naaaah
existsSync("/tmp/a.css"); // access (___F) private/tmp/a.css
existsSync("broken.lnk"); // access [ 2] (___F) nowhere
accessSync("/tmp/a.css", constants.W_OK); // access (_W__) private/tmp/a.css
accessSync("/tmp/a.css", constants.R_OK); // access (R___) private/tmp/a.css
accessSync("/tmp/a.css", constants.F_OK); // access (___F) private/tmp/a.css
accessSync("/tmp/a.css", constants.X_OK); // access [ 13] (__X_) private/tmp/a.css
all: broken.lnk exists
broken.lnk:
ln -s nowhere $@
exists: exists.c
cc -o $@ $^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment