home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / dos / lfn / lfnshort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-01  |  1.6 KB  |  83 lines

  1. #include <libc/stubs.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <dpmi.h>
  6. #include <go32.h>
  7. #include <sys/movedata.h>
  8.  
  9. static int
  10. msdos_toupper_fname (int c)
  11. {
  12.   return (c >= 'a' && c <= 'z') ? toupper (c) : c;
  13. }
  14.  
  15. char *
  16. _lfn_gen_short_fname (const char *long_fname, char *short_fname)
  17. {
  18.   __dpmi_regs r;
  19.   unsigned long tbuf = __tb & 0xfffff;
  20.  
  21.   r.x.ax = 0x7100;
  22.   if (_USE_LFN)
  23.     {
  24.       dosmemput (long_fname, strlen (long_fname) + 1, tbuf);
  25.       r.x.ax = 0x71a8;
  26.       r.x.ds = tbuf >> 4;
  27.       r.x.si = 0;
  28.       r.x.es = r.x.ds;
  29.       r.x.di = 260;
  30.       r.x.dx = 0x0011;    /* DH=01 would be better, but it's buggy */
  31.       __dpmi_int (0x21, &r);
  32.     }
  33.  
  34.   if ((r.x.flags & 1) == 0 && r.x.ax != 0x7100)
  35.     {
  36.       char buf[13], *s = buf, *d = short_fname;
  37.  
  38.       dosmemget (tbuf + 260, sizeof buf, buf);
  39.  
  40.       /* The short name in BUF is ASCIZ in the FCB format.
  41.      Convert to 8.3 filename.  */
  42.       while (s - buf < 8 && *s && *s != ' ')
  43.     *d++ = *s++;
  44.       while (*s && *s == ' ')
  45.     s++;
  46.       if (*s)
  47.     {
  48.       *d++ = '.';
  49.       while (*s && *s != ' ')
  50.         *d++ = *s++;
  51.     }
  52.       *d = '\0';
  53.     }
  54.   else
  55.     {
  56.       const char *s = long_fname;
  57.       char *d = short_fname;
  58.  
  59.       while ((*d++ = msdos_toupper_fname (*s++)))
  60.     if (d - short_fname >= 12)
  61.       {
  62.         *d = '\0';
  63.         break;
  64.       }
  65.     }
  66.   return short_fname;
  67. }
  68.  
  69. #ifdef TEST
  70.  
  71. #include <stdio.h>
  72.  
  73. int main (int argc, char *argv[])
  74. {
  75.   char sh[13];
  76.   if (argc > 1)
  77.     printf ("Orig:  %s\nShort: %s\n",
  78.         argv[1], _lfn_gen_short_fname(argv[1], sh));
  79.   return 0;
  80. }
  81.  
  82. #endif
  83.