home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / zoo / part01 / basename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-08-16  |  3.1 KB  |  125 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) basename.c 1.4 87/05/29 12:53:28";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. */
  8.  
  9. #include "zoo.h"
  10. #include "options.h"
  11. #include "parse.h"
  12. #include <stdio.h>
  13. #include "various.h"
  14. #include "zoofns.h"
  15. #include "debug.h"
  16. #include "assert.h"
  17.  
  18. /* This function strips device/directory information from
  19. a pathname and returns just the plain filename */
  20. void basename (pathname, fname)
  21. char *pathname;
  22. char fname[];
  23. {
  24.    strcpy (fname, nameptr (pathname));
  25. }
  26.  
  27. /* Set of legal MSDOS filename characters.  The working of cvtchr() depends
  28. on the order of the first few characters here.  In particular, '_' is
  29. positioned so '.' gets converted to it. */
  30. static char legal[] = 
  31. "tabcdefghijklmnopqrs_uvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@^`{}~!#$%&'()-";
  32.  
  33. /****************
  34. cvtchr() converts a character to a lowercase alphabetic character in
  35. a somewhat random way.  
  36. */
  37. #define  cvtchr(ch)        legal[(ch & 0xff) % 26]
  38.  
  39. /***************
  40. cleanup() cleans up a string so it contains only legal MSDOS filename
  41. characters.  Any other characters are converted to an underscore.
  42. If the filename is null or if it begins with a dot, it is fixed.
  43. All dots are also converted.
  44. */
  45. void cleanup (p)
  46. char *p;
  47. {
  48.    assert(p != NULL);
  49.    if (*p == '\0')
  50.       strcpy (p, "X");
  51.    if (*p == '.')
  52.       *p = '_';
  53.    while (*p != '\0') {
  54.       if (strchr (legal, *p) == NULL) {   /* if invalid character */
  55.          *p = cvtchr(*p);
  56.       }
  57.       p++;
  58.    }
  59. }
  60. /* This function strips device/directory information from a pathname,
  61. forces the remaining filename to MSDOS format, and returns it.  Any
  62. illegal characters are fixed.
  63. */
  64. void dosname (pathname, fname)
  65. char *pathname;
  66. char fname[];
  67. {
  68.    struct path_st path_st;
  69.    parse (&path_st, pathname);
  70.    strcpy (fname, path_st.fname);
  71.    cleanup (fname);
  72.    if (path_st.ext[0] != '\0') {
  73.       strcat (fname, ".");
  74.       cleanup (path_st.ext);
  75.       strcat (fname, path_st.ext);
  76.    }
  77. }
  78.  
  79. /* 
  80. This function accepts a pathname and returns the extension.  If there is
  81. none, it returns a null string
  82. */
  83.  
  84. void extension (pathname, ext)
  85. char *pathname;
  86. register char ext[];
  87. {
  88.    struct path_st path_st;
  89.    parse (&path_st, pathname);
  90.    strcpy (ext, path_st.ext);
  91. }
  92.  
  93. /* rootname() */
  94. /* Accepts a pathname.  Returns the root filename, i.e., with both the
  95. directory path and the extension stripped. */
  96.  
  97. void rootname (path, root)
  98. char *path, *root;
  99. {
  100.    char *p;
  101.    static char dot[] = {EXT_CH, '\0'};
  102.    strcpy(root, nameptr(path));           /* copy all but path prefix */
  103.    p = findlast(root, dot);               /* find last dot */
  104.    if (p != NULL)                         /* if found ... */
  105.       *p = '\0';                          /* ... null it out */
  106. }
  107.  
  108. /* nameptr() */
  109. /* Accepts a pathname.  Returns a pointer to the filename within
  110. that pathname.
  111. */
  112.  
  113. char *nameptr (path)
  114. char *path;
  115. {
  116.    char *t;
  117.    t = findlast (path, PATH_SEP);   /* last char separating device/directory */
  118.    debug ((printf ("nameptr:  findlast returned ptr to string [%s].\n",t)))
  119.    if (t == NULL)                /* no separator */
  120.       return (path);
  121.    else {
  122.       return (t+1);
  123.    }
  124. }
  125.