home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / zoo_src / z201src1 / basename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-25  |  3.2 KB  |  128 lines

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