summaryrefslogtreecommitdiff
path: root/not.c
blob: 5d1170fbcb26fe8534580f6c83ada4a089331abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#define _POSIX_C_SOURCE 2
#include <dirent.h>
#include <fnmatch.h>
#include <locale.h>
#include <stdio.h>
#include <unistd.h>

static int matches(char *fn, int argc, char *argv[])
{
	for (int i = 0; i < argc; i++) {
		if (fnmatch(argv[i], fn, 0) == 0) {
			return 1;
		}
	}
	return 0;
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL, "");
	int c;
	while ((c = getopt(argc, argv, "")) != -1) {
		switch (c) {
		default:
			return 1;
		}
	}

	if (argc <= optind) {
		fprintf(stderr, "not: missing operands\n");
		return 1;
	}

	DIR *d = opendir(".");
	if (d == NULL) {
		perror("not");
		return 1;
	}

	struct dirent *de;
	while ((de = readdir(d)) != NULL) {
		if (!matches(de->d_name, argc - optind, argv + optind)) {
			printf("%s\n", de->d_name);
		}
	}

	return 0;
}