commit: 540e0acfd384afc753a20c8641d68c4a0f16a806
parent 73023c7944d5064569507fea039818efb1c5b705
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 24 Sep 2025 08:59:39 +0200
Add support for -d and -e options
Diffstat:
M | checksrc.c | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- |
1 file changed, 63 insertions(+), 4 deletions(-)
diff --git a/checksrc.c b/checksrc.c
@@ -5,6 +5,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
+#include <fnmatch.h>
#include <limits.h> // PATH_MAX
#include <stdbool.h>
#include <stdint.h> // uint8_t
@@ -36,6 +37,11 @@ static const char *generated[] = {
bool verbose = false;
+#define EXCLUDE_MAX 400
+#define EXCLUDE_MAX_STR "400"
+static const char *exclude[400];
+static int exclude_len;
+
static int
checkfile(const char *fname)
{
@@ -182,6 +188,15 @@ checkfile(const char *fname)
}
static int
+exclude_match(char *path)
+{
+ for(int ei = 0; ei < exclude_len; ei++)
+ if(fnmatch(exclude[ei], path, 0) == 0) return 0;
+
+ return 1;
+}
+
+static int
checkdir(const char *dirname)
{
int ret = 0;
@@ -230,6 +245,8 @@ checkdir(const char *dirname)
return 1;
}
+ if(exclude_match(path) == 0) continue;
+
int err = 0;
switch(dent->d_type)
{
@@ -239,9 +256,6 @@ checkdir(const char *dirname)
case DT_REG:
err = checkfile(path);
break;
- default:
- fprintf(stderr, "checksrc: warning: Unknown dirent type %d\n", dent->d_type);
- continue;
}
if(err < 0) return 1;
if(err > 0) ret = 1 + err;
@@ -254,21 +268,66 @@ checkdir(const char *dirname)
return ret;
}
+static void
+usage(void)
+{
+ fputs("Usage: checksrc [-v] [-d workdir] [-e excluded_path ...]\n", stderr);
+}
+
int
main(int argc, char *argv[])
{
- for(int c = -1; (c = getopt(argc, argv, "v")) != -1;)
+ char *workdir = NULL;
+
+ for(int c = -1; (c = getopt(argc, argv, "vd:e:")) != -1;)
{
switch(c)
{
case 'v':
verbose = true;
break;
+ case 'd':
+ if(workdir)
+ {
+ fputs("checksrc: error: Option -d can only be passed once\n", stderr);
+ usage();
+ return 1;
+ }
+
+ workdir = optarg;
+ break;
+ case 'e':
+ if(exclude_len == EXCLUDE_MAX)
+ {
+ fputs("checksrc: error: Can only use '-e' option " EXCLUDE_MAX_STR " times\n", stderr);
+ return 1;
+ }
+
+ exclude[exclude_len++] = optarg;
+ break;
}
}
argc -= optind;
argv += optind;
+ if(argc > 0)
+ {
+ fprintf(stderr, "checksrc: error: Expected 0 arguments, got %d\n", argc);
+ return 1;
+ }
+
+ if(workdir)
+ {
+ if(chdir(workdir) != 0)
+ {
+ fprintf(stderr,
+ "checksrc: error: Failed changing into directory '%s': %s\n",
+ workdir,
+ strerror(errno));
+ return 1;
+ }
+ }
+
return checkdir(".");
}