Back to index

tetex-bin  3.0
Functions
filenames.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "basics.h"

Go to the source code of this file.

Functions

static char * charptr (char *name, char c)
int equal (char *s, char *t)
char * extension (char *str)
char * newname (char *name, char *ext)
char * basename (char *str, char *suffix)
int ps_resource (char *name)

Function Documentation

char* basename ( char *  str,
char *  suffix 
)

Definition at line 82 of file filenames.c.

                                       {   
   char *p, *t; int len = 0;
   char *base;

   printf("basename of %s = ", str);
#ifdef KPATHSEA
   for (p = base = (NAME_BEGINS_WITH_DEVICE(str) ? str+2 : str);
       *p; p++) {
#else
   for (p = base = str; *p; p++) {
#endif
     /*      if (*p++ == DIRSEP) { base = p; len = 0; } */
     if (IS_DIR_SEP(*p)) { base = p+1; len = 0; }
      else len++;
   }
   printf("%s\n", base);
   if (suffix != NULL) {
      for (t = suffix; *t; ++t);
      do {
        len--; t--; p--;
        if (*t != *p) break;
        if (t == suffix) { char *bn;
            if (len == 0) return NULL;
            bn = malloc(len+1);
            if (bn == NULL) fatal("Out of memory\n");
            strncpy(bn, base, len);
            *(bn+len) = '\0'; /* RA */
            return bn;
         }
      } while (p > base);
   }
   return base;
}

Here is the call graph for this function:

static char* charptr ( char *  name,
char  c 
) [static]

Definition at line 22 of file filenames.c.

{
   while (*name != c && *name != '\0') name++; 
   if (*name == '\0') return NULL;
   else return name;
}

Here is the caller graph for this function:

int equal ( char *  s,
char *  t 
)

Definition at line 30 of file filenames.c.

{
#  ifndef UNIX
   while (tolower(*s) == tolower(*t)) {
      if (*s == '\0') break;
      s++; t++;
   }
#  else
   while (*s == *t) {
      if (*s == '\0' || *t == '\0')  break;
      s++; t++;
   }
#  endif
   if (*s == '\0' && *t == '\0') return 1;
   else return 0;
}
char* extension ( char *  str)

Definition at line 50 of file filenames.c.

                           {
   char *p, *base;
   for (p = base = str; *p; ++p)
     /*      if (*p++ == DIRSEP) base = p; */
     if (IS_DIR_SEP(*p)) base = p + 1;
   for (p = base ; *p; p++)
      if (*p == '.') break;
   return p;
}
char* newname ( char *  name,
char *  ext 
)

Definition at line 64 of file filenames.c.

{  
   char *e, *nn; int len1, len2;

   e = extension(name);
   if (equal(ext, e)) return name;
   len1 = strlen(name) - strlen(e);
   len2 = len1 + strlen(ext) + 1;
   nn = (char *) malloc(len2);
   if (nn == NULL) fatal("Out of memory\n");
   strncpy(nn, name, len1);
   strcpy(nn+len1, ext);
   return nn;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ps_resource ( char *  name)

Definition at line 120 of file filenames.c.

                            {
   if (charptr(name, '.')) return 0 ;
#ifdef KPATHSEA
   if (kpse_absolute_p(name, true)) return 0;
#else
   if (charptr(name, DIRSEP)) return 0 ;
#endif
   return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function: