Sync to trunk HEAD (r43416)
[reactos.git] / rosapps / dflat32 / direct.c
1 /* ---------- direct.c --------- */
2
3 #include <direct.h>
4 #include <io.h>
5
6 #include "dflat.h"
7
8 #define DRIVE 1
9 #define DIRECTORY 2
10 #define FILENAME 4
11 #define EXTENSION 8
12
13 static char path[MAX_PATH];
14 static char drive[_MAX_DRIVE] = " :";
15 static char dir[_MAX_DIR];
16 static char name[_MAX_FNAME];
17 static char ext[_MAX_EXT];
18
19 /* ----- Create unambiguous path from file spec, filling in the
20 drive and directory if incomplete. Optionally change to
21 the new drive and subdirectory ------ */
22 void DfCreatePath(char *path,char *fspec,int InclName,int Change)
23 {
24 int cm = 0;
25 char currdir[MAX_PATH];
26 char *cp;
27
28 /* save the current directory */
29 if (!Change)
30 GetCurrentDirectory (MAX_PATH, currdir);
31
32 *drive = *dir = *name = *ext = '\0';
33 _splitpath(fspec, drive, dir, name, ext);
34 if (!InclName)
35 *name = *ext = '\0';
36 *drive = toupper(*drive);
37 if (*ext)
38 cm |= EXTENSION;
39 if (InclName && *name)
40 cm |= FILENAME;
41 if (*dir)
42 cm |= DIRECTORY;
43 if (*drive)
44 cm |= DRIVE;
45 if (cm & DRIVE)
46 _chdrive(*drive - '@');
47 else
48 {
49 *drive = _getdrive();
50 *drive += '@';
51 }
52 if (cm & DIRECTORY)
53 {
54 cp = dir+strlen(dir)-1;
55 if (*cp == '\\')
56 *cp = '\0';
57 chdir(dir);
58 }
59 getcwd(dir, sizeof dir);
60 memmove(dir, dir+2, strlen(dir+1));
61 if (InclName) {
62 if (!(cm & FILENAME))
63 strcpy(name, "*");
64 if (!(cm & EXTENSION) && strchr(fspec, '.') != NULL)
65 strcpy(ext, ".*");
66 }
67 else
68 *name = *ext = '\0';
69 if (dir[strlen(dir)-1] != '\\')
70 strcat(dir, "\\");
71 memset(path, 0, sizeof path);
72 _makepath(path, drive, dir, name, ext);
73
74 if (!Change)
75 SetCurrentDirectory (currdir);
76 }
77
78
79 static int dircmp(const void *c1, const void *c2)
80 {
81 return stricmp(*(char **)c1, *(char **)c2);
82 }
83
84
85 BOOL DfDlgDirList(DFWINDOW wnd, char *fspec,
86 enum DfCommands nameid, enum DfCommands pathid,
87 unsigned attrib)
88 {
89 int ax, i = 0;
90 struct _finddata_t ff;
91 DF_CTLWINDOW *ct = DfFindCommand(wnd->extension,nameid,DF_LISTBOX);
92 DFWINDOW lwnd;
93 char **dirlist = NULL;
94
95 DfCreatePath(path, fspec, TRUE, TRUE);
96 if (ct != NULL)
97 {
98 lwnd = ct->wnd;
99 DfSendMessage(ct->wnd, DFM_CLEARTEXT, 0, 0);
100
101 if (attrib & 0x8000)
102 {
103 DWORD cd, dr;
104
105 cd = GetLogicalDrives ();
106 for (dr = 0; dr < 26; dr++)
107 {
108 if (cd & (1 << dr))
109 {
110 char drname[15];
111
112 sprintf(drname, "[%c:\\]", (char)(dr+'A'));
113 #if 0
114 /* ---- test for network or RAM disk ---- */
115 regs.x.ax = 0x4409; /* IOCTL func 9 */
116 regs.h.bl = dr+1;
117 int86(DOS, &regs, &regs);
118 if (!regs.x.cflag) {
119 if (regs.x.dx & 0x1000)
120 strcat(drname, " (Network)");
121 else if (regs.x.dx == 0x0800)
122 strcat(drname, " (RAMdisk)");
123 }
124 #endif
125 DfSendMessage(lwnd,DFM_ADDTEXT,(DF_PARAM)drname,0);
126 }
127 }
128 DfSendMessage(lwnd, DFM_PAINT, 0, 0);
129 }
130 ax = _findfirst(path, &ff);
131 if (ax == -1)
132 return FALSE;
133 do
134 {
135 if (!((attrib & 0x4000) &&
136 (ff.attrib & (attrib & 0x3f)) == 0) &&
137 strcmp(ff.name, "."))
138 {
139 char fname[MAX_PATH+2];
140 sprintf(fname, (ff.attrib & FILE_ATTRIBUTE_DIRECTORY) ?
141 "[%s]" : "%s" , ff.name);
142 dirlist = DfRealloc(dirlist,
143 sizeof(char *)*(i+1));
144 dirlist[i] = DfMalloc(strlen(fname)+1);
145 if (dirlist[i] != NULL)
146 strcpy(dirlist[i], fname);
147 i++;
148 }
149 }
150 while (_findnext(ax, &ff) == 0);
151 _findclose(ax);
152 if (dirlist != NULL)
153 {
154 int j;
155 /* -- sort file/drive/directory list box data -- */
156 qsort(dirlist, i, sizeof(void *), dircmp);
157
158 /* ---- send sorted list to list box ---- */
159 for (j = 0; j < i; j++) {
160 DfSendMessage(lwnd,DFM_ADDTEXT,(DF_PARAM)dirlist[j],0);
161 free(dirlist[j]);
162 }
163 free(dirlist);
164 }
165 DfSendMessage(lwnd, DFM_SHOW_WINDOW, 0, 0);
166 }
167 if (pathid)
168 {
169 _makepath(path, drive, dir, NULL, NULL);
170 DfPutItemText(wnd, pathid, path);
171 }
172 return TRUE;
173 }
174
175 /* EOF */