From 170c59e4c22b0008f31f01f292deca0ecd7b160b Mon Sep 17 00:00:00 2001
From: "ol.sc" 
Date: Mon, 15 Oct 2012 18:52:40 +0000
Subject: [PATCH] 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
---
 samples/Makefile     |  31 ++++++-------
 samples/README       |  50 +++++++++++----------
 samples/enumdevdir.c | 101 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+), 37 deletions(-)
 create mode 100644 samples/enumdevdir.c
diff --git a/samples/Makefile b/samples/Makefile
index 4499586ce..046eb8fc9 100644
--- a/samples/Makefile
+++ b/samples/Makefile
@@ -48,7 +48,7 @@ MY_ASM = --forget-inc-paths -I . -I $(CA65_INC)
 endif
 
 # This one comes with VICE
-C1541  	= c1541
+C1541	= c1541
 
 
 # --------------------------------------------------------------------------
@@ -71,25 +71,26 @@ C1541  	= c1541
 # List of executables. This list could be made target dependent by checking
 # $(SYS).
 
-EXELIST	=	ascii 		\
-     		diodemo		\
-     		fire 		\
-     		gunzip65 	\
-     		hello 		\
+EXELIST	=	ascii		\
+		diodemo		\
+		enumdevdir	\
+		fire		\
+		gunzip65	\
+		hello		\
 		mandelbrot	\
-     		mousedemo 	\
-     		multdemo 	\
-     		nachtm 		\
-     		ovrldemo	\
-     		plasma 		\
-     		sieve 		\
-     		tgidemo
+		mousedemo	\
+		multdemo	\
+		nachtm		\
+		ovrldemo	\
+		plasma		\
+		sieve		\
+		tgidemo
 
 # --------------------------------------------------------------------------
 # Rules how to make each one of the binaries
 
 .PHONY:	all
-all:   	$(EXELIST)
+all:	$(EXELIST)
 
 ascii:		ascii.o
 
@@ -152,7 +153,7 @@ endif
 # with the VICE emulator.
 
 .PHONY:	disk
-disk:  	samples.d64
+disk:	samples.d64
 
 samples.d64:	all
 	@$(C1541) -format samples,AA  d64 $@ > /dev/null
diff --git a/samples/README b/samples/README
index 4310159d5..4d677fa1a 100644
--- a/samples/README
+++ b/samples/README
@@ -19,42 +19,49 @@ Please note:
 List of supplied sample programs:
 
 -----------------------------------------------------------------------------
-Name:           ascii
+Name:		ascii
 Description:	Shows the ASCII (or ATASCII, PETSCII) codes of typed
 		characters. Written and contributed by Greg King
 		.
-Platforms:     	All platforms with conio or stdio (compile time configurable).
+Platforms:	All platforms with conio or stdio (compile time configurable).
 
 -----------------------------------------------------------------------------
-Name:           diodemo
+Name:		diodemo
 Description:	A disc copy program written and contributed by Oliver
 		Schmidt, . Supports single or dual disc copy.
