1 /* addext.c -- add an extension to a file name
2    Copyright (C) 1990, 1997-1999, 2001-2003, 2005-2006, 2020 Free Software
3    Foundation, Inc.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17 
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Paul Eggert */
19 
20 #include <config.h>
21 
22 #ifndef HAVE_DOS_FILE_NAMES
23 # define HAVE_DOS_FILE_NAMES 0
24 #endif
25 #ifndef HAVE_LONG_FILE_NAMES
26 # define HAVE_LONG_FILE_NAMES 0
27 #endif
28 
29 #include "backupfile.h"
30 
31 #include <limits.h>
32 #ifndef _POSIX_NAME_MAX
33 # define _POSIX_NAME_MAX 14
34 #endif
35 
36 #include <sys/types.h>
37 #if HAVE_STRING_H
38 # include <string.h>
39 #else
40 # include <strings.h>
41 #endif
42 
43 #include <unistd.h>
44 
45 #include "basename-lgpl.h"
46 
47 /* Append to FILENAME the extension EXT, unless the result would be too long,
48    in which case just append the character E.  */
49 
50 void
addext(char * filename,char const * ext,char e)51 addext (char *filename, char const *ext, char e)
52 {
53   char *s = last_component (filename);
54   size_t slen = strlen (s), extlen = strlen (ext);
55   long slen_max = -1;
56 
57 #if HAVE_PATHCONF && defined _PC_NAME_MAX
58   if (slen + extlen <= _POSIX_NAME_MAX && ! HAVE_DOS_FILE_NAMES)
59     /* The file name is so short there's no need to call pathconf.  */
60     slen_max = _POSIX_NAME_MAX;
61   else if (s == filename)
62     slen_max = pathconf (".", _PC_NAME_MAX);
63   else
64     {
65       char c = *s;
66       *s = 0;
67       slen_max = pathconf (filename, _PC_NAME_MAX);
68       *s = c;
69     }
70 #endif
71   if (slen_max < 0)
72     slen_max = HAVE_LONG_FILE_NAMES ? 255 : 14;
73 
74   if (HAVE_DOS_FILE_NAMES && slen_max <= 12)
75     {
76       /* Live within DOS's 8.3 limit.  */
77       char *dot = strchr (s, '.');
78       if (dot)
79         {
80           slen -= dot + 1 - s;
81           s = dot + 1;
82           slen_max = 3;
83         }
84       else
85         slen_max = 8;
86       extlen = 9; /* Don't use EXT.  */
87     }
88 
89   if (slen + extlen <= slen_max)
90     strcpy (s + slen, ext);
91   else
92     {
93       if (slen_max <= slen)
94         slen = slen_max - 1;
95       s[slen] = e;
96       s[slen + 1] = 0;
97     }
98 }
99