2012-10-15 18:52:40 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** Enumerate devices, directories and files.
|
|
|
|
**
|
|
|
|
** 2012-10-15, Oliver Schmidt (ol.sc@web.de)
|
|
|
|
**
|
|
|
|
*/
|
2012-10-15 18:52:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2021-06-10 16:11:45 +00:00
|
|
|
#include <stdbool.h>
|
2012-10-15 18:52:40 +00:00
|
|
|
#include <device.h>
|
|
|
|
#include <dirent.h>
|
2016-06-18 21:35:57 +00:00
|
|
|
#include <cc65.h>
|
2012-10-15 18:52:40 +00:00
|
|
|
|
|
|
|
|
2021-06-10 16:11:45 +00:00
|
|
|
/* returns true for error, false for OK */
|
|
|
|
bool printdir (char *newdir)
|
2012-10-15 18:52:40 +00:00
|
|
|
{
|
2021-06-10 15:15:29 +00:00
|
|
|
char *olddir;
|
|
|
|
char *curdir;
|
2012-10-15 18:52:40 +00:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
char *subdirs = NULL;
|
|
|
|
unsigned dirnum = 0;
|
|
|
|
unsigned num;
|
|
|
|
|
2021-06-10 15:15:29 +00:00
|
|
|
olddir = malloc (FILENAME_MAX);
|
2021-06-10 16:11:45 +00:00
|
|
|
if (olddir == NULL) {
|
2021-06-10 15:15:29 +00:00
|
|
|
perror ("cannot allocate memory");
|
2021-06-10 16:11:45 +00:00
|
|
|
return true;
|
2021-06-10 15:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getcwd (olddir, FILENAME_MAX);
|
2012-10-15 18:52:40 +00:00
|
|
|
if (chdir (newdir)) {
|
|
|
|
|
|
|
|
/* If chdir() fails we just print the
|
2014-06-30 09:10:35 +00:00
|
|
|
** directory name - as done for files.
|
|
|
|
*/
|
2012-10-15 18:52:40 +00:00
|
|
|
printf (" Dir %s\n", newdir);
|
2021-06-10 15:15:29 +00:00
|
|
|
free (olddir);
|
2021-06-10 16:11:45 +00:00
|
|
|
return false;
|
2021-06-10 15:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curdir = malloc (FILENAME_MAX);
|
2021-06-10 16:11:45 +00:00
|
|
|
if (curdir == NULL) {
|
2021-06-10 15:15:29 +00:00
|
|
|
perror ("cannot allocate memory");
|
2021-06-10 16:11:45 +00:00
|
|
|
return true;
|
2012-10-15 18:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We call getcwd() in order to print the
|
2014-06-30 09:10:35 +00:00
|
|
|
** absolute pathname for a subdirectory.
|
|
|
|
*/
|
2021-06-10 15:15:29 +00:00
|
|
|
getcwd (curdir, FILENAME_MAX);
|
2012-10-15 18:52:40 +00:00
|
|
|
printf (" Dir %s:\n", curdir);
|
2021-06-10 15:15:29 +00:00
|
|
|
free (curdir);
|
2012-10-15 18:52:40 +00:00
|
|
|
|
|
|
|
/* Calling opendir() always with "." avoids
|
2014-06-30 09:10:35 +00:00
|
|
|
** fiddling around with pathname separators.
|
|
|
|
*/
|
2012-10-15 18:52:40 +00:00
|
|
|
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
|
2014-06-30 09:10:35 +00:00
|
|
|
** current one as several targets don't support other disk i/o
|
|
|
|
** while reading a directory (see cc65 readdir() doc for more).
|
|
|
|
*/
|
2012-10-15 18:52:40 +00:00
|
|
|
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) {
|
2021-06-10 15:15:29 +00:00
|
|
|
if (printdir (subdirs + FILENAME_MAX * num))
|
|
|
|
break;
|
2012-10-15 18:52:40 +00:00
|
|
|
}
|
|
|
|
free (subdirs);
|
|
|
|
|
|
|
|
chdir (olddir);
|
2021-06-10 15:15:29 +00:00
|
|
|
free (olddir);
|
2021-06-10 16:11:45 +00:00
|
|
|
return false;
|
2012-10-15 18:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main (void)
|
|
|
|
{
|
|
|
|
unsigned char device;
|
2021-06-10 15:15:29 +00:00
|
|
|
char *devicedir;
|
|
|
|
|
|
|
|
devicedir = malloc (FILENAME_MAX);
|
2021-06-10 16:11:45 +00:00
|
|
|
if (devicedir == NULL) {
|
2021-06-10 15:15:29 +00:00
|
|
|
perror ("cannot allocate memory");
|
|
|
|
return;
|
|
|
|
}
|
2012-10-15 18:52:40 +00:00
|
|
|
|
|
|
|
/* Calling getfirstdevice()/getnextdevice() does _not_ turn on the motor
|
2014-06-30 09:10:35 +00:00
|
|
|
** of a drive-type device and does _not_ check for a disk in the drive.
|
|
|
|
*/
|
2012-10-15 18:52:40 +00:00
|
|
|
device = getfirstdevice ();
|
|
|
|
while (device != INVALID_DEVICE) {
|
|
|
|
printf ("Device %d:\n", device);
|
|
|
|
|
|
|
|
/* Calling getdevicedir() _does_ check for a (formatted) disk in a
|
2014-06-30 09:10:35 +00:00
|
|
|
** floppy-disk-type device and returns NULL if that check fails.
|
|
|
|
*/
|
2021-06-10 15:15:29 +00:00
|
|
|
if (getdevicedir (device, devicedir, FILENAME_MAX)) {
|
2012-10-15 18:52:40 +00:00
|
|
|
printdir (devicedir);
|
|
|
|
} else {
|
|
|
|
printf (" N/A\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
device = getnextdevice (device);
|
|
|
|
}
|
|
|
|
|
2016-06-18 21:35:57 +00:00
|
|
|
if (doesclrscrafterexit ()) {
|
|
|
|
getchar ();
|
|
|
|
}
|
2021-06-10 15:15:29 +00:00
|
|
|
|
|
|
|
free (devicedir);
|
2012-10-15 18:52:40 +00:00
|
|
|
}
|