home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / dflt14.zip / DIRECT.C < prev    next >
Text File  |  1992-04-22  |  5KB  |  174 lines

  1. /* ---------- direct.c --------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. static char path[MAXPATH];
  6. static char drive[MAXDRIVE] = " :";
  7. static char dir[MAXDIR];
  8. static char name[MAXFILE];
  9. static char ext[MAXEXT];
  10.  
  11. /* ----- Create unambiguous path from file spec, filling in the
  12.      drive and directory if incomplete. Optionally change to
  13.      the new drive and subdirectory ------ */
  14. void CreatePath(char *path,char *fspec,int InclName,int Change)
  15. {
  16.     int cm = 0;
  17.     unsigned currdrive;
  18.     char currdir[64];
  19.     char *cp;
  20.  
  21.     if (!Change)    {
  22.         /* ---- save the current drive and subdirectory ---- */
  23.         currdrive = getdisk();
  24.         getcwd(currdir, sizeof currdir);
  25.         memmove(currdir, currdir+2, strlen(currdir+1));
  26.         cp = currdir+strlen(currdir)-1;
  27.         if (*cp == '\\')
  28.             *cp = '\0';
  29.     }
  30.     *drive = *dir = *name = *ext = '\0';
  31.     fnsplit(fspec, drive, dir, name, ext);
  32.     if (!InclName)
  33.         *name = *ext = '\0';
  34.     *drive = toupper(*drive);
  35.     if (*ext)
  36.         cm |= EXTENSION;
  37.     if (InclName && *name)
  38.         cm |= FILENAME;
  39.     if (*dir)
  40.         cm |= DIRECTORY;
  41.     if (*drive)
  42.         cm |= DRIVE;
  43.     if (cm & DRIVE)
  44.         setdisk(*drive - 'A');
  45.     else     {
  46.         *drive = getdisk();
  47.         *drive += 'A';
  48.     }
  49.     if (cm & DIRECTORY)    {
  50.         cp = dir+strlen(dir)-1;
  51.         if (*cp == '\\')
  52.             *cp = '\0';
  53.         chdir(dir);
  54.     }
  55.     getcwd(dir, sizeof dir);
  56.     memmove(dir, dir+2, strlen(dir+1));
  57.     if (InclName)    {
  58.         if (!(cm & FILENAME))
  59.             strcpy(name, "*");
  60.         if (!(cm & EXTENSION) && strchr(fspec, '.') != NULL)
  61.             strcpy(ext, ".*");
  62.     }
  63.     else
  64.         *name = *ext = '\0';
  65.     if (dir[strlen(dir)-1] != '\\')
  66.         strcat(dir, "\\");
  67.     memset(path, 0, sizeof path);
  68.     fnmerge(path, drive, dir, name, ext);
  69.     if (!Change)    {
  70.         setdisk(currdrive);
  71.         chdir(currdir);
  72.     }
  73. }
  74.  
  75. static int dircmp(const void *c1, const void *c2)
  76. {
  77.     return stricmp(*(char **)c1, *(char **)c2);
  78. }
  79.  
  80. BOOL DlgDirList(WINDOW wnd, char *fspec,
  81.                 enum commands nameid, enum commands pathid,
  82.                 unsigned attrib)
  83. {
  84.     int ax, i = 0, criterr = 1;
  85.     struct ffblk ff;
  86.     CTLWINDOW *ct = FindCommand(wnd->extension,nameid,LISTBOX);
  87.     WINDOW lwnd;
  88.     char **dirlist = NULL;
  89.  
  90.     CreatePath(path, fspec, TRUE, TRUE);
  91.     if (ct != NULL)    {
  92.         lwnd = ct->wnd;
  93.         SendMessage(ct->wnd, CLEARTEXT, 0, 0);
  94.  
  95.         if (attrib & 0x8000)    {
  96.             union REGS regs;
  97.             char drname[15];
  98.             unsigned int cd, dr;
  99.  
  100.             cd = getdisk();
  101.             for (dr = 0; dr < 26; dr++)    {
  102.                 unsigned ndr;
  103.                 setdisk(dr);
  104.                 ndr = getdisk();
  105.                 if (ndr == dr)    {
  106.                     /* ----- test for remapped B drive ----- */
  107.                     if (dr == 1)    {
  108.                         regs.x.ax = 0x440e; /* IOCTL func 14 */
  109.                         regs.h.bl = dr+1;
  110.                         int86(DOS, ®s, ®s);
  111.                         if (regs.h.al != 0)
  112.                             continue;
  113.                     }
  114.  
  115.                     sprintf(drname, "[%c:]", dr+'A');
  116.  
  117.                     /* ---- test for network or RAM disk ---- */
  118.                     regs.x.ax = 0x4409;     /* IOCTL func 9 */
  119.                     regs.h.bl = dr+1;
  120.                     int86(DOS, ®s, ®s);
  121.                     if (!regs.x.cflag)    {
  122.                         if (regs.x.dx & 0x1000)
  123.                             strcat(drname, " (Network)");
  124.                         else if (regs.x.dx == 0x0800)
  125.                             strcat(drname, " (RAMdisk)");
  126.                     }
  127.                     SendMessage(lwnd,ADDTEXT,(PARAM)drname,0);
  128.                 }
  129.             }
  130.             setdisk(cd);
  131.         }
  132.         while (criterr == 1)    {
  133.             ax = findfirst(path, &ff, attrib & 0x3f);
  134.             criterr = TestCriticalError();
  135.         }
  136.         if (criterr)
  137.             return FALSE;
  138.         while (ax == 0)    {
  139.             if (!((attrib & 0x4000) &&
  140.                     (ff.ff_attrib & (attrib & 0x3f)) == 0) &&
  141.                         strcmp(ff.ff_name, "."))    {
  142.                 char fname[15];
  143.                 sprintf(fname, (ff.ff_attrib & 0x10) ?
  144.                                 "[%s]" : "%s" , ff.ff_name);
  145.                 dirlist = DFrealloc(dirlist,
  146.                                     sizeof(char *)*(i+1));
  147.                 dirlist[i] = DFmalloc(strlen(fname)+1);
  148.                 if (dirlist[i] != NULL)
  149.                     strcpy(dirlist[i], fname);
  150.                 i++;
  151.             }
  152.             ax = findnext(&ff);
  153.         }
  154.         if (dirlist != NULL)    {
  155.             int j;
  156.             /* -- sort file/drive/directory list box data -- */
  157.             qsort(dirlist, i, sizeof(void *), dircmp);
  158.  
  159.             /* ---- send sorted list to list box ---- */
  160.             for (j = 0; j < i; j++)    {
  161.                 SendMessage(lwnd,ADDTEXT,(PARAM)dirlist[j],0);
  162.                 free(dirlist[j]);
  163.             }
  164.             free(dirlist);
  165.         }
  166.         SendMessage(lwnd, SHOW_WINDOW, 0, 0);
  167.     }
  168.     if (pathid)    {
  169.         fnmerge(path, drive, dir, NULL, NULL);
  170.         PutItemText(wnd, pathid, path);
  171.     }
  172.     return TRUE;
  173. }
  174.