File: basechsuffix.c

package info (click to toggle)
texlive-bin 2016.20160513.41080.dfsg-2%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 155,060 kB
  • sloc: ansic: 702,862; cpp: 222,090; perl: 76,014; sh: 23,402; makefile: 8,078; lex: 4,710; pascal: 3,782; python: 3,614; java: 3,569; yacc: 2,905; exp: 2,031; xml: 800; tcl: 670; ruby: 620; lisp: 397; sed: 351; asm: 140; csh: 48; awk: 30
file content (43 lines) | stat: -rw-r--r-- 1,280 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* basechsuffix.c: replace the last bit of a filename with something else.

   Written in 1995 by Karl Berry.  Public domain.  */

#include <w2c/config.h>
#include "lib.h"

/* Return the basename of NAME, with trailing characters OLD replaced by
   NEW.  (If last characters in NAME are not OLD, just append NEW.)
   Since this is used to turn, e.g., foo/cmr10.300pk -> cmr10.300gf,
   don't assume a `.' preceding OLD or NEW.
   
   In other words, we're implementing `basename NAME OLD`NEW.  */

string
basenamechangesuffix (const_string name,  const_string old_suffix,
                      const_string new_suffix)
{
  string answer;
  unsigned c;
  const_string base = xbasename (name);
  unsigned base_len = strlen (base);
  unsigned copy_limit = base_len;
  unsigned old_suffix_len = strlen (old_suffix);
  
  if (old_suffix_len <= base_len) {
    for (c = 0; c < old_suffix_len; c++) {
      if (!FILECHARCASEEQ (old_suffix[old_suffix_len - c - 1],
                       base[base_len - c - 1]))
        break;
    }
    if (c == old_suffix_len) {
      copy_limit -= old_suffix_len;
    }
  }
  
  answer = xmalloc (copy_limit + strlen (new_suffix) + 1);
  strncpy (answer, base, copy_limit);
  answer[copy_limit] = 0;
  strcat (answer, new_suffix);

  return answer;
}