Skip to content

Instantly share code, notes, and snippets.

@nadiaholmquist
Created June 6, 2021 10:40
Show Gist options
  • Save nadiaholmquist/8422b0b85de9e7d80769e6b8b162a857 to your computer and use it in GitHub Desktop.
Save nadiaholmquist/8422b0b85de9e7d80769e6b8b162a857 to your computer and use it in GitHub Desktop.
Little program to print the flags set on a file by name on macOS.
BEGIN {
print "#include <sys/stat.h>\n"
print "struct { unsigned flag; const char* name; } flag_names[] = {"
}
/^#define [SU]F_/ && !/[SU]F_SETTABLE/ {
print "\t" $2 ", \"" tolower(substr($2,4)) "\","
}
END {
print "\t0, 0"
print "};"
}
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include "flag_names.h"
int main(int argc, char** argv) {
struct stat file_stat;
int nth = 0;
if (argc < 2 || argc > 3) {
fprintf(stderr, "Usage: %s [-l] <filename>\n", argv[0]);
fprintf(stderr, " -l If filename is a symlink, print info about the link itself\n");
return 1;
}
if (strncmp(argv[1], "-l", 2) == 0) {
lstat(argv[2], &file_stat);
} else {
stat(argv[1], &file_stat);
}
if (errno != 0) {
perror(argv[1]);
return errno;
}
for (int i = 0; flag_names[i].flag != 0; i++) {
if (file_stat.st_flags & flag_names[i].flag) {
if (nth != 0) {
printf(", ");
}
printf("%s", flag_names[i].name);
nth++;
}
}
if (nth > 0) {
putchar('\n');
}
}
OUT := getflags
SDK := $(shell xcrun --show-sdk-path)
all: $(OUT)
clean:
rm flag_names.h $(OUT)
flag_names.h: flag_names.awk
awk -f $< "$(SDK)/usr/include/sys/stat.h" > $@
$(OUT): getflags.c flag_names.h
$(CC) -o $(OUT) getflags.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment