@@ -632,94 +632,79 @@ is_range(const char *str)
632
632
return 0 ;
633
633
}
634
634
635
+ static int
636
+ print_val_err (const char * name , const int msg_type )
637
+ {
638
+ printf ("%s: %s\n" , name , unsafe_name_msgs [msg_type ]);
639
+ return 0 ;
640
+ }
641
+
635
642
/* Return 1 if NAME is a safe filename, or 0 if not.
636
643
* See https://dwheeler.com/essays/fixing-unix-linux-filenames.html */
637
644
static int
638
- is_valid_filename (char * name )
645
+ is_valid_filename (const char * name )
639
646
{
640
- char * n = name ;
647
+ const char * n = name ;
641
648
/* Trailing spaces were already removed (by get_newname()) */
642
649
643
650
/* Starting with dash */
644
- if (* n == '-' ) {
645
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_DASH ]);
646
- return 0 ;
647
- }
651
+ if (* n == '-' )
652
+ return print_val_err (name , UNSAFE_DASH );
648
653
649
654
/* Reserved keyword (internal: MIME type and file type expansions) */
650
- if ((* n == '=' && n [1 ] >= 'a' && n [1 ] <= 'z' && !n [2 ]) || * n == '@' ) {
651
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_MIME ]);
652
- return 0 ;
653
- }
655
+ if ((* n == '=' && n [1 ] >= 'a' && n [1 ] <= 'z' && !n [2 ]) || * n == '@' )
656
+ return print_val_err (name , UNSAFE_MIME );
654
657
655
658
/* Reserved keyword (internal: bookmarks, tags, and selected files
656
659
* constructs) */
657
660
if (((* n == 'b' || * n == 's' ) && n [1 ] == ':' )
658
- || strcmp (n , "sel" ) == 0 ) {
659
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_BTS_CONST ]);
660
- return 0 ;
661
- }
661
+ || strcmp (n , "sel" ) == 0 )
662
+ return print_val_err (name , UNSAFE_BTS_CONST );
662
663
663
- if (* n == 't' && n [1 ] == ':' && n [2 ]) {
664
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_BTS_CONST ]);
665
- return 0 ;
666
- }
664
+ if ((* n == 't' || * n == 'w' ) && n [1 ] == ':' && n [2 ])
665
+ return print_val_err (name , UNSAFE_BTS_CONST );
667
666
668
667
/* Reserved (internal: ELN/range expansion) */
669
- if ((* n > '0' && is_number (n )) || is_range (n )) {
670
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_ELN ]);
671
- return 0 ;
672
- }
668
+ if ((* n > '0' && is_number (n )) || is_range (n ))
669
+ return print_val_err (name , UNSAFE_ELN );
673
670
674
671
/* "~" or ".": Reserved keyword */
675
- if ((* n == '~' || * n == '.' ) && !n [1 ]) {
676
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_SYS_KEY ]);
677
- return 0 ;
678
- }
672
+ if ((* n == '~' || * n == '.' ) && !n [1 ])
673
+ return print_val_err (name , UNSAFE_SYS_KEY );
679
674
680
675
/* ".." or "./": Reserved keyword */
681
- if (* n == '.' && (n [1 ] == '.' || n [1 ] == '/' ) && !n [2 ]) {
682
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_SYS_KEY ]);
683
- return 0 ;
684
- }
676
+ if (* n == '.' && (n [1 ] == '.' || n [1 ] == '/' ) && !n [2 ])
677
+ return print_val_err (name , UNSAFE_SYS_KEY );
685
678
686
679
int only_dots = 1 ;
687
- char * s = name ;
680
+ const char * s = name ;
688
681
while (* s ) {
689
682
/* Contains control characters (being not UTF-8 leading nor
690
683
* continuation bytes) */
691
- if (* s < ' ' && (* s & 0xC0 ) != 0xC0 && (* s & 0xC0 ) != 0x80 ) {
692
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_CONTROL ]);
693
- return 0 ;
694
- }
684
+ if (* s < ' ' && !IS_UTF8_CHAR (* s ))
685
+ return print_val_err (name , UNSAFE_CONTROL );
686
+
695
687
/* Contains shell meta-characters */
696
- if (strchr ("*?[]<>|(){}&=`^!\\;$" , * s )) {
697
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_META ]);
698
- return 0 ;
699
- }
688
+ if (strchr ("*?[]<>|(){}&=`^!\\;$" , * s ))
689
+ return print_val_err (name , UNSAFE_META );
690
+
700
691
/* Only dots: Reserved keyword (internal: fastback expansion) */
701
692
if (* s != '.' )
702
693
only_dots = 0 ;
703
694
704
695
s ++ ;
705
696
}
706
697
707
- if (only_dots == 1 ) {
708
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_FASTBACK ]);
709
- return 0 ;
710
- }
698
+ if (only_dots == 1 )
699
+ return print_val_err (name , UNSAFE_FASTBACK );
711
700
712
701
/* Name too long */
713
- if (s - name >= NAME_MAX ) {
714
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_TOO_LONG ]);
715
- return 0 ;
716
- }
702
+ if (s - name >= NAME_MAX )
703
+ return print_val_err (name , UNSAFE_TOO_LONG );
717
704
718
705
#ifdef _BE_POSIX
719
- if (is_portable_filename (name , (size_t )(s - name )) != FUNC_SUCCESS ) {
720
- printf ("%s: %s\n" , name , unsafe_name_msgs [UNSAFE_NOT_PORTABLE ]);
721
- return 0 ;
722
- }
706
+ if (is_portable_filename (name , (size_t )(s - name )) != FUNC_SUCCESS )
707
+ return print_val_err (name , UNSAFE_NOT_PORTABLE );
723
708
#endif /* _BE_POSIX */
724
709
725
710
return 1 ;
0 commit comments