Commit 2ac39ba7 authored by Dominik Widhalm's avatar Dominik Widhalm
Browse files

Improved example solution for task 6.06

parent 6d9e9a36
......@@ -51,10 +51,10 @@ typedef struct song {
/***** FUNCTION PROTOTYPES ****************************************************/
song_t *songs_new (char *file);
int songs_scan (song_t **head, char *dir);
int songs_scan (song_t **head, const char *dir);
void songs_print (song_t *head);
void songs_shuffle (song_t **head);
void swap (long int *a, long int *b) ;
void swap (song_t **a, song_t **b) ;
/***** LOCAL FUNCTIONS ********************************************************/
......@@ -165,7 +165,7 @@ song_t *songs_new (char *file) {
* @retval 1 Directory can be accessed *
* @retval 0 Directory cannot be accessed *
******************************************************************************/
int songs_scan (song_t **head, char *dir) {
int songs_scan (song_t **head, const char *dir) {
/* Temporary song pointer for newly created element */
song_t *new = NULL;
/* Temporary song pointer for traversing the list */
......@@ -186,7 +186,20 @@ int songs_scan (song_t **head, char *dir) {
return 0;
}
/* Scan the given directory */
while ((file = readdir(fd)) && (read_songs < SONGS_MAX)) {
while (fd && (read_songs < SONGS_MAX)) {
/* Local variable for error handling */
int errno = 0;
/* Check if there are files in the directory */
if ((file = readdir(fd)) == NULL) {
/* Close the directory handle */
closedir(fd);
/* Check if readdir resulted in an error */
if (errno != 0) {
return 0;
}
/* Return to calling function */
return 1;
}
/* Ignore the current and parent directories */
if ((!strcmp(file->d_name, ".")) || (!strcmp(file->d_name, ".."))) {
/* Continue with next directory entry */
......@@ -276,7 +289,7 @@ void songs_print (song_t *head) {
******************************************************************************/
void songs_shuffle (song_t **head) {
/* Array to store the entry's addresses */
long int addresses[SONGS_MAX] = {-1};
song_t *addresses[SONGS_MAX] = {NULL};
/* Temporary variable for the number of entries */
int entries = 0;
/* Temporary pointer to traverse the list */
......@@ -284,7 +297,7 @@ void songs_shuffle (song_t **head) {
/* Go through the list and store the addresses */
while (curr != NULL) {
/* Store the current address */
addresses[entries] = (long int)curr;
addresses[entries] = curr;
/* Increment number of entries */
entries++;
/* Go to next element */
......@@ -302,7 +315,7 @@ void songs_shuffle (song_t **head) {
/* Check if it's currently the first element */
if (i==0) {
/* Set head to the new first element */
*head = (song_t*)addresses[i];
*head = addresses[i];
/* Set next initially to NULL */
(*head)->next = NULL;
/* Save head as new current element */
......@@ -332,9 +345,9 @@ void songs_shuffle (song_t **head) {
* @param a Pointer to the first long int variable *
* @param b Pointer to the second long int variable *
******************************************************************************/
void swap (long int *a, long int *b) {
void swap (song_t **a, song_t **b) {
/* Do a triangular exchange */
long int tmp = *a;
song_t *tmp = *a;
*a = *b;
*b = tmp;
/* A void function has nothing to return */
......@@ -345,7 +358,7 @@ void swap (long int *a, long int *b) {
/***** MAIN ROUTINE ***********************************************************/
int main (void) {
/*** Local Variables ***/
song_t *head = NULL;
static song_t *head = NULL;
song_t *tmp;
char path[STRING_MAX];
......@@ -356,6 +369,7 @@ int main (void) {
printf("Please enter the name of the directory to scan: ");
/* Read in the user input */
fgets(path,STRING_MAX,stdin);
path[strlen(path)-1] = '\0';
/* Scan directory and read in the songs */
if (songs_scan(&head,path) == 0) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment