addext.c 2.5 KB

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