in lib:
[gnulib.git] / lib / addext.c
1 /* addext.c -- add an extension to a file name
2
3    Copyright (C) 1990, 1997, 1998, 1999, 2001, 2003 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; see the file COPYING.
18    If not, write to the Free Software Foundation,
19    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Paul Eggert */
22
23 #if HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #ifndef HAVE_DOS_FILE_NAMES
28 # define HAVE_DOS_FILE_NAMES 0
29 #endif
30 #ifndef HAVE_LONG_FILE_NAMES
31 # define HAVE_LONG_FILE_NAMES 0
32 #endif
33
34 #include <limits.h>
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 <errno.h>
51 #ifndef errno
52 extern int errno;
53 #endif
54
55 #include "backupfile.h"
56 #include "dirname.h"
57
58 /* Append to FILENAME the extension EXT, unless the result would be too long,
59    in which case just append the character E.  */
60
61 void
62 addext (char *filename, char const *ext, int e)
63 {
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;
68
69 #if HAVE_PATHCONF && defined _PC_NAME_MAX
70   if (_POSIX_NAME_MAX < slen + extlen || HAVE_DOS_FILE_NAMES)
71     {
72       /* The new base name is long enough to require a pathconf check.  */
73       long name_max;
74       errno = 0;
75       if (s == filename)
76         name_max = pathconf (".", _PC_NAME_MAX);
77       else
78         {
79           char c = *s;
80           if (! ISSLASH (c))
81             *s = 0;
82           name_max = pathconf (filename, _PC_NAME_MAX);
83           *s = c;
84         }
85       if (0 <= name_max || errno == 0)
86         {
87           long size = slen_max = name_max;
88           if (name_max != size)
89             slen_max = -1;
90         }
91     }
92 #endif
93
94   if (HAVE_DOS_FILE_NAMES && slen_max <= 12)
95     {
96       /* Live within DOS's 8.3 limit.  */
97       char *dot = strchr (s, '.');
98       if (dot)
99         {
100           slen -= dot + 1 - s;
101           s = dot + 1;
102           slen_max = 3;
103         }
104       else
105         slen_max = 8;
106       extlen = 9; /* Don't use EXT.  */
107     }
108
109   if (slen + extlen <= slen_max)
110     strcpy (s + slen, ext);
111   else
112     {
113       if (slen_max <= slen)
114         slen = slen_max - 1;
115       s[slen] = e;
116       s[slen + 1] = 0;
117     }
118 }