* Update to version 2.19.5
[alpine.git] / alpine / osdep / fltrname.c
blob400849b9e63db2797b25c15dbcf111f7339e6bee
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-2014 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 ALLOW_WEIRD 1
43 #ifdef ALLOW_WEIRD
44 static char illegal[] = {'\177', '\0'};
45 #else
46 #ifdef _WINDOWS
47 static char illegal[] = {'"', '#', '$', '%', '&', /*'/',*/ '(', ')','*',
48 ',', ';', '<', '=', '>', '?', '[', ']',
49 '^', '|', '\177', '\0'};
50 #else /* UNIX */
51 static char illegal[] = {'"', '#', '$', '%', '&', '\'','(', ')','*',
52 ',', ':', ';', '<', '=', '>', '?', '[', ']',
53 '\\', '^', '|', '\177', '\0'};
54 #endif /* UNIX */
55 #endif
56 static char error[100];
57 char ill_file[MAXPATH+1], *ill_char, *ptr, e2[20];
58 int i;
60 for(ptr = file; *ptr == ' '; ptr++) ; /* leading spaces gone */
62 while(*ptr && (unsigned char)(*ptr) >= ' ' && strchr(illegal, *ptr) == 0)
63 ptr++;
65 *fatal = TRUE;
66 if(*ptr != '\0') {
67 if(*ptr == '\n') {
68 ill_char = "<newline>";
69 } else if(*ptr == '\r') {
70 ill_char = "<carriage return>";
71 } else if(*ptr == '\t') {
72 ill_char = "<tab>";
73 *fatal = FALSE; /* just whitespace */
74 } else if(*ptr < ' ') {
75 snprintf(e2, sizeof(e2), "control-%c", *ptr + '@');
76 ill_char = e2;
77 } else if (*ptr == '\177') {
78 ill_char = "<del>";
79 } else {
80 e2[0] = *ptr;
81 e2[1] = '\0';
82 ill_char = e2;
83 *fatal = FALSE; /* less offensive? */
85 if(!*fatal){
86 strncpy(error, ill_char, sizeof(error)-1);
87 error[sizeof(error)-1] = '\0';
89 else if(ptr != file) {
90 strncpy(ill_file, file, MIN(ptr-file,sizeof(ill_file)-1));
91 ill_file[MIN(ptr-file,sizeof(ill_file)-1)] = '\0';
92 snprintf(error, sizeof(error),
93 "Character \"%s\" after \"%.*s\" not allowed in file name",
94 ill_char, sizeof(error)-50, ill_file);
95 } else {
96 snprintf(error, sizeof(error),
97 "First character, \"%s\", not allowed in file name",
98 ill_char);
101 return(error);
104 if((i=is_writable_dir(file)) == 0 || i == 1){
105 snprintf(error, sizeof(error), "\"%.*s\" is a directory", sizeof(error)-50, file);
106 return(error);
109 if(strict){
110 for(ptr = file; *ptr == ' '; ptr++) ; /* leading spaces gone */
112 if((ptr[0] == '.' && ptr[1] == '.') || filename_parent_ref(ptr)){
113 snprintf(error, sizeof(error), "\"..\" not allowed in filename");
114 return(error);
118 return((char *)NULL);