* Deactivate some color code from Pico (as standalone editor) until
[alpine.git] / alpine / osdep / fltrname.c
blob419f9792b2982f375c11037ae2bad0921eca4db3
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: fltrname.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2017 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../c-client/mail.h" /* for MAILSTREAM */
20 #undef ERROR
22 #include <system.h>
23 #include <general.h>
25 #include "../../pith/osdep/writ_dir.h"
26 #include "../../pith/osdep/bldpath.h"
28 #include "fltrname.h"
31 /*----------------------------------------------------------------------
32 Filter file names for strange characters
34 Args: file -- the file name to check
36 Result: Returns NULL if file name is OK
37 Returns formatted error message if it is not
38 ----*/
39 char *
40 filter_filename(char *file, int *fatal, int strict)
42 #define ERRORLEN 100
43 #define ALLOW_WEIRD 1
44 #ifdef ALLOW_WEIRD
45 static char illegal[] = {'\177', '\0'};
46 #else
47 #ifdef _WINDOWS
48 static char illegal[] = {'"', '#', '$', '%', '&', /*'/',*/ '(', ')','*',
49 ',', ';', '<', '=', '>', '?', '[', ']',
50 '^', '|', '\177', '\0'};
51 #else /* UNIX */
52 static char illegal[] = {'"', '#', '$', '%', '&', '\'','(', ')','*',
53 ',', ':', ';', '<', '=', '>', '?', '[', ']',
54 '\\', '^', '|', '\177', '\0'};
55 #endif /* UNIX */
56 #endif
57 static char error[ERRORLEN];
58 char ill_file[MAXPATH+1], *ill_char, *ptr, e2[20];
59 int i;
61 for(ptr = file; *ptr == ' '; ptr++) ; /* leading spaces gone */
63 while(*ptr && (unsigned char)(*ptr) >= ' ' && strchr(illegal, *ptr) == 0)
64 ptr++;
66 *fatal = TRUE;
67 if(*ptr != '\0') {
68 if(*ptr == '\n') {
69 ill_char = "<newline>";
70 } else if(*ptr == '\r') {
71 ill_char = "<carriage return>";
72 } else if(*ptr == '\t') {
73 ill_char = "<tab>";
74 *fatal = FALSE; /* just whitespace */
75 } else if(*ptr < ' ') {
76 snprintf(e2, sizeof(e2), "control-%c", *ptr + '@');
77 ill_char = e2;
78 } else if (*ptr == '\177') {
79 ill_char = "<del>";
80 } else {
81 e2[0] = *ptr;
82 e2[1] = '\0';
83 ill_char = e2;
84 *fatal = FALSE; /* less offensive? */
86 if(!*fatal){
87 strncpy(error, ill_char, sizeof(error)-1);
88 error[sizeof(error)-1] = '\0';
90 else if(ptr != file) {
91 strncpy(ill_file, file, MIN(ptr-file,sizeof(ill_file)-1));
92 ill_file[MIN(ptr-file,sizeof(ill_file)-1)] = '\0';
93 snprintf(error, sizeof(error),
94 "Character \"%s\" after \"%.*s\" not allowed in file name",
95 ill_char, ERRORLEN-50, ill_file);
96 } else {
97 snprintf(error, sizeof(error),
98 "First character, \"%s\", not allowed in file name",
99 ill_char);
102 return(error);
105 if((i=is_writable_dir(file)) == 0 || i == 1){
106 snprintf(error, sizeof(error), "\"%.*s\" is a directory", ERRORLEN-50, file);
107 return(error);
110 if(strict){
111 for(ptr = file; *ptr == ' '; ptr++) ; /* leading spaces gone */
113 if((ptr[0] == '.' && ptr[1] == '.') || filename_parent_ref(ptr)){
114 snprintf(error, sizeof(error), "\"..\" not allowed in filename");
115 return(error);
119 return((char *)NULL);