home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / BASENAME.C < prev    next >
C/C++ Source or Header  |  1987-02-07  |  3KB  |  122 lines

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