aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2021-05-07 10:35:28 -0400
committerGitHub <noreply@github.com>2021-05-07 10:35:28 -0400
commitb072d93e47949c9729c979fceef8a7c7bfd22d39 (patch)
tree96172b7aa41d6e0b4a44a4d8e67e7bcfd15511ef
parent003c7d1b27140d42c026c25fbca1cef545bfb5c7 (diff)
downloadcleanpath-b072d93e47949c9729c979fceef8a7c7bfd22d39.tar.gz
Improvements (#4)
* Move display code into functions * Mark windows supported * Guard against overruns * Add --list argument to docs
-rw-r--r--README.md1
-rw-r--r--docs/man/cleanpath.13
-rw-r--r--include/cleanpath.h3
-rw-r--r--lib/cleanpath.c6
-rw-r--r--src/main.c47
5 files changed, 50 insertions, 10 deletions
diff --git a/README.md b/README.md
index 3ee681a..2950971 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ $ make install
usage: cleanpath [-hVelrsv] [pattern ...]
--help -h Displays this help message
--version -V Displays the program's version
+ --list Format output as a list
--exact -e Filter when pattern is an exact match (default)
--loose -l Filter when any part of the pattern matches
--regex -r Filter matches with (Extended) Regular Expressions
diff --git a/docs/man/cleanpath.1 b/docs/man/cleanpath.1
index 84479c7..89f6fe0 100644
--- a/docs/man/cleanpath.1
+++ b/docs/man/cleanpath.1
@@ -25,6 +25,9 @@ Prints brief usage information.
\f[B]-V\f[R], \f[B]--version\f[R]
Prints the current version number.
.TP
+\f[B]--list\f[R]
+Format output as a list
+.TP
\f[B]-e\f[R], \f[B]--exact\f[R]
Filter when pattern is an exact match (default)
.TP
diff --git a/include/cleanpath.h b/include/cleanpath.h
index b2d21f4..9f38188 100644
--- a/include/cleanpath.h
+++ b/include/cleanpath.h
@@ -16,6 +16,8 @@
#elif defined(_WIN32)
# undef OS_WINDOWS
# define OS_WINDOWS 1
+# undef OS_SUPPORTED
+# define OS_SUPPORTED 1
#elif defined(__linux) || defined(__linux__)
# undef OS_LINUX
@@ -24,6 +26,7 @@
# define OS_SUPPORTED 1
#endif
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/lib/cleanpath.c b/lib/cleanpath.c
index 2337869..fdb615d 100644
--- a/lib/cleanpath.c
+++ b/lib/cleanpath.c
@@ -43,7 +43,7 @@ struct CleanPath *cleanpath_init(const char *path, const char *sep) {
// Split string by separator
elem = strtok(result->data, sep);
- for (i = 0; elem != NULL; i++) {
+ for (i = 0; i < CLEANPATH_PART_MAX && elem != NULL; i++) {
result->part[i] = elem;
elem = strtok(NULL, sep);
}
@@ -67,7 +67,7 @@ void cleanpath_filter(struct CleanPath *path, unsigned mode, const char *pattern
#endif
int match;
- for (size_t i = 0; path->part[i]; i++) {
+ for (size_t i = 0; i < CLEANPATH_PART_MAX && path->part[i]; i++) {
match = 0;
switch(mode) {
case CLEANPATH_FILTER_LOOSE:
@@ -109,7 +109,7 @@ char *cleanpath_read(struct CleanPath *path) {
goto cleanpath_read__failure;
}
- for (size_t i = 0; path->part[i] != NULL; i++) {
+ for (size_t i = 0; i < CLEANPATH_PART_MAX && path->part[i] != NULL; i++) {
// Ignore filtered paths
if (*path->part[i] == '\0') {
continue;
diff --git a/src/main.c b/src/main.c
index 7bb92c3..ec77f78 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,7 +10,7 @@
// Global program path
static char *program;
-static int is_valid_arg(char **args, char *s) {
+int is_valid_arg(char **args, char *s) {
int match;
match = 0;
@@ -29,8 +29,7 @@ static int is_valid_arg(char **args, char *s) {
return match;
}
-
-static char *getenv_ex(char *s) {
+char *getenv_ex(char *s) {
char *env_var;
env_var = getenv(s);
if (!env_var || !strlen(env_var)) {
@@ -39,6 +38,31 @@ static char *getenv_ex(char *s) {
return env_var;
}
+void show_listing(struct CleanPath *path) {
+ if (path == NULL) {
+ return;
+ }
+
+ for (size_t i = 0; i < CLEANPATH_PART_MAX && path->part[i] != NULL; i++) {
+ if (*path->part[i] == '\0') {
+ continue;
+ }
+ puts(path->part[i]);
+ }
+}
+
+void show_path(struct CleanPath *path) {
+ char *result;
+
+ result = cleanpath_read(path);
+ if (result == NULL) {
+ return;
+ }
+
+ printf("%s\n", result);
+ free(result);
+}
+
static void show_version() {
puts(CLEANPATH_VERSION);
}
@@ -50,6 +74,7 @@ static void show_usage() {
printf("usage: %s [-hVelrsv] [pattern ...]\n", bname ? bname + 1 : program);
printf(" --help -h Displays this help message\n");
printf(" --version -V Displays the program's version\n");
+ printf(" --list Format output as a list\n");
printf(" --exact -e Filter when pattern is an exact match (default)\n");
printf(" --loose -l Filter when any part of the pattern matches\n");
printf(" --regex -r Filter matches with (Extended) Regular Expressions " CLEANPATH_MSG_NOAVAIL "\n");
@@ -63,6 +88,7 @@ int main(int argc, char *argv[]) {
char *sys_var;
char *result;
size_t pattern_nelem;
+ int do_listing;
int filter_mode;
int args_invalid;
char *pattern[CLEANPATH_PART_MAX];
@@ -71,6 +97,7 @@ int main(int argc, char *argv[]) {
result = NULL;
sys_var = NULL;
sep = CLEANPATH_SEP;
+ do_listing = 0;
filter_mode = CLEANPATH_FILTER_NONE;
memset(pattern, 0, sizeof(pattern) / sizeof(char *));
pattern_nelem = 0;
@@ -79,6 +106,7 @@ int main(int argc, char *argv[]) {
char *args_valid[] = {
"--help", "-h",
"--version", "-V",
+ "--list",
"--exact", "-e",
"--loose", "-l",
"--regex", "-r",
@@ -104,6 +132,9 @@ int main(int argc, char *argv[]) {
show_version();
exit(0);
}
+ if (ARGM("--list")) {
+ do_listing = 1;
+ }
if (ARGM("--exact") || ARGM("-e")) {
filter_mode = CLEANPATH_FILTER_EXACT;
continue;
@@ -151,16 +182,18 @@ int main(int argc, char *argv[]) {
}
// Remove patterns from sys_var
- for (size_t i = 0; pattern[i] != NULL; i++) {
+ for (size_t i = 0; i < CLEANPATH_PART_MAX && pattern[i] != NULL; i++) {
cleanpath_filter(path, filter_mode, pattern[i]);
}
// Print filtered result
- result = cleanpath_read(path);
- printf("%s\n", result);
+ if (do_listing) {
+ show_listing(path);
+ } else {
+ show_path(path);
+ }
// Clean up
- free(result);
cleanpath_free(path);
return 0;