Commit Diff


commit - 9b3ec9e19fea764a9b266a5270970aa63ca9124a
commit + 1b9dd9ac4c4715318b8f83bdf0fe20c1081cff30
blob - 0e1461a79a41373f0c82294564f26e354c5a2476
blob + ae5e1fa0070fc33678a3355475af45c448e8b50c
--- FICS/utils.c
+++ FICS/utils.c
@@ -49,9 +49,11 @@
 #include "stdinclude.h"
 #include "common.h"
 
+#include <ctype.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <string.h>
 
 #include "config.h"
 #include "network.h"
@@ -78,6 +80,28 @@ struct t_dirs {
 PRIVATE char**	t_buffer = NULL;
 PRIVATE int	t_buffersize = 0;
 
+PUBLIC bool
+is_valid_filename(const char *name, const bool allow_hidden)
+{
+	if (name == NULL || strcmp(name, "") == 0)
+		return false;
+	if (!allow_hidden && name[0] == '.')
+		return false;
+	if (strstr(name, "..") || strchr(name, '/') || strchr(name, '\\') ||
+	    name[0] == '/')
+		return false;
+	for (const char *p = name; *p; ++p) {
+		if (isspace((unsigned char)*p) || iscntrl((unsigned char)*p))
+			return false;
+	}
+	for (const char *p = name; *p; ++p) {
+		if (!isalnum((unsigned char)*p) && *p != '-' && *p != '_' &&
+		    *p != '.')
+			return false;
+	}
+	return true;
+}
+
 PUBLIC int
 count_lines(FILE *fp)
 {
blob - 6f60dbf250e8ec89c3ab3ac202daf1d91c527129
blob + b35527b7c9dffa92d2a49dedf413675c39278d78
--- FICS/utils.h
+++ FICS/utils.h
@@ -31,6 +31,7 @@
 #ifndef _UTILS_H
 #define _UTILS_H
 
+#include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 
@@ -58,6 +59,7 @@
 #define CheckFlag(VAR, FLAG)	(VAR & (FLAG))
 
 __FICS_BEGIN_DECLS
+extern bool		 is_valid_filename(const char *, const bool);
 extern char		*dotQuad(unsigned int);
 extern char		*eattailwhite(char *);
 extern char		*eatwhite(char *);