Files
86Box/src/qt/win_opendir.c

179 lines
4.1 KiB
C
Raw Normal View History

2017-05-18 14:03:43 -04:00
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
2017-05-18 14:03:43 -04:00
*
* This file is part of the 86Box distribution.
2017-05-18 14:03:43 -04:00
*
* Implementation POSIX OpenDir(3) and friends for Win32 API.
2017-05-18 14:03:43 -04:00
*
* Based on old original code @(#)dir_win32.c 1.2.0 2007/04/19
2017-05-18 14:03:43 -04:00
*
2020-03-25 00:46:02 +02:00
*
2017-05-18 14:03:43 -04:00
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
*
* Copyright 1998-2007 MicroWalt Corporation
* Copyright 2017 Fred N. van Kempen
* Copyright 2021-2023 Jasmine Iwanek.
2017-05-18 14:03:43 -04:00
*/
#include <io.h>
#include <stdio.h>
#include <stdint.h>
2017-05-18 14:03:43 -04:00
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include <86box/86box.h>
#include <86box/plat.h>
#include <86box/plat_dir.h>
2017-05-18 14:03:43 -04:00
2022-10-15 13:38:10 -03:00
#define SUFFIX "\\*"
#define FINDATA struct _finddata_t
#define FINDFIRST _findfirst
#define FINDNEXT _findnext
2017-05-18 14:03:43 -04:00
/* Open a directory. */
DIR *
opendir(const char *name)
{
DIR *p;
/* Create a new control structure. */
p = (DIR *) malloc(sizeof(DIR));
if (p == NULL)
2022-10-15 13:38:10 -03:00
return (NULL);
2017-05-18 14:03:43 -04:00
memset(p, 0x00, sizeof(DIR));
2022-10-15 13:38:10 -03:00
p->flags = (DIR_F_LOWER | DIR_F_SANE);
2017-05-18 14:03:43 -04:00
p->offset = 0;
2022-10-15 13:38:10 -03:00
p->sts = 0;
2017-05-18 14:03:43 -04:00
/* Create a work area. */
2022-10-15 13:38:10 -03:00
p->dta = (char *) malloc(sizeof(FINDATA));
2017-05-18 14:03:43 -04:00
if (p->dta == NULL) {
2022-10-15 13:38:10 -03:00
free(p);
return (NULL);
2017-05-18 14:03:43 -04:00
}
memset(p->dta, 0x00, sizeof(struct _finddata_t));
/* Add search filespec. */
strcpy(p->dir, name);
strcat(p->dir, SUFFIX);
/* Special case: flag if we are in the root directory. */
if (strlen(p->dir) == 3)
2022-10-15 13:38:10 -03:00
p->flags |= DIR_F_ISROOT;
2017-05-18 14:03:43 -04:00
/* Start the searching by doing a FindFirst. */
2022-10-15 13:38:10 -03:00
p->handle = FINDFIRST(p->dir, (FINDATA *) p->dta);
2017-05-18 14:03:43 -04:00
if (p->handle < 0L) {
2022-10-15 13:38:10 -03:00
free(p->dta);
free(p);
return (NULL);
2017-05-18 14:03:43 -04:00
}
/* All OK. */
2023-06-01 18:32:25 -04:00
return p;
2017-05-18 14:03:43 -04:00
}
/* Close an open directory. */
int
closedir(DIR *p)
{
if (p == NULL)
2023-06-01 18:32:25 -04:00
return 0;
2017-05-18 14:03:43 -04:00
_findclose(p->handle);
if (p->dta != NULL)
2022-10-15 13:38:10 -03:00
free(p->dta);
2017-05-18 14:03:43 -04:00
free(p);
2023-06-01 18:32:25 -04:00
return 0;
2017-05-18 14:03:43 -04:00
}
/*
* Read the next entry from a directory.
* Note that the DOS (FAT), Windows (FAT, FAT32) and Windows NTFS
* file systems do not have a root directory containing the UNIX-
* standard "." and ".." entries. Many applications do assume
* this anyway, so we simply fake these entries.
*/
struct dirent *
2017-05-18 14:03:43 -04:00
readdir(DIR *p)
{
FINDATA *ffp;
if (p == NULL || p->sts == 1)
2022-10-15 13:38:10 -03:00
return (NULL);
2017-05-18 14:03:43 -04:00
/* Format structure with current data. */
2022-10-15 13:38:10 -03:00
ffp = (FINDATA *) p->dta;
2017-05-18 14:03:43 -04:00
p->dent.d_ino = 1L;
p->dent.d_off = p->offset++;
2022-10-15 13:38:10 -03:00
switch (p->offset) {
case 1: /* . */
strncpy(p->dent.d_name, ".", MAXNAMLEN + 1);
p->dent.d_reclen = 1;
break;
case 2: /* .. */
strncpy(p->dent.d_name, "..", MAXNAMLEN + 1);
p->dent.d_reclen = 2;
break;
default: /* regular entry. */
strncpy(p->dent.d_name, ffp->name, MAXNAMLEN + 1);
p->dent.d_reclen = (char) strlen(p->dent.d_name);
2017-05-18 14:03:43 -04:00
}
/* Read next entry. */
p->sts = 0;
/* Fake the "." and ".." entries here.. */
if ((p->flags & DIR_F_ISROOT) && (p->offset <= 2))
2022-10-15 13:38:10 -03:00
return (&(p->dent));
2017-05-18 14:03:43 -04:00
/* Get the next entry if we did not fake the above. */
if (FINDNEXT(p->handle, ffp) < 0)
2022-10-15 13:38:10 -03:00
p->sts = 1;
2017-05-18 14:03:43 -04:00
2022-10-15 13:38:10 -03:00
return (&(p->dent));
2017-05-18 14:03:43 -04:00
}
/* Report current position within the directory. */
long
telldir(DIR *p)
{
2022-10-15 13:38:10 -03:00
return (p->offset);
2017-05-18 14:03:43 -04:00
}
void
seekdir(DIR *p, long newpos)
{
short pos;
/* First off, rewind to start of directory. */
2022-10-15 13:38:10 -03:00
p->handle = FINDFIRST(p->dir, (FINDATA *) p->dta);
2017-05-18 14:03:43 -04:00
if (p->handle < 0L) {
2022-10-15 13:38:10 -03:00
p->sts = 1;
return;
2017-05-18 14:03:43 -04:00
}
p->offset = 0;
2022-10-15 13:38:10 -03:00
p->sts = 0;
2017-05-18 14:03:43 -04:00
/* If we are rewinding, that's all... */
2022-10-15 13:38:10 -03:00
if (newpos == 0L)
return;
2017-05-18 14:03:43 -04:00
/* Nope.. read entries until we hit the right spot. */
pos = (short) newpos;
while (p->offset != pos) {
2022-10-15 13:38:10 -03:00
p->offset++;
if (FINDNEXT(p->handle, (FINDATA *) p->dta) < 0) {
p->sts = 1;
return;
}
2017-05-18 14:03:43 -04:00
}
}