enumdevdir.c: allocate path name buffers from the heap.

This commit is contained in:
Christian Groessler
2021-06-10 17:15:29 +02:00
committed by Oliver Schmidt
parent c90c3c9133
commit 7f1f0249f3

View File

@@ -16,31 +16,47 @@
#include <cc65.h> #include <cc65.h>
void printdir (char *newdir) int printdir (char *newdir)
{ {
char olddir[FILENAME_MAX]; char *olddir;
char curdir[FILENAME_MAX]; char *curdir;
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
char *subdirs = NULL; char *subdirs = NULL;
unsigned dirnum = 0; unsigned dirnum = 0;
unsigned num; unsigned num;
getcwd (olddir, sizeof (olddir)); olddir = malloc (FILENAME_MAX);
if (olddir != NULL) {
perror ("cannot allocate memory");
return 1;
}
getcwd (olddir, FILENAME_MAX);
if (chdir (newdir)) { if (chdir (newdir)) {
/* If chdir() fails we just print the /* If chdir() fails we just print the
** directory name - as done for files. ** directory name - as done for files.
*/ */
printf (" Dir %s\n", newdir); printf (" Dir %s\n", newdir);
return; free (olddir);
return 0;
}
curdir = malloc (FILENAME_MAX);
if (curdir != NULL) {
perror ("cannot allocate memory");
return 1;
} }
/* We call getcwd() in order to print the /* We call getcwd() in order to print the
** absolute pathname for a subdirectory. ** absolute pathname for a subdirectory.
*/ */
getcwd (curdir, sizeof (curdir)); getcwd (curdir, FILENAME_MAX);
printf (" Dir %s:\n", curdir); printf (" Dir %s:\n", curdir);
free (curdir);
/* Calling opendir() always with "." avoids /* Calling opendir() always with "." avoids
** fiddling around with pathname separators. ** fiddling around with pathname separators.
@@ -65,18 +81,28 @@ void printdir (char *newdir)
closedir (dir); closedir (dir);
for (num = 0; num < dirnum; ++num) { for (num = 0; num < dirnum; ++num) {
printdir (subdirs + FILENAME_MAX * num); if (printdir (subdirs + FILENAME_MAX * num))
break;
} }
free (subdirs); free (subdirs);
chdir (olddir); chdir (olddir);
free (olddir);
return 0;
} }
void main (void) void main (void)
{ {
unsigned char device; unsigned char device;
char devicedir[FILENAME_MAX]; char *devicedir;
devicedir = malloc (FILENAME_MAX);
if (devicedir != NULL) {
perror ("cannot allocate memory");
return;
}
/* Calling getfirstdevice()/getnextdevice() does _not_ turn on the motor /* Calling getfirstdevice()/getnextdevice() does _not_ turn on the motor
** of a drive-type device and does _not_ check for a disk in the drive. ** of a drive-type device and does _not_ check for a disk in the drive.
@@ -88,7 +114,7 @@ void main (void)
/* Calling getdevicedir() _does_ check for a (formatted) disk in a /* Calling getdevicedir() _does_ check for a (formatted) disk in a
** floppy-disk-type device and returns NULL if that check fails. ** floppy-disk-type device and returns NULL if that check fails.
*/ */
if (getdevicedir (device, devicedir, sizeof (devicedir))) { if (getdevicedir (device, devicedir, FILENAME_MAX)) {
printdir (devicedir); printdir (devicedir);
} else { } else {
printf (" N/A\n"); printf (" N/A\n");
@@ -100,4 +126,6 @@ void main (void)
if (doesclrscrafterexit ()) { if (doesclrscrafterexit ()) {
getchar (); getchar ();
} }
free (devicedir);
} }