Use "", not <> to include non-system header files.
[gnulib.git] / lib / addext.c
1 /* addext.c -- add an extension to a file name
2    Copyright 1990, 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Paul Eggert */
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #ifndef HAVE_DOS_FILE_NAMES
26 # define HAVE_DOS_FILE_NAMES 0
27 #endif
28 #ifndef HAVE_LONG_FILE_NAMES
29 # define HAVE_LONG_FILE_NAMES 0
30 #endif
31
32 #if HAVE_LIMITS_H
33 # include <limits.h>
34 #endif
35 #ifndef _POSIX_NAME_MAX
36 # define _POSIX_NAME_MAX 14
37 #endif
38
39 #include <sys/types.h>
40 #if HAVE_STRING_H
41 # include <string.h>
42 #else
43 # include <strings.h>
44 #endif
45
46 #if HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49
50 #include "backupfile.h"
51 #include "dirname.h"
52
53 /* Append to FILENAME the extension EXT, unless the result would be too long,
54    in which case just append the character E.  */
55
56 void
57 addext (char *filename, char const *ext, int e)
58 {
59   char *s = base_name (filename);
60   size_t slen = base_len (s), extlen = strlen (ext);
61   long slen_max = -1;
62
63 #if HAVE_PATHCONF && defined _PC_NAME_MAX
64   if (slen + extlen <= _POSIX_NAME_MAX && ! HAVE_DOS_FILE_NAMES)
65     /* The file name is so short there's no need to call pathconf.  */
66     slen_max = _POSIX_NAME_MAX;
67   else if (s == filename)
68     slen_max = pathconf (".", _PC_NAME_MAX);
69   else
70     {
71       char c = *s;
72       if (! ISSLASH (c))
73         *s = 0;
74       slen_max = pathconf (filename, _PC_NAME_MAX);
75       *s = c;
76     }
77 #endif
78   if (slen_max < 0)
79     slen_max = HAVE_LONG_FILE_NAMES ? 255 : 14;
80
81   if (HAVE_DOS_FILE_NAMES && slen_max <= 12)
82     {
83       /* Live within DOS's 8.3 limit.  */
84       char *dot = strchr (s, '.');
85       if (dot)
86         {
87           slen -= dot + 1 - s;
88           s = dot + 1;
89           slen_max = 3;
90         }
91       else
92         slen_max = 8;
93       extlen = 9; /* Don't use EXT.  */
94     }
95
96   if (slen + extlen <= slen_max)
97     strcpy (s + slen, ext);
98   else
99     {
100       if (slen_max <= slen)
101         slen = slen_max - 1;
102       s[slen] = e;
103       s[slen + 1] = 0;
104     }
105 }