1 /* addext.c -- add an extension to a file name
2 Copyright 1990, 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING.
16 If not, write to the Free Software Foundation,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Paul Eggert */
25 #ifndef HAVE_DOS_FILE_NAMES
26 # define HAVE_DOS_FILE_NAMES 0
28 #ifndef HAVE_LONG_FILE_NAMES
29 # define HAVE_LONG_FILE_NAMES 0
35 #ifndef _POSIX_NAME_MAX
36 # define _POSIX_NAME_MAX 14
39 #include <sys/types.h>
55 #include "backupfile.h"
58 /* Append to FILENAME the extension EXT, unless the result would be too long,
59 in which case just append the character E. */
62 addext (char *filename
, char const *ext
, int e
)
64 char *s
= base_name (filename
);
65 size_t slen
= base_len (s
);
66 size_t extlen
= strlen (ext
);
67 size_t slen_max
= HAVE_LONG_FILE_NAMES
? 255 : _POSIX_NAME_MAX
;
69 #if HAVE_PATHCONF && defined _PC_NAME_MAX
70 if (_POSIX_NAME_MAX
< slen
+ extlen
|| HAVE_DOS_FILE_NAMES
)
72 /* The new base name is long enough to require a pathconf check. */
76 name_max
= pathconf (".", _PC_NAME_MAX
);
82 name_max
= pathconf (filename
, _PC_NAME_MAX
);
85 if (0 <= name_max
|| errno
== 0)
86 slen_max
= name_max
== (size_t) name_max
? name_max
: -1;
90 if (HAVE_DOS_FILE_NAMES
&& slen_max
<= 12)
92 /* Live within DOS's 8.3 limit. */
93 char *dot
= strchr (s
, '.');
102 extlen
= 9; /* Don't use EXT. */
105 if (slen
+ extlen
<= slen_max
)
106 strcpy (s
+ slen
, ext
);
109 if (slen_max
<= slen
)