source: vendor/patch/2.5.9/addext.c

Last change on this file was 3444, checked in by bird, 18 years ago

patch 2.5.9

File size: 2.8 KB
Line 
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#if HAVE_LIMITS_H
35# include <limits.h>
36#endif
37#ifndef _POSIX_NAME_MAX
38# define _POSIX_NAME_MAX 14
39#endif
40
41#include <sys/types.h>
42#if HAVE_STRING_H
43# include <string.h>
44#else
45# include <strings.h>
46#endif
47
48#if HAVE_UNISTD_H
49# include <unistd.h>
50#endif
51
52#include <errno.h>
53#ifndef errno
54extern int errno;
55#endif
56
57#include "backupfile.h"
58#include "dirname.h"
59
60/* Append to FILENAME the extension EXT, unless the result would be too long,
61 in which case just append the character E. */
62
63void
64addext (char *filename, char const *ext, int e)
65{
66 char *s = base_name (filename);
67 size_t slen = base_len (s);
68 size_t extlen = strlen (ext);
69 size_t slen_max = HAVE_LONG_FILE_NAMES ? 255 : _POSIX_NAME_MAX;
70
71#if HAVE_PATHCONF && defined _PC_NAME_MAX
72 if (_POSIX_NAME_MAX < slen + extlen || HAVE_DOS_FILE_NAMES)
73 {
74 /* The new base name is long enough to require a pathconf check. */
75 long name_max;
76 errno = 0;
77 if (s == filename)
78 name_max = pathconf (".", _PC_NAME_MAX);
79 else
80 {
81 char c = *s;
82 if (! ISSLASH (c))
83 *s = 0;
84 name_max = pathconf (filename, _PC_NAME_MAX);
85 *s = c;
86 }
87 if (0 <= name_max || errno == 0)
88 {
89 long size = slen_max = name_max;
90 if (name_max != size)
91 slen_max = -1;
92 }
93 }
94#endif
95
96 if (HAVE_DOS_FILE_NAMES && slen_max <= 12)
97 {
98 /* Live within DOS's 8.3 limit. */
99 char *dot = strchr (s, '.');
100 if (dot)
101 {
102 slen -= dot + 1 - s;
103 s = dot + 1;
104 slen_max = 3;
105 }
106 else
107 slen_max = 8;
108 extlen = 9; /* Don't use EXT. */
109 }
110
111 if (slen + extlen <= slen_max)
112 strcpy (s + slen, ext);
113 else
114 {
115 if (slen_max <= slen)
116 slen = slen_max - 1;
117 s[slen] = e;
118 s[slen + 1] = 0;
119 }
120}
Note: See TracBrowser for help on using the repository browser.