Added enumdevdir showcasing the recently introduced device functions (together with directory access).

git-svn-id: svn://svn.cc65.org/cc65/trunk@5851 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
ol.sc
2012-10-15 18:52:40 +00:00
parent 976c94b8ca
commit 170c59e4c2
3 changed files with 145 additions and 37 deletions

View File

@@ -73,6 +73,7 @@ C1541 = c1541
EXELIST = ascii \ EXELIST = ascii \
diodemo \ diodemo \
enumdevdir \
fire \ fire \
gunzip65 \ gunzip65 \
hello \ hello \

View File

@@ -33,6 +33,13 @@ Platforms: The program does depend on conio and dio (direct disk i/o),
so it does currently compile for the Atari and Apple ][ so it does currently compile for the Atari and Apple ][
machines. machines.
-----------------------------------------------------------------------------
Name: enumdevdir
Description: Enumerates all devices, directories and files. Written and
contributed by Oliver Schmidt, <ol.sc@web.de>.
Platforms: All systems with device enumeration and directory access
(currently the C64, the C128 and the Apple ][).
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
Name: fire Name: fire
Description: Another graphics demo written by groepaz/hitmen. Description: Another graphics demo written by groepaz/hitmen.
@@ -112,4 +119,3 @@ Description: Shows some of the graphics capabilities of the "tiny graphics
interface". interface".
Platforms: Runs on all platforms that have TGI support: Platforms: Runs on all platforms that have TGI support:
Apple ][, C64, C128, Oric Atmos, Geos and Lynx. Apple ][, C64, C128, Oric Atmos, Geos and Lynx.

101
samples/enumdevdir.c Normal file
View File

@@ -0,0 +1,101 @@
/*
* Enumerate devices, directories and files.
*
* 2012-10-15, Oliver Schmidt (ol.sc@web.de)
*
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <device.h>
#include <dirent.h>
void printdir (char *newdir)
{
char olddir[FILENAME_MAX];
char curdir[FILENAME_MAX];
DIR *dir;
struct dirent *ent;
char *subdirs = NULL;
unsigned dirnum = 0;
unsigned num;
getcwd (olddir, sizeof (olddir));
if (chdir (newdir)) {
/* If chdir() fails we just print the
* directory name - as done for files.
*/
printf (" Dir %s\n", newdir);
return;
}
/* We call getcwd() in order to print the
* absolute pathname for a subdirectory.
*/
getcwd (curdir, sizeof (curdir));
printf (" Dir %s:\n", curdir);
/* Calling opendir() always with "." avoids
* fiddling around with pathname separators.
*/
dir = opendir (".");
while (ent = readdir (dir)) {
if (_DE_ISREG (ent->d_type)) {
printf (" File %s\n", ent->d_name);
continue;
}
/* We defer handling of subdirectories until we're done with the
* current one as several targets don't support other disk i/o
* while reading a directory (see cc65 readdir() doc for more).
*/
if (_DE_ISDIR (ent->d_type)) {
subdirs = realloc (subdirs, FILENAME_MAX * (dirnum + 1));
strcpy (subdirs + FILENAME_MAX * dirnum++, ent->d_name);
}
}
closedir (dir);
for (num = 0; num < dirnum; ++num) {
printdir (subdirs + FILENAME_MAX * num);
}
free (subdirs);
chdir (olddir);
}
void main (void)
{
unsigned char device;
char devicedir[FILENAME_MAX];
/* Calling getfirstdevice()/getnextdevice() does _not_ turn on the motor
* of a drive-type device and does _not_ check for a disk in the drive.
*/
device = getfirstdevice ();
while (device != INVALID_DEVICE) {
printf ("Device %d:\n", device);
/* Calling getdevicedir() _does_ check for a (formatted) disk in a
* floppy-disk-type device and returns NULL if that check fails.
*/
if (getdevicedir (device, devicedir, sizeof (devicedir))) {
printdir (devicedir);
} else {
printf (" N/A\n");
}
device = getnextdevice (device);
}
cgetc ();
}