3 * NOV 1993 Erik Bos (erik@(trashcan.)hacktic.nl)
5 * FindFile by Bob, hacked for dos & unixpaths by Erik.
17 #include <sys/types.h>
18 #include <sys/mount.h>
28 #define MAX_OPEN_DIRS 16
30 extern char WindowsDirectory[256], SystemDirectory[256],TempDirectory[256];
32 char WindowsPath[256];
34 void DOS_DeInitFS(void);
35 int DOS_SetDefaultDrive(int);
36 char *GetDirectUnixFileName(char *);
42 struct DosDriveStruct { /* eg: */
43 char *rootdir; /* /usr/windows */
44 char cwd[256]; /* / */
45 char label[13]; /* DRIVE-A */
46 unsigned int serialnumber; /* ABCD5678 */
50 struct DosDriveStruct DosDrives[MAX_DOS_DRIVES];
52 struct dosdirent DosDirs[MAX_OPEN_DIRS];
57 char drive[2], temp[256], *ptr;
59 GetPrivateProfileString("wine", "windows", "c:\\windows",
60 WindowsDirectory, sizeof(WindowsDirectory), WINE_INI);
62 GetPrivateProfileString("wine", "system", "c:\\windows\\system",
63 SystemDirectory, sizeof(SystemDirectory), WINE_INI);
65 GetPrivateProfileString("wine", "temp", "c:\\windows",
66 TempDirectory, sizeof(TempDirectory), WINE_INI);
68 GetPrivateProfileString("wine", "path", "c:\\windows;c:\\windows\\system",
69 WindowsPath, sizeof(WindowsPath), WINE_INI);
71 ToDos(WindowsDirectory);
72 ToDos(SystemDirectory);
77 fprintf(stderr,"wine.ini = %s\n",WINE_INI);
78 fprintf(stderr,"win.ini = %s\n",WIN_INI);
79 fprintf(stderr,"windir = %s\n",WindowsDirectory);
80 fprintf(stderr,"sysdir = %s\n",SystemDirectory);
81 fprintf(stderr,"tempdir = %s\n",TempDirectory);
82 fprintf(stderr,"path = %s\n",WindowsPath);
85 for (x=0; x!=MAX_DOS_DRIVES; x++) {
86 DosDrives[x].serialnumber = (0xEB0500L | x);
90 GetPrivateProfileString("drives", drive, "*", temp, sizeof(temp), WINE_INI);
91 if (!strcmp(temp, "*") || *temp == '\0') {
92 DosDrives[x].rootdir = NULL;
93 DosDrives[x].cwd[0] = '\0';
94 DosDrives[x].label[0] = '\0';
95 DosDrives[x].disabled = 1;
99 if ((ptr = (char *) malloc(strlen(temp)+1)) == NULL) {
100 fprintf(stderr,"DOSFS: can't malloc for drive info!");
103 if (temp[strlen(temp)-1] == '/')
104 temp[strlen(temp)] = '\0';
105 DosDrives[x].rootdir = ptr;
106 strcpy(DosDrives[x].rootdir, temp);
107 strcpy(DosDrives[x].cwd, "/windows/");
108 strcpy(DosDrives[x].label, "DRIVE-");
109 strcat(DosDrives[x].label, drive);
110 DosDrives[x].disabled = 0;
113 atexit(DOS_DeInitFS);
115 DOS_SetDefaultDrive(2);
117 for (x=0; x!=MAX_DOS_DRIVES; x++) {
118 if (DosDrives[x].rootdir != NULL) {
120 fprintf(stderr, "DOSFS: %c: => %-40s %s %s %X %d\n",
122 DosDrives[x].rootdir,
125 DosDrives[x].serialnumber,
126 DosDrives[x].disabled
132 for (x=0; x!=MAX_OPEN_DIRS ; x++)
133 DosDirs[x].inuse = 0;
137 void DOS_DeInitFS(void)
141 for (x=0; x!=MAX_DOS_DRIVES ; x++)
142 if (DosDrives[x].rootdir != NULL) {
145 fprintf(stderr, "DOSFS: %c: => %s %s %s %X %d\n",
147 DosDrives[x].rootdir,
150 DosDrives[x].serialnumber,
151 DosDrives[x].disabled
153 free(DosDrives[x].rootdir);
158 WORD DOS_GetEquipment(void)
163 /* borrowed from Ralph Brown's interrupt lists
165 bits 15-14: number of parallel devices
166 bit 13: [Conv] Internal modem
168 bits 11- 9: number of serial devices
170 bits 7- 6: number of diskette drives minus one
171 bits 5- 4: Initial video mode:
177 bit 2: [PS] =1 if pointing device
179 bit 1: =1 if math co-processor
180 bit 0: =1 if diskette available for boot
183 if (DosDrives[0].rootdir != NULL)
185 if (DosDrives[1].rootdir != NULL)
190 equipment = diskdrives << 6;
195 int DOS_ValidDrive(int drive)
199 fprintf(stderr,"ValidDrive %c (%d)\n",'A'+drive,drive);
202 if (drive >= MAX_DOS_DRIVES)
204 if (DosDrives[drive].rootdir == NULL)
206 if (DosDrives[drive].disabled)
212 int DOS_GetDefaultDrive(void)
215 fprintf(stderr,"GetDefaultDrive (%c)\n",'A'+CurrentDrive);
218 return( CurrentDrive);
221 int DOS_SetDefaultDrive(int drive)
224 fprintf(stderr,"SetDefaultDrive to %c:\n",'A'+drive);
227 if (!DOS_ValidDrive(drive))
230 CurrentDrive = drive;
257 int DOS_DisableDrive(int drive)
259 if (drive >= MAX_DOS_DRIVES)
261 if (DosDrives[drive].rootdir == NULL)
264 DosDrives[drive].disabled = 1;
268 int DOS_EnableDrive(int drive)
270 if (drive >= MAX_DOS_DRIVES)
272 if (DosDrives[drive].rootdir == NULL)
275 DosDrives[drive].disabled = 0;
279 void GetUnixDirName(char *rootdir, char *name)
282 char *nameptr, *cwdptr;
284 cwdptr = rootdir + strlen(rootdir);
288 fprintf(stderr,"GetUnixDirName: %s <=> %s => ",rootdir, name);
292 if (*nameptr == '.' & !filename) {
294 if (*nameptr == '\0') {
298 if (*nameptr == '.') {
300 while (cwdptr != rootdir) {
302 if (*cwdptr == '/') {
310 if (*nameptr == '\\' || *nameptr == '/') {
316 if (*nameptr == '\\' || *nameptr == '/') {
325 *cwdptr++ = *nameptr++;
332 fprintf(stderr,"%s\n", rootdir);
337 char *GetDirectUnixFileName(char *dosfilename)
339 /* a:\windows\system.ini => /dos/windows/system.ini */
341 static char temp[256];
345 if (dosfilename[1] == ':')
347 drive = (islower(*dosfilename) ? toupper(*dosfilename) : *dosfilename) - 'A';
349 if (!DOS_ValidDrive(drive))
354 drive = CurrentDrive;
356 strcpy(temp,DosDrives[drive].rootdir);
357 strcat(temp, DosDrives[drive].cwd);
358 GetUnixDirName(temp + strlen(DosDrives[drive].rootdir), dosfilename);
363 fprintf(stderr,"GetDirectUnixFileName: %c:%s => %s\n",'A'+ drive, dosfilename, temp);
369 char *GetUnixFileName(char *dosfilename)
371 char *dirname, *unixname, workingpath[256], temp[256];
373 /* check if program specified a path */
375 if (*dosfilename == '.' || *dosfilename == '\\')
376 return( GetDirectUnixFileName(dosfilename) );
378 /* nope, lets find it */
381 fprintf(stderr,"GetUnixFileName: %s\n",dosfilename);
384 strcpy(workingpath, WindowsPath);
386 for(dirname = strtok(workingpath, ";") ;
388 dirname = strtok(NULL, ";"))
390 strcpy(temp,dirname);
391 if (temp[strlen(temp)-1] != '\\')
393 strcat(temp,dosfilename);
396 fprintf(stderr,"trying %s\n",temp);
399 if ( (unixname = GetDirectUnixFileName(temp)) != NULL)
406 char *DOS_GetCurrentDir(int drive, char *dirname)
408 /* should return 'windows\system' */
412 if (!DOS_ValidDrive(drive))
415 strcpy(temp, DosDrives[drive].cwd);
418 if (temp[strlen(temp)-1] == '\\')
419 temp[strlen(temp)] = '\0';
422 fprintf(stderr,"DOS_GetCWD: %c:\%s",'A'+drive, temp+1);
427 int DOS_ChangeDir(int drive, char *dirname)
429 if (!DOS_ValidDrive(drive))
432 GetUnixDirName(DosDrives[drive].cwd, dirname);
433 strcat(DosDrives[drive].cwd,"/");
435 fprintf(stderr,"DOS_SetCWD: %c:\%s",'A'+drive, DosDrives[drive].cwd);
440 int DOS_MakeDir(int drive, char *dirname)
444 if (!DOS_ValidDrive(drive))
447 strcpy(temp, DosDrives[drive].cwd);
448 GetUnixDirName(temp, dirname);
449 strcat(DosDrives[drive].cwd,"/");
455 fprintf(stderr,"DOS_MakeDir: %c:\%s => %s",'A'+drive, dirname, temp);
462 strcpy(DosDrives[0].cwd, "1/2/3/");
464 puts(DosDrives[0].cwd);
466 puts(DosDrives[0].cwd);
468 ChangeDir(0,"..\\..");
469 puts(DosDrives[0].cwd);
472 puts(DosDrives[0].cwd);
475 puts(DosDrives[0].cwd);
477 ChangeDir(0,"\\qwerty\\ab");
478 puts(DosDrives[0].cwd);
480 ChangeDir(0,"erik\\.\\bos\\..\\24");
481 puts(DosDrives[0].cwd);
486 int DOS_GetSerialNumber(int drive, unsigned long *serialnumber)
488 if (!DOS_ValidDrive(drive))
491 *serialnumber = DosDrives[drive].serialnumber;
495 int DOS_SetSerialNumber(int drive, unsigned long serialnumber)
497 if (!DOS_ValidDrive(drive))
500 DosDrives[drive].serialnumber = serialnumber;
504 int DOS_GetFreeSpace(int drive, long *size, long *available)
508 if (!DOS_ValidDrive(drive))
511 if (statfs(DosDrives[drive].rootdir, &info) < 0) {
512 fprintf(stderr,"dosfs: cannot do statfs(%s)\n",DosDrives[drive].rootdir);
516 *size = info.f_bsize * info.f_blocks / 1024;
517 *available = info.f_bavail * info.f_bsize / 1024;
522 char *FindFile(char *buffer, int buflen, char *rootname, char **extensions,
534 if (strchr(rootname, '\\') != NULL)
536 strncpy(buffer, GetDirectUnixFileName(rootname), buflen);
540 fprintf(stderr,"FindFile: %s -> %s\n",rootname,buffer);
546 if (strchr(rootname, '/') != NULL)
548 strncpy(buffer, rootname, buflen);
551 fprintf(stderr,"FindFile: %s -> %s\n",rootname,buffer);
558 fprintf(stderr,"FindFile: looking for %s\n",rootname);
563 rootnamelen = strlen(rootname);
564 workingpath = malloc(strlen(path) + 1);
565 if (workingpath == NULL)
567 strcpy(workingpath, path);
569 for(dirname = strtok(workingpath, ";");
571 dirname = strtok(NULL, ";"))
573 if (strchr(dirname, '\\')!=NULL)
574 d = opendir( GetDirectUnixFileName(dirname) );
576 d = opendir( dirname );
579 fprintf(stderr,"in %s\n",dirname);
584 while ((f = readdir(d)) != NULL)
586 if (strncasecmp(rootname, f->d_name, rootnamelen) == 0)
588 if (extensions == NULL ||
589 strcasecmp(rootname, f->d_name) == 0)
593 else if (f->d_name[rootnamelen] == '.')
595 for (e = extensions; *e != NULL; e++)
597 if (strcasecmp(*e, f->d_name + rootnamelen + 1)
608 if (strchr(dirname, '\\')!=NULL)
609 strncpy(buffer, GetDirectUnixFileName(dirname), buflen);
611 strncpy(buffer, dirname, buflen);
613 if (buffer[strlen(buffer)-1]!='/')
614 strncat(buffer, "/", buflen - strlen(buffer));
616 strncat(buffer, f->d_name, buflen - strlen(buffer));
631 /**********************************************************************
634 char *WineIniFileName(void)
636 static char *IniName = NULL;
642 getcwd(inipath, 256);
643 strcat(inipath, ";");
644 strcat(inipath, getenv("HOME"));
645 strcat(inipath, ";");
646 strcat(inipath, getenv("WINEPATH"));
648 IniName = malloc(1024);
649 if (FindFile(IniName, 1024, "wine.ini", NULL, inipath) == NULL)
656 IniName = realloc(IniName, strlen(IniName) + 1);
663 char *WinIniFileName()
665 static char name[256];
667 strcpy(name,GetDirectUnixFileName(WindowsDirectory));
668 strcat(name,"win.ini");
675 struct dosdirent *DOS_opendir(char *dosdirname)
681 for (x=0; x != MAX_OPEN_DIRS && DosDirs[x].inuse; x++)
684 if (x == MAX_OPEN_DIRS)
687 if ((unixdirname = GetDirectUnixFileName(dosdirname)) == NULL)
690 strcpy(temp,unixdirname);
704 fprintf(stderr,"%s -> %s\n",unixdirname,temp);
706 DosDirs[x].inuse = 1;
707 strcpy(DosDirs[x].unixpath, temp);
709 if ((DosDirs[x].ds = opendir(temp)) == NULL)
716 struct dosdirent *DOS_readdir(struct dosdirent *de)
725 if ((d = readdir(de->ds)) == NULL)
732 strcpy(de->filename, d->d_name);
733 if (d->d_reclen > 12)
734 de->filename[12] = '\0';
735 ToDos (de->filename);
739 strcpy(temp,de->unixpath);
741 strcat(temp,de->filename);
744 if S_ISDIR(st.st_mode)
745 de->attribute |= 0x08;
750 void DOS_closedir(struct dosdirent *de)