diff options
Diffstat (limited to '')
-rw-r--r-- | src/c_lfiles.c | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/c_lfiles.c b/src/c_lfiles.c new file mode 100644 index 0000000..af13a39 --- /dev/null +++ b/src/c_lfiles.c @@ -0,0 +1,161 @@ +/* + * ======================== + * ===== =============== + * ====== ================ + * ====== ================ + * ====== ==== ==== == + * ====== === == = = + * ====== === = == = + * = === === = == ==== + * = === === = == = = + * == ===== ==== == + * ======================== + * + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2020 Joe + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the organization nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOE ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JOE BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * unixize: src/c_lfiles.c + * 2020-11-05 18:05 + * Joe + * + * In this files are functions to handle the files linked list. + */ + +#include <dirent.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "c_unixize.h" +#include "u_utils.h" + +static void +c_lfiles_add_back +(struct lfiles_s** head, + struct lfiles_s* new) +{ + struct lfiles_s* tmp; + + if (*head == NULL) { + *head = new; + return; + } + tmp = *head; + while (tmp->next != NULL) { + tmp = tmp->next; + } + tmp->next = new; +} + +void +c_lfiles_clear(struct lfiles_s** head) +{ + struct lfiles_s* renext; + struct lfiles_s* tmp; + + if (head == NULL) { + return; + } + tmp = *head; + while (tmp != NULL) { + renext = tmp->next; + u_memdel((void*)&tmp->filename); + u_memdel((void*)&tmp); + tmp = renext; + } + *head = NULL; +} + +static struct lfiles_s* +c_lfiles_new +(const char filename[], + unsigned char filetype) +{ + struct lfiles_s* link; + + link = (struct lfiles_s*)malloc(sizeof(struct lfiles_s*)); + if (link == NULL) { + return (NULL); + } + link->filename = strdup(filename); + link->filetype = filetype; + if (link->filename == NULL) { + u_memdel((void*)&link); + return (NULL); + } + link->next = NULL; + return (link); +} + +/* struct lfiles_s* */ +/* c_lfiles_duplicate(struct lfiles_s** head) */ +/* { */ +/* struct lfiles_s* dup; */ +/* struct lfiles_s* origin; */ +/* */ +/* if (head == NULL) { */ +/* return (NULL); */ +/* } */ +/* } */ + +struct lfiles_s* +c_lfiles_gather(void) +{ + DIR* dirp; + struct dirent* dp; + struct lfiles_s* head; + struct lfiles_s* link; + + head = NULL; + link = NULL; + dirp = opendir("."); + if (dirp == NULL) { + dprintf( + STDERR_FILENO, + "unixize: %s\n", + strerror(errno) + ); + return (NULL); + } + while ((dp = readdir(dirp)) != NULL) { + link = c_lfiles_new(dp->d_name, dp->d_type); + if (link == NULL) { + dprintf( + STDERR_FILENO, + "unixize: %s\n", + strerror(errno) + ); + c_lfiles_clear(&link); + return (NULL); + } + c_lfiles_add_back(&head, link); + } + closedir(dirp); + return (head); +} |