home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / BASENAME.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  3KB  |  124 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. void cleanup (p)
  48. char *p;
  49. {
  50.    assert(p != NULL);
  51.    if (*p == '\0')
  52.       strcpy (p, "X");
  53.    if (*p == '.')
  54.       *p = '_';
  55.    while (*p != '\0') {
  56.       if (strchr (legal, *p) == NULL) {   /* if invalid character */
  57.          *p = cvtchr(*p);
  58.       }
  59.       p++;
  60.    }
  61. }
  62. /* This function strips device/directory information from a pathname,
  63. forces the remaining filename to MSDOS format, and returns it.  Any
  64. illegal characters are fixed.
  65. */
  66. void dosname (pathname, fname)
  67. char *pathname;
  68. char fname[];
  69. {
  70.    struct path_st path_st;
  71.    parse (&path_st, pathname);
  72.    strcpy (fname, path_st.fname);
  73.    cleanup (fname);
  74.  
  75. #ifdef VER_CH  /* remove any trailing extension field */
  76.    if (path_st.ext[0] != '\0')
  77.       strip_ver (path_st.ext);
  78. #endif
  79.  
  80.    /* extension could have been nulled, so we test again */
  81.    if (path_st.ext[0] != '\0') {
  82.       cleanup (path_st.ext);
  83.       strcat (fname, ".");
  84.       strcat (fname, path_st.ext);
  85.    }
  86.  
  87. #ifdef SPECMOD
  88.    specfname (fname);
  89. #endif
  90. }
  91.  
  92. /* rootname() */
  93. /* Accepts a pathname.  Returns the root filename, i.e., with both the
  94. directory path and the extension stripped. */
  95.  
  96. void rootname (path, root)
  97. char *path, *root;
  98. {
  99.    char *p;
  100.    static char dot[] = {EXT_CH, '\0'};
  101.    strcpy(root, nameptr(path));           /* copy all but path prefix */
  102.    p = findlast(root, dot);               /* find last dot */
  103.    if (p != NULL)                         /* if found ... */
  104.       *p = '\0';                          /* ... null it out */
  105. }
  106.  
  107. /* nameptr() */
  108. /* Accepts a pathname.  Returns a pointer to the filename within
  109. that pathname.
  110. */
  111.  
  112. char *nameptr (path)
  113. char *path;
  114. {
  115.    char *t;
  116.    t = findlast (path, PATH_SEP);   /* last char separating device/directory */
  117.    debug ((printf ("nameptr:  findlast returned ptr to string [%s].\n",t)))
  118.    if (t == NULL)                /* no separator */
  119.       return (path);
  120.    else {
  121.       return (t+1);
  122.    }
  123. }
  124.