-Platforms:     	The program does depend on conio and dio (direct disk i/o),
+Platforms:	The program does depend on conio and dio (direct disk i/o),
 		so it does currently compile for the Atari and Apple ][
 		machines.
 
 -----------------------------------------------------------------------------
-Name:           fire
+Name:		enumdevdir
+Description:	Enumerates all devices, directories and files. Written and
+		contributed by Oliver Schmidt, .
+Platforms:	All systems with device enumeration and directory access
+		(currently the C64, the C128 and the Apple ][).
+
+-----------------------------------------------------------------------------
+Name:		fire
 Description:	Another graphics demo written by groepaz/hitmen.
-Platforms:     	The program is currently only running on the C64, but should
+Platforms:	The program is currently only running on the C64, but should
 		be portable to the C128 and CBM510 (and maybe more machines).
 
 -----------------------------------------------------------------------------
-Name:           gunzip65
+Name:		gunzip65
 Description:	A gunzip utility for 6502 based machines written by Piotr
 		Fusik .
-Platforms:     	Runs on all platforms with file I/O (currently the Atari, the
+Platforms:	Runs on all platforms with file I/O (currently the Atari, the
 		Apple ][ and most Commodore machines).
 
 -----------------------------------------------------------------------------
-Name:	        hello
+Name:		hello
 Description:	A nice "Hello world" type program that uses the conio
 		console I/O library for output.
 Platforms:	Runs on all platforms that support conio, which means:
 		Apple ][, Atari, C16, C64, C128, CBM510, CBM610, PET, Plus/4
 
 -----------------------------------------------------------------------------
-Name:           mandelbrot
+Name:		mandelbrot
 Description:	A mandelbrot demo using integer arithmetic. The demo was
 		written by groepaz/hitmen and converted to cc65 using TGI
 		graphics by Stephan Haubenthal.
@@ -62,43 +69,43 @@ Platforms:	Runs on all platforms that have TGI support:
 		Apple ][, C64, C128, Oric Atmos, Geos and Lynx.
 
 -----------------------------------------------------------------------------
-Name:           mousedemo
+Name:		mousedemo
 Description:	Shows how to use the mouse.
-Platforms:     	All systems with mouse and conio support:
+Platforms:	All systems with mouse and conio support:
 		C64, C128, CBM510, Atari, Apple ][
 
 -----------------------------------------------------------------------------
-Name:           multidemo
+Name:		multidemo
 Description:	Shows how to combine multiple cc65 features incl. overlays
 		and extended memory drivers. Written and contributed by
 		Oliver Schmidt, .
-Platforms:     	All systems with an overlay linker config, disk directory
+Platforms:	All systems with an overlay linker config, disk directory
 		access and EMD support (currently the C64, the C128,
 		the Atari and the Apple ][).
 
 -----------------------------------------------------------------------------
-Name:           nachtm
+Name:		nachtm
 Description:	Plays "Eine kleine Nachtmusik" by Wolfgang Amadeus Mozart
 Platforms:	All systems that have the Commodore SID (Sound Interface
 		Device):
 		C64, C128, CBM510, CBM610
 
 -----------------------------------------------------------------------------
-Name:           overlaydemo
+Name:		overlaydemo
 Description:	Shows how to load overlay files from disk. Written and
 		contributed by Oliver Schmidt, .
-Platforms:     	All systems with an overlay linker config (currently the C64,
+Platforms:	All systems with an overlay linker config (currently the C64,
 		the C128, the Atari and the Apple ][).
 
 -----------------------------------------------------------------------------
-Name:           plasma
+Name:		plasma
 Description:	A fancy graphics demo written by groepaz/hitmen.
-Platforms:     	The program needs a VIC, or a TED, so it runs on the following
+Platforms:	The program needs a VIC, or a TED, so it runs on the following
 		systems:
 		C64, C128, CBM510, Plus/4
 
 -----------------------------------------------------------------------------
-Name:           sieve
+Name:		sieve
 Description:	Implements the "Sieve of Eratosthenes" as a way to find all
 		prime numbers in a specific number interval. Often used as
 		a benchmark program.
@@ -107,9 +114,8 @@ Platforms:	All systems with conio and clock support:
 		Apple ][ (without timing due to missing clock support)
 
 -----------------------------------------------------------------------------
-Name:           tgidemo
+Name:		tgidemo
 Description:	Shows some of the graphics capabilities of the "tiny graphics
 		interface".
 Platforms:	Runs on all platforms that have TGI support:
 		Apple ][, C64, C128, Oric Atmos, Geos and Lynx.
-
diff --git a/samples/enumdevdir.c b/samples/enumdevdir.c
new file mode 100644
index 000000000..5969c2339
--- /dev/null
+++ b/samples/enumdevdir.c
@@ -0,0 +1,101 @@
+/*
+ * Enumerate devices, directories and files.
+ *
+ * 2012-10-15, Oliver Schmidt (ol.sc@web.de)
+ *
+ */
+
+
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+
+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 ();
+}
-- 
2.39.5