Back to index

tetex-bin  3.0
basechsuffix.c
Go to the documentation of this file.
00001 /* basechsuffix.c: replace the last bit of a filename with something else.
00002 
00003    Written in 1995 by Karl Berry.  Public domain.  */
00004 
00005 #include "config.h"
00006 
00007 /* Return the basename of NAME, with trailing characters OLD replaced by
00008    NEW.  (If last characters in NAME are not OLD, just append NEW.)
00009    Since this is used to turn, e.g., foo/cmr10.300pk -> cmr10.300gf,
00010    don't assume a `.' preceding OLD or NEW.
00011    
00012    In other words, we're implementing `basename NAME OLD`NEW.  */
00013 
00014 string
00015 basenamechangesuffix P3C(const_string, name,  const_string, old_suffix,
00016                          const_string, new_suffix)
00017 {
00018   string answer;
00019   unsigned c;
00020   const_string base = xbasename (name);
00021   unsigned base_len = strlen (base);
00022   unsigned copy_limit = base_len;
00023   unsigned old_suffix_len = strlen (old_suffix);
00024   
00025   if (old_suffix_len <= base_len) {
00026     for (c = 0; c < old_suffix_len; c++) {
00027       if (!FILECHARCASEEQ (old_suffix[old_suffix_len - c - 1],
00028                        base[base_len - c - 1]))
00029         break;
00030     }
00031     if (c == old_suffix_len) {
00032       copy_limit -= old_suffix_len;
00033     }
00034   }
00035   
00036   answer = (string)xmalloc (copy_limit + strlen (new_suffix) + 1);
00037   strncpy (answer, base, copy_limit);
00038   answer[copy_limit] = 0;
00039   strcat (answer, new_suffix);
00040 
00041   return answer;
00042 }