File: basename.c

package info (click to toggle)
zoo 2.10-9
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 780 kB
  • ctags: 1,288
  • sloc: ansic: 9,041; asm: 793; makefile: 211
file content (123 lines) | stat: -rw-r--r-- 3,185 bytes parent folder | download | duplicates (7)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef LINT
/* @(#) basename.c 2.2 87/12/27 13:42:40 */
static char sccsid[]="@(#) basename.c 2.2 87/12/27 13:42:40";
#endif /* LINT */

/*
Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
*/

#include "options.h"
#include "zooio.h"
#include "zoo.h"
#include "parse.h"
#include "various.h"
#include "zoofns.h"
#include "debug.h"
#include "assert.h"

/* This function strips device/directory information from
a pathname and returns just the plain filename */
void zoo_basename (pathname, fname)
char *pathname;
char fname[];
{
   strcpy (fname, nameptr (pathname));
}

/* Set of legal MSDOS filename characters.  The working of cvtchr() depends
on the order of the first few characters here.  In particular, '_' is
positioned so '.' gets converted to it. */
static char legal[] = 
"tabcdefghijklmnopqrs_uvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@^`{}~!#$%&'()-";

/****************
cvtchr() converts a character to a lowercase alphabetic character in
a somewhat random way.  
*/
#define  cvtchr(ch)        legal[(ch & 0xff) % 26]

/***************
cleanup() cleans up a string so it contains only legal MSDOS filename
characters.  Any other characters are converted to an underscore.
If the filename is null or if it begins with a dot, it is fixed.
All dots are also converted.
*/

void cleanup (p)
char *p;
{
   assert(p != NULL);
   if (*p == '\0')
      strcpy (p, "X");
   if (*p == '.')
      *p = '_';
   while (*p != '\0') {
      if (strchr (legal, *p) == NULL) {   /* if invalid character */
         *p = cvtchr(*p);
      }
      p++;
   }
}
/* This function strips device/directory information from a pathname,
forces the remaining filename to MSDOS format, and returns it.  Any
illegal characters are fixed.
*/
void dosname (pathname, fname)
char *pathname;
char fname[];
{
   struct path_st path_st;
   parse (&path_st, pathname);
   strcpy (fname, path_st.fname);
   cleanup (fname);

#ifdef VER_CH  /* remove any trailing extension field */
   if (path_st.ext[0] != '\0')
      strip_ver (path_st.ext);
#endif

   /* extension could have been nulled, so we test again */
   if (path_st.ext[0] != '\0') {
      cleanup (path_st.ext);
      strcat (fname, ".");
      strcat (fname, path_st.ext);
   }

#ifdef SPECMOD
   specfname (fname);
#endif
}

/* rootname() */
/* Accepts a pathname.  Returns the root filename, i.e., with both the
directory path and the extension stripped. */

void rootname (path, root)
char *path, *root;
{
   char *p;
   static char dot[] = {EXT_CH, '\0'};
   strcpy(root, nameptr(path));           /* copy all but path prefix */
   p = findlast(root, dot);               /* find last dot */
   if (p != NULL)                         /* if found ... */
      *p = '\0';                          /* ... null it out */
}

/* nameptr() */
/* Accepts a pathname.  Returns a pointer to the filename within
that pathname.
*/

char *nameptr (path)
char *path;
{
   char *t;
   t = findlast (path, PATH_SEP);   /* last char separating device/directory */
   debug ((printf ("nameptr:  findlast returned ptr to string [%s].\n",t)))
   if (t == NULL)                /* no separator */
      return (path);
   else {
      return (t+1);
   }
}