1 /* addext.c -- add an extension to a file name
2 Copyright (C) 1990, 1997-1999, 2001-2003, 2005-2006 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 3 of the License, or
7 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Paul Eggert */
21 #ifndef HAVE_DOS_FILE_NAMES
22 # define HAVE_DOS_FILE_NAMES 0
24 #ifndef HAVE_LONG_FILE_NAMES
25 # define HAVE_LONG_FILE_NAMES 0
28 #include "backupfile.h"
31 #ifndef _POSIX_NAME_MAX
32 # define _POSIX_NAME_MAX 14
35 #include <sys/types.h>
46 /* Append to FILENAME the extension EXT, unless the result would be too long,
47 in which case just append the character E. */
50 addext (char *filename, char const *ext, char e)
52 char *s = basename (filename);
53 size_t slen = strlen (s), extlen = strlen (ext);
56 #if HAVE_PATHCONF && defined _PC_NAME_MAX
57 if (slen + extlen <= _POSIX_NAME_MAX && ! HAVE_DOS_FILE_NAMES)
58 /* The file name is so short there's no need to call pathconf. */
59 slen_max = _POSIX_NAME_MAX;
60 else if (s == filename)
61 slen_max = pathconf (".", _PC_NAME_MAX);
66 slen_max = pathconf (filename, _PC_NAME_MAX);
71 slen_max = HAVE_LONG_FILE_NAMES ? 255 : 14;
73 if (HAVE_DOS_FILE_NAMES && slen_max <= 12)
75 /* Live within DOS's 8.3 limit. */
76 char *dot = strchr (s, '.');
85 extlen = 9; /* Don't use EXT. */
88 if (slen + extlen <= slen_max)
89 strcpy (s + slen, ext);