1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00

New files with implementations of dirent and uname for Win32.

This commit is contained in:
Marius Vollmer 2001-11-01 23:23:41 +00:00
parent d245ce231d
commit 8dd6dfdcd2
4 changed files with 480 additions and 0 deletions

152
libguile/win32-dirent.c Normal file
View file

@ -0,0 +1,152 @@
/* Copyright (C) 2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
#include "libguile/__scm.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "dirent.h"
DIR *
opendir (const char * name)
{
DIR *dir;
HANDLE hnd;
char *file;
WIN32_FIND_DATA find;
if (!name || !*name)
return NULL;
file = malloc (strlen (name) + 3);
strcpy (file, name);
if (file[strlen (name) - 1] != '/' && file[strlen (name) - 1] != '\\')
strcat (file, "/*");
else
strcat (file, "*");
if ((hnd = FindFirstFile (file, &find)) == INVALID_HANDLE_VALUE)
{
free (file);
return NULL;
}
dir = malloc (sizeof (DIR));
dir->mask = file;
dir->fd = (int) hnd;
dir->data = malloc (sizeof (WIN32_FIND_DATA));
dir->allocation = sizeof (WIN32_FIND_DATA);
dir->size = dir->allocation;
dir->filepos = 0;
memcpy (dir->data, &find, sizeof (WIN32_FIND_DATA));
return dir;
}
struct dirent *
readdir (DIR * dir)
{
static struct dirent entry;
WIN32_FIND_DATA *find;
entry.d_ino = 0;
entry.d_type = 0;
find = (WIN32_FIND_DATA *) dir->data;
if (dir->filepos)
{
if (!FindNextFile ((HANDLE) dir->fd, find))
return NULL;
}
entry.d_off = dir->filepos;
strncpy (entry.d_name, find->cFileName, sizeof (entry.d_name));
entry.d_reclen = strlen (find->cFileName);
dir->filepos++;
return &entry;
}
int
closedir (DIR * dir)
{
HANDLE hnd = (HANDLE) dir->fd;
free (dir->data);
free (dir->mask);
free (dir);
return FindClose (hnd) ? 0 : -1;
}
void
rewinddir (DIR * dir)
{
HANDLE hnd = (HANDLE) dir->fd;
WIN32_FIND_DATA *find = (WIN32_FIND_DATA *) dir->data;
FindClose (hnd);
hnd = FindFirstFile (dir->mask, find);
dir->fd = (int) hnd;
dir->filepos = 0;
}
void
seekdir (DIR * dir, off_t offset)
{
off_t n;
rewinddir (dir);
for (n = 0; n < offset; n++)
{
if (FindNextFile ((HANDLE) dir->fd, (WIN32_FIND_DATA *) dir->data))
dir->filepos++;
}
}
off_t
telldir (DIR * dir)
{
return dir->filepos;
}
int
dirfd (DIR * dir)
{
return dir->fd;
}

88
libguile/win32-dirent.h Normal file
View file

@ -0,0 +1,88 @@
/* classes: h_files */
#ifndef SCM_DIRENT_H
#define SCM_DIRENT_H
/* Copyright (C) 2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
/* Directory stream type.
The miscellaneous Unix `readdir' implementations read directory data
into a buffer and return `struct dirent *' pointers into it. */
#include <sys/types.h>
struct dirstream
{
int fd; /* File descriptor. */
char *data; /* Directory block. */
size_t allocation; /* Space allocated for the block. */
size_t size; /* Total valid data in the block. */
size_t offset; /* Current offset into the block. */
off_t filepos; /* Position of next entry to read. */
char *mask; /* Initial file mask. */
};
struct dirent
{
long d_ino;
off_t d_off;
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256];
};
#define d_fileno d_ino /* Backwards compatibility. */
/* This is the data type of directory stream objects.
The actual structure is opaque to users. */
typedef struct dirstream DIR;
DIR * opendir (const char * name);
struct dirent * readdir (DIR * dir);
int closedir (DIR * dir);
void rewinddir (DIR * dir);
void seekdir (DIR * dir, off_t offset);
off_t telldir (DIR * dir);
int dirfd (DIR * dir);
#endif /* SCM_DIRENT_H */

165
libguile/win32-uname.c Normal file
View file

