mirror of
https://github.com/FunKey-Project/RetroFE.git
synced 2026-04-02 10:16:50 +02:00
Port from git
This commit is contained in:
139
ThirdParty/dirent-1.20.1/examples/find.c
vendored
Normal file
139
ThirdParty/dirent-1.20.1/examples/find.c
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* An example demonstrating recursive directory traversal.
|
||||
*
|
||||
* Compile this file with Visual Studio 2008 project vs2008.sln and run
|
||||
* the produced command in console with a directory name argument. For
|
||||
* example, command
|
||||
*
|
||||
* find "C:\Program Files"
|
||||
*
|
||||
* might produce a listing with thousands of entries such as
|
||||
*
|
||||
* c:\Program Files/7-Zip/7-zip.chm
|
||||
* c:\Program Files/7-Zip/7-zip.dll
|
||||
* c:\Program Files/7-Zip/7z.dll
|
||||
* c:\Program Files/Adobe/Reader 10.0/Reader/logsession.dll
|
||||
* c:\Program Files/Adobe/Reader 10.0/Reader/LogTransport2.exe
|
||||
* c:\Program Files/Windows NT/Accessories/wordpad.exe
|
||||
* c:\Program Files/Windows NT/Accessories/write.wpc
|
||||
*
|
||||
* The find command provided by this file is only an example. That is,
|
||||
* the command does not provide options to restrict the output to certain
|
||||
* files as the Linux version does.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "dirent.h"
|
||||
|
||||
static int find_directory (const char *dirname);
|
||||
|
||||
|
||||
int
|
||||
main(
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int ok;
|
||||
|
||||
/* For each directory in command line */
|
||||
i = 1;
|
||||
while (i < argc) {
|
||||
ok = find_directory (argv[i]);
|
||||
if (!ok) {
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
/* List current working directory if no arguments on command line */
|
||||
if (argc == 1) {
|
||||
find_directory (".");
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/* Find files and subdirectories recursively */
|
||||
static int
|
||||
find_directory(
|
||||
const char *dirname)
|
||||
{
|
||||
DIR *dir;
|
||||
char buffer[PATH_MAX + 2];
|
||||
char *p = buffer;
|
||||
const char *src;
|
||||
char *end = &buffer[PATH_MAX];
|
||||
int ok;
|
||||
|
||||
/* Copy directory name to buffer */
|
||||
src = dirname;
|
||||
while (p < end && *src != '\0') {
|
||||
*p++ = *src++;
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
/* Open directory stream */
|
||||
dir = opendir (dirname);
|
||||
if (dir != NULL) {
|
||||
struct dirent *ent;
|
||||
|
||||
/* Print all files and directories within the directory */
|
||||
while ((ent = readdir (dir)) != NULL) {
|
||||
char *q = p;
|
||||
char c;
|
||||
|
||||
/* Get final character of directory name */
|
||||
if (buffer < q) {
|
||||
c = q[-1];
|
||||
} else {
|
||||
c = ':';
|
||||
}
|
||||
|
||||
/* Append directory separator if not already there */
|
||||
if (c != ':' && c != '/' && c != '\\') {
|
||||
*q++ = '/';
|
||||
}
|
||||
|
||||
/* Append file name */
|
||||
src = ent->d_name;
|
||||
while (q < end && *src != '\0') {
|
||||
*q++ = *src++;
|
||||
}
|
||||
*q = '\0';
|
||||
|
||||
/* Decide what to do with the directory entry */
|
||||
switch (ent->d_type) {
|
||||
case DT_LNK:
|
||||
case DT_REG:
|
||||
/* Output file name with directory */
|
||||
printf ("%s\n", buffer);
|
||||
break;
|
||||
|
||||
case DT_DIR:
|
||||
/* Scan sub-directory recursively */
|
||||
if (strcmp (ent->d_name, ".") != 0
|
||||
&& strcmp (ent->d_name, "..") != 0) {
|
||||
find_directory (buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Ignore device entries */
|
||||
/*NOP*/;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
closedir (dir);
|
||||
ok = 1;
|
||||
|
||||
} else {
|
||||
/* Could not open directory */
|
||||
printf ("Cannot open directory %s\n", dirname);
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
257
ThirdParty/dirent-1.20.1/examples/locate.c
vendored
Normal file
257
ThirdParty/dirent-1.20.1/examples/locate.c
vendored
Normal file
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* A file look-up utility to complement updatedb
|
||||
*
|
||||
* Compile and run updatedb first to create locate.db file. Then, compile
|
||||
* this program with the same Visual Studio 2008 project file and run the
|
||||
* program in console with a file name argument. For example, command
|
||||
*
|
||||
* locate autoexec
|
||||
*
|
||||
* might output
|
||||
*
|
||||
* c:/AUTOEXEC.BAT
|
||||
* c:/WINDOWS/repair/autoexec.nt
|
||||
* c:/WINDOWS/system32/AUTOEXEC.NT
|
||||
*
|
||||
* Be ware that this file uses wide-character API which is not compatible
|
||||
* with Linux or other major Unixes.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include "dirent.h"
|
||||
|
||||
/* File name and location of database file */
|
||||
#define DB_LOCATION L"locate.db"
|
||||
|
||||
|
||||
/* Forward-decl */
|
||||
static int db_locate (const wchar_t *pattern);
|
||||
static int db_match (const wchar_t *fn, const wchar_t *pattern);
|
||||
static void db_open (void);
|
||||
static void db_close (void);
|
||||
static int db_read (wchar_t *buffer, size_t max);
|
||||
|
||||
|
||||
/* Module local variables */
|
||||
static FILE *db = NULL;
|
||||
|
||||
|
||||
int
|
||||
main(
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Prepare for unicode output */
|
||||
_setmode (_fileno (stdout), _O_U16TEXT);
|
||||
|
||||
/* For each pattern in command line */
|
||||
i = 1;
|
||||
while (i < argc) {
|
||||
wchar_t buffer[PATH_MAX + 1];
|
||||
errno_t error;
|
||||
size_t n;
|
||||
int count = 0;
|
||||
|
||||
/* Convert ith argument to wide-character string */
|
||||
error = mbstowcs_s (&n, buffer, PATH_MAX, argv[i], _TRUNCATE);
|
||||
if (!error) {
|
||||
/* Find files matching pattern */
|
||||
count = db_locate (buffer);
|
||||
|
||||
/* Output warnign if string is not found */
|
||||
if (count == 0) {
|
||||
wprintf (L"%s not found\n", buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
if (argc < 2) {
|
||||
wprintf (L"Usage: locate pattern\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/* Match pattern against files in locate.db file */
|
||||
static int
|
||||
db_locate(
|
||||
const wchar_t *pattern)
|
||||
{
|
||||
wchar_t buffer[PATH_MAX + 1];
|
||||
int count = 0;
|
||||
|
||||
/* Open locate.db for read */
|
||||
db_open ();
|
||||
|
||||
/* Read one directory and file name at a time from database file */
|
||||
while (db_read (buffer, PATH_MAX)) {
|
||||
/* See if file name in buffer matches the search pattern */
|
||||
if (db_match (buffer, pattern)) {
|
||||
/* Match found => output file name and path */
|
||||
wprintf (L"%s\n", buffer);
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
db_close ();
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Match pattern against file name */
|
||||
static int
|
||||
db_match(
|
||||
const wchar_t *fn, const wchar_t *pattern)
|
||||
{
|
||||
wchar_t *p;
|
||||
wchar_t base[PATH_MAX + 1];
|
||||
wchar_t patt[PATH_MAX + 1];
|
||||
int done = 0;
|
||||
int found;
|
||||
int i;
|
||||
|
||||
/* Locate zero-terminator from fn */
|
||||
p = wcschr (fn, '\0');
|
||||
|
||||
/* Find base name from buffer */
|
||||
while (fn < p && !done) {
|
||||
switch (p[-1]) {
|
||||
case ':':
|
||||
case '/':
|
||||
case '\\':
|
||||
/* Final path separator found */
|
||||
done = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* No path separator yet */
|
||||
p--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Convert base name to lower case */
|
||||
i = 0;
|
||||
while (i < PATH_MAX && p[i] != '\0') {
|
||||
base[i] = towlower (p[i]);
|
||||
i++;
|
||||
}
|
||||
base[i] = '\0';
|
||||
|
||||
/* Convert search pattern to lower case */
|
||||
i = 0;
|
||||
while (i < PATH_MAX && pattern[i] != '\0') {
|
||||
patt[i] = towlower (pattern[i]);
|
||||
i++;
|
||||
}
|
||||
patt[i] = '\0';
|
||||
|
||||
/* See if file name matches pattern */
|
||||
if (wcsstr (base, patt) != NULL) {
|
||||
found = 1;
|
||||
} else {
|
||||
found = 0;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read line from locate.db. This function is same as fgetws() except
|
||||
* that new-line at the end of line is not included.
|
||||
*/
|
||||
static int
|
||||
db_read(
|
||||
wchar_t *buffer, size_t max)
|
||||
{
|
||||
size_t i = 0;
|
||||
wchar_t c;
|
||||
int done = 0;
|
||||
int ok = 0;
|
||||
|
||||
do {
|
||||
/* Read wide-character from stream */
|
||||
if (db) {
|
||||
c = fgetwc (db);
|
||||
} else {
|
||||
wprintf (L"Database not open\n");
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/* Determine how to process character */
|
||||
switch (c) {
|
||||
case '\r':
|
||||
/* Ignore, should be handled by run-time libraries */
|
||||
/*NOP*/;
|
||||
break;
|
||||
|
||||
case '\n':
|
||||
/* End of string => return file name and true */
|
||||
done = 1;
|
||||
ok = 1;
|
||||
break;
|
||||
|
||||
case /*EOF*/WEOF:
|
||||
/* End of file */
|
||||
done = 1;
|
||||
if (i == 0) {
|
||||
/* No data in buffer => return false to indicate EOF */
|
||||
ok = 0;
|
||||
} else {
|
||||
/* Data in buffer => return true */
|
||||
ok = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Store character */
|
||||
if (i < max - 1) {
|
||||
buffer[i++] = c;
|
||||
} else {
|
||||
wprintf (L"Buffer too small");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
} while (!done);
|
||||
|
||||
/* Zero-terminate buffer */
|
||||
buffer[i] = '\0';
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* Open database file locate.db */
|
||||
static void
|
||||
db_open(
|
||||
void)
|
||||
{
|
||||
if (db == NULL) {
|
||||
errno_t error;
|
||||
|
||||
/* Open file for writing */
|
||||
error = _wfopen_s (&db, DB_LOCATION, L"rt, ccs=UNICODE");
|
||||
if (error) {
|
||||
wprintf (L"Cannot open %s\n", DB_LOCATION);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Close database file */
|
||||
static void
|
||||
db_close(
|
||||
void)
|
||||
{
|
||||
if (db) {
|
||||
fclose (db);
|
||||
db = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
94
ThirdParty/dirent-1.20.1/examples/ls.c
vendored
Normal file
94
ThirdParty/dirent-1.20.1/examples/ls.c
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* An example demonstrating basic directory listing.
|
||||
*
|
||||
* Compile this file with Visual Studio 2008 project vs2008.sln and run the
|
||||
* produced command in console with a directory name argument. For example,
|
||||
* command
|
||||
*
|
||||
* ls "c:\Program Files"
|
||||
*
|
||||
* might output something like
|
||||
*
|
||||
* ./
|
||||
* ../
|
||||
* 7-Zip/
|
||||
* Internet Explorer/
|
||||
* Microsoft Visual Studio 9.0/
|
||||
* Microsoft.NET/
|
||||
* Mozilla Firefox/
|
||||
*
|
||||
* The ls command provided by this file is only an example. That is, the
|
||||
* command does not have any fancy options like "ls -al" in Linux and the
|
||||
* command does not support file name matching like "ls *.c".
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "dirent.h"
|
||||
|
||||
static void list_directory (const char *dirname);
|
||||
|
||||
|
||||
int
|
||||
main(
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
|
||||
/* For each directory in command line */
|
||||
i = 1;
|
||||
while (i < argc) {
|
||||
list_directory (argv[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
/* List current working directory if no arguments on command line */
|
||||
if (argc == 1) {
|
||||
list_directory (".");
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* List files and directories within a directory.
|
||||
*/
|
||||
static void
|
||||
list_directory(
|
||||
const char *dirname)
|
||||
{
|
||||
DIR *dir;
|
||||
struct dirent *ent;
|
||||
|
||||
/* Open directory stream */
|
||||
dir = opendir (dirname);
|
||||
if (dir != NULL) {
|
||||
|
||||
/* Print all files and directories within the directory */
|
||||
while ((ent = readdir (dir)) != NULL) {
|
||||
switch (ent->d_type) {
|
||||
case DT_REG:
|
||||
printf ("%s\n", ent->d_name);
|
||||
break;
|
||||
|
||||
case DT_DIR:
|
||||
printf ("%s/\n", ent->d_name);
|
||||
break;
|
||||
|
||||
case DT_LNK:
|
||||
printf ("%s@\n", ent->d_name);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf ("%s*\n", ent->d_name);
|
||||
}
|
||||
}
|
||||
|
||||
closedir (dir);
|
||||
|
||||
} else {
|
||||
/* Could not open directory */
|
||||
printf ("Cannot open directory %s\n", dirname);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
215
ThirdParty/dirent-1.20.1/examples/updatedb.c
vendored
Normal file
215
ThirdParty/dirent-1.20.1/examples/updatedb.c
vendored
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* An example demonstrating wide-character functions
|
||||
*
|
||||
* Compile this file with Visual Studio 2008 project vs2008.sln and run
|
||||
* the produced command in console with a directory name argument. For
|
||||
* example, command
|
||||
*
|
||||
* updatedb C:\
|
||||
*
|
||||
* will produce file locate.db containing lines such as
|
||||
*
|
||||
* c:\Program Files/7-Zip/7-zip.chm
|
||||
* c:\Program Files/7-Zip/7-zip.dll
|
||||
* c:\Program Files/7-Zip/7z.dll
|
||||
* c:\Program Files/Adobe/Reader 10.0/Reader/logsession.dll
|
||||
* c:\Program Files/Adobe/Reader 10.0/Reader/LogTransport2.exe
|
||||
* c:\Program Files/Windows NT/Accessories/wordpad.exe
|
||||
* c:\Program Files/Windows NT/Accessories/write.wpc
|
||||
*
|
||||
* Be ware that this code is not compatible with Linux or other major
|
||||
* Unixes. Linux does not even have wide-character version of dirent!
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include "dirent.h"
|
||||
|
||||
/* File name and location of database file */
|
||||
#define DB_LOCATION L"locate.db"
|
||||
|
||||
|
||||
/* Forward-decl */
|
||||
static int update_directory (const wchar_t *dirname);
|
||||
static void db_open (void);
|
||||
static void db_close (void);
|
||||
static void db_store (const wchar_t *dirname);
|
||||
|
||||
|
||||
/* Module local variables */
|
||||
static FILE *db = NULL;
|
||||
|
||||
|
||||
int
|
||||
main(
|
||||
int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
int ok;
|
||||
|
||||
/* Prepare for unicode output */
|
||||
_setmode (_fileno (stdout), _O_U16TEXT);
|
||||
|
||||
/* Open locate.db */
|
||||
db_open ();
|
||||
|
||||
/* For each directory in command line */
|
||||
i = 1;
|
||||
while (i < argc) {
|
||||
wchar_t buffer[PATH_MAX + 1];
|
||||
errno_t error;
|
||||
size_t n;
|
||||
|
||||
/* Convert ith argument to wide-character string */
|
||||
error = mbstowcs_s (&n, buffer, PATH_MAX, argv[i], _TRUNCATE);
|
||||
if (!error) {
|
||||
|
||||
/* Scan directory for files */
|
||||
ok = update_directory (buffer);
|
||||
if (!ok) {
|
||||
wprintf (L"Cannot open directory %s\n", buffer);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
/* Use current working directory if no arguments on command line */
|
||||
if (argc == 1) {
|
||||
update_directory (L".");
|
||||
}
|
||||
|
||||
db_close ();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/* Find files recursively */
|
||||
static int
|
||||
update_directory(
|
||||
const wchar_t *dirname)
|
||||
{
|
||||
_WDIR *dir;
|
||||
wchar_t buffer[PATH_MAX + 2];
|
||||
wchar_t *p = buffer;
|
||||
const wchar_t *src;
|
||||
wchar_t *end = &buffer[PATH_MAX];
|
||||
int ok;
|
||||
|
||||
/* Copy directory name to buffer */
|
||||
src = dirname;
|
||||
while (p < end && *src != '\0') {
|
||||
*p++ = *src++;
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
/* Open directory stream */
|
||||
dir = _wopendir (dirname);
|
||||
if (dir != NULL) {
|
||||
struct _wdirent *ent;
|
||||
|
||||
/* Print all files and directories within the directory */
|
||||
while ((ent = _wreaddir (dir)) != NULL) {
|
||||
wchar_t *q = p;
|
||||
wchar_t c;
|
||||
|
||||
/* Get final character of directory name */
|
||||
if (buffer < q) {
|
||||
c = q[-1];
|
||||
} else {
|
||||
c = ':';
|
||||
}
|
||||
|
||||
/* Append directory separator if not already there */
|
||||
if (c != ':' && c != '/' && c != '\\') {
|
||||
*q++ = '/';
|
||||
}
|
||||
|
||||
/* Append file name */
|
||||
src = ent->d_name;
|
||||
while (q < end && *src != '\0') {
|
||||
*q++ = *src++;
|
||||
}
|
||||
*q = '\0';
|
||||
|
||||
/* Decide what to do with the directory entry */
|
||||
switch (ent->d_type) {
|
||||
case DT_REG:
|
||||
/* Store file name */
|
||||
db_store (buffer);
|
||||
break;
|
||||
|
||||
case DT_DIR:
|
||||
/* Scan sub-directory recursively */
|
||||
if (wcscmp (ent->d_name, L".") != 0
|
||||
&& wcscmp (ent->d_name, L"..") != 0) {
|
||||
update_directory (buffer);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Do not device entries */
|
||||
/*NOP*/;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
wclosedir (dir);
|
||||
ok = 1;
|
||||
|
||||
} else {
|
||||
|
||||
/* Cannot open directory */
|
||||
ok = 0;
|
||||
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* Store file name to locate.db */
|
||||
static void
|
||||
db_store(
|
||||
const wchar_t *dirname)
|
||||
{
|
||||
if (db) {
|
||||
/* Output line to file */
|
||||
fwprintf (db, L"%s\n", dirname);
|
||||
} else {
|
||||
wprintf (L"Database not open\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
/* Open database file locate.db */
|
||||
static void
|
||||
db_open(
|
||||
void)
|
||||
{
|
||||
if (db == NULL) {
|
||||
errno_t error;
|
||||
|
||||
/* Open file for writing */
|
||||
error = _wfopen_s (&db, DB_LOCATION, L"wt, ccs=UNICODE");
|
||||
if (error) {
|
||||
wprintf (L"Cannot open %s\n", DB_LOCATION);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Close database file */
|
||||
static void
|
||||
db_close(
|
||||
void)
|
||||
{
|
||||
if (db) {
|
||||
fclose (db);
|
||||
db = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user