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