@ -0,0 +1,165 @@
/* Copyright (C) 2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
#include "libguile/__scm.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "uname.h"
/*
* Get name and information about current kernel.
*/
int
uname (struct utsname *uts)
{
enum { WinNT, Win95, Win98, WinUnknown };
OSVERSIONINFO osver;
SYSTEM_INFO sysinfo;
DWORD sLength;
DWORD os = WinUnknown;
memset (uts, 0, sizeof (*uts));
osver.dwOSVersionInfoSize = sizeof (osver);
GetVersionEx (&osver);
GetSystemInfo (&sysinfo);
switch (osver.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT: /* NT, Windows 2000 or Windows XP */
if (osver.dwMajorVersion == 4)
strcpy (uts->sysname, "Windows NT4x"); /* NT4x */
else if (osver.dwMajorVersion <= 3)
strcpy (uts->sysname, "Windows NT3x"); /* NT3x */
else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion < 1)
strcpy (uts->sysname, "Windows 2000"); /* 2k */
else if (osver.dwMajorVersion >= 5)
strcpy (uts->sysname, "Windows XP"); /* XP */
os = WinNT;
break;
case VER_PLATFORM_WIN32_WINDOWS: /* Win95, Win98 or WinME */
if ((osver.dwMajorVersion > 4) ||
((osver.dwMajorVersion == 4) && (osver.dwMinorVersion > 0)))
{
if (osver.dwMinorVersion >= 90)
strcpy (uts->sysname, "Windows ME"); /* ME */
else
strcpy (uts->sysname, "Windows 98"); /* 98 */
os = Win98;
}
else
{
strcpy (uts->sysname, "Windows 95"); /* 95 */
os = Win95;
}
break;
case VER_PLATFORM_WIN32s: /* Windows 3.x */
strcpy (uts->sysname, "Windows");
break;
}
sprintf (uts->version, "%ld.%02ld",
osver.dwMajorVersion, osver.dwMinorVersion);
if (osver.szCSDVersion[0] != '\0' &&
(strlen (osver.szCSDVersion) + strlen (uts->version) + 1) <
sizeof (uts->version))
{
strcat (uts->version, " ");
strcat (uts->version, osver.szCSDVersion);
}
sprintf (uts->release, "build %ld", osver.dwBuildNumber & 0xFFFF);
switch (sysinfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_PPC:
strcpy (uts->machine, "ppc");
break;
case PROCESSOR_ARCHITECTURE_ALPHA:
strcpy (uts->machine, "alpha");
break;
case PROCESSOR_ARCHITECTURE_MIPS:
strcpy (uts->machine, "mips");
break;
case PROCESSOR_ARCHITECTURE_INTEL:
/*
* dwProcessorType is only valid in Win95 and Win98 and WinME
* wProcessorLevel is only valid in WinNT
*/
switch (os)
{
case Win95:
case Win98:
switch (sysinfo.dwProcessorType)
{
case PROCESSOR_INTEL_386:
case PROCESSOR_INTEL_486:
case PROCESSOR_INTEL_PENTIUM:
sprintf (uts->machine, "i%ld", sysinfo.dwProcessorType);
break;
default:
strcpy (uts->machine, "i386");
break;
}
break;
case WinNT:
sprintf (uts->machine, "i%d86", sysinfo.wProcessorLevel);
break;
default:
strcpy (uts->machine, "unknown");
break;
}
break;
default:
strcpy (uts->machine, "unknown");
break;
}
sLength = sizeof (uts->nodename) - 1;
GetComputerName (uts->nodename, &sLength);
return 0;
}

75
libguile/win32-uname.h Normal file
View file

@ -0,0 +1,75 @@
/* classes: h_files */
#ifndef SCM_UNAME_H
#define SCM_UNAME_H
/* Copyright (C) 2001 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*
* As a special exception, the Free Software Foundation gives permission
* for additional uses of the text contained in its release of GUILE.
*
* The exception is that, if you link the GUILE library with other files
* to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the GUILE library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the
* Free Software Foundation under the name GUILE. If you copy
* code from other Free Software Foundation releases into a copy of
* GUILE, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for GUILE, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice. */
#define _UTSNAME_LENGTH 65
#define _UTSNAME_NODENAME_LENGTH _UTSNAME_LENGTH
#define _UTSNAME_DOMAIN_LENGTH _UTSNAME_LENGTH
/* Structure describing the system and machine. */
struct utsname
{
/* Name of the implementation of the operating system. */
char sysname[_UTSNAME_LENGTH];
/* Name of this node on the network. */
char nodename[_UTSNAME_NODENAME_LENGTH];
/* Current release level of this implementation. */
char release[_UTSNAME_LENGTH];
/* Current version level of this release. */
char version[_UTSNAME_LENGTH];
/* Name of the hardware type the system is running on. */
char machine[_UTSNAME_LENGTH];
/* Name of the domain of this node on the network. */
char domainname[_UTSNAME_DOMAIN_LENGTH];
};
int uname (struct utsname *uts);
#endif /* SCM_UNAME_H */