/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4dir08.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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. */ /** @file dk4dir08.c The dk4dir08 module. */ #line 146 "dk4dir08.ctr" #include "dk4conf.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #endif #include "dk4dir08.h" #ifndef DK4DIR_H_INCLUDED #include "dk4dir.h" #endif #ifndef DK4STR8_H_INCLUDED #include "dk4str8.h" #endif #ifndef DK4STAT_H_INCLUDED #include "dk4stat.h" #endif #ifndef DK4STAT8_H_INCLUDED #include "dk4stat8.h" #endif #ifndef DK4MEM_H_INCLUDED #include "dk4mem.h" #endif #ifndef DK4MPL_H_INCLUDED #include "dk4mpl.h" #endif #if DK4_HAVE_SYS_TYPES_H #ifndef SYS_TYPES_H_INCLUDED #include #define SYS_TYPES_H_INCLUDED 1 #endif #endif #if DK4_HAVE_DIRENT_H #ifndef DIRENT_H_INCLUDED #include #define DIRENT_H_INCLUDED 1 #endif #endif #if DK4_HAVE_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_H_INCLUDED 1 #endif #endif #line 206 "dk4dir08.ctr" int dk4dir_c8_compare(const void *l, const void *r, int cr) { int back = 0; if (NULL != l) { if (NULL != r) { back = dk4str8_pathcmp((const char *)l, (const char *)r); } else { back = 1; } } else { if (NULL != r) { back = -1; } } return back; } static void dk4dir_c8_clean_storage(dk4_sto_t *s, dk4_sto_it_t *i) { char *n; if (NULL != s) { if (NULL != i) { dk4sto_it_reset(i); while (NULL != (n = (char *)dk4sto_it_next(i))) { dk4mem_free(n); } dk4sto_it_close(i); } dk4sto_close(s); } } void dk4dir_c8_close(dk4_dir_c8_t *dptr) { #line 250 "dk4dir08.ctr" if (NULL != dptr) { dk4mem_release(dptr->path); dk4dir_c8_clean_storage(dptr->s_dir, dptr->i_dir); dptr->s_dir = NULL; dptr->i_dir = NULL; dk4dir_c8_clean_storage(dptr->s_file, dptr->i_file); dptr->s_file = NULL; dptr->i_file = NULL; dk4mem_free(dptr); } #line 259 "dk4dir08.ctr" } #if DK4_ON_WINDOWS || DK4_HAVE_DIRENT_H /** Fixed texts used by the module. */ static const char * const dk4dir_c8_kw[] = { /* 0 */ ".", /* 1 */ "..", /* 2 */ "/", /* 3 */ "\\", /* 4 */ "*", NULL #line 289 "dk4dir08.ctr" }; static int dk4dir_c8_fill_directory(dk4_dir_c8_t *dptr, int om, dk4_er_t *erp) { #if DK4_ON_WINDOWS char pab[DK4_MAX_PATH]; /* Pattern buffer */ WIN32_FIND_DATAA ffdata; /* Find result */ HANDLE ffres; /* Result from FindFirstFile */ DWORD attr; /* File attributes */ char *nn; /* Copy of name, dyn */ size_t slen; /* Entry name length */ int isdir; /* Flag: Entry is directory */ int back = 0; #line 307 "dk4dir08.ctr" if (dk4str8_cpy_s(pab, sizeof(pab), dptr->path, erp)) { if (dk4str8_cat_s(pab, sizeof(pab), dk4dir_c8_kw[3], erp)) { if (dk4str8_cat_s(pab, sizeof(pab), dk4dir_c8_kw[4], erp)) { ffres = FindFirstFileA(pab, &ffdata); if (INVALID_HANDLE_VALUE != ffres) { back = 1; do { if (0 != dk4str8_cmp(dk4dir_c8_kw[0], ffdata.cFileName)) { if (0 != dk4str8_cmp(dk4dir_c8_kw[1], ffdata.cFileName)) { slen = dk4str8_len(ffdata.cFileName); if (slen > dptr->maxlen) { dptr->maxlen = slen; } nn = dk4str8_dup(ffdata.cFileName, erp); if (NULL != nn) { isdir = 0; attr = ffdata.dwFileAttributes; if (0 != (FILE_ATTRIBUTE_DIRECTORY & attr)) { isdir = 1; if (0 != (FILE_ATTRIBUTE_REPARSE_POINT & attr)) { if (0 != (DK4_DIR_SYMLINK_DIR_AS_FILE & om)) { isdir = 0; } } } if (0 != isdir) { if (0 == dk4sto_add(dptr->s_dir, (void *)nn, erp)) { back = 0; dk4mem_free(nn); } } else { if (0 == dk4sto_add(dptr->s_file, (void *)nn, erp)) { back = 0; dk4mem_free(nn); } } } else { back = 0; } } } } while(FindNextFileA(ffres, &ffdata)); FindClose(ffres); } else { /* ERROR: FindFirstFile failed */ dk4error_set_ldetails( erp, DK4_E_FINDFIRSTFILE_FAILED, (long)((unsigned long)GetLastError()) ); } } } } #line 359 "dk4dir08.ctr" return back; #else char fnb[DK4_MAX_PATH]; /* File name buffer */ dk4_stat_t stb; /* Stat buffer */ DIR *dir; /* Directory */ struct dirent *de; /* Directory entry */ char *nn; /* Dynamic copy of file name */ size_t slen; /* Entry name length */ int isdir = 0; /* Flag: Current item is directory */ int back = 0; #line 370 "dk4dir08.ctr" errno = 0; dir = opendir(dptr->path); if (NULL != dir) { #line 373 "dk4dir08.ctr" back = 1; do { de = readdir(dir); if (NULL != de) { if (0 < (slen = dk4str8_len(de->d_name))) { if (slen > dptr->maxlen) { dptr->maxlen = slen; } if (0 != dk4str8_cmp(dk4dir_c8_kw[0], de->d_name)) { if (0 != dk4str8_cmp(dk4dir_c8_kw[1], de->d_name)) { if (dk4str8_cpy_s(fnb, sizeof(fnb), dptr->path, erp)) { if (dk4str8_cat_s(fnb, sizeof(fnb), dk4dir_c8_kw[2], erp)) { if (dk4str8_cat_s(fnb, sizeof(fnb), de->d_name, erp)) { nn = dk4str8_dup(de->d_name, erp); if (NULL != nn) { isdir = 0; if (0 != dk4stat_c8(&stb, fnb, erp)) { isdir = dk4stat_is_directory(&stb, NULL); if (0 != isdir) { if (0 != (DK4_DIR_SYMLINK_DIR_AS_FILE & om)) { if (0 != dk4stat_l_c8(&stb, fnb, NULL)) { if (dk4stat_is_symlink(&stb, NULL)) { isdir = 0; } } } } } else { #line 399 "dk4dir08.ctr" #if 0 /* 2015-04-11 Dangling links are not directories. */ back = 0; #endif } if (0 != isdir) { if (0 == dk4sto_add(dptr->s_dir, (void *)nn, erp)) { back = 0; #line 409 "dk4dir08.ctr" dk4mem_free(nn); } } else { if (0 == dk4sto_add(dptr->s_file, (void *)nn, erp)) { back = 0; #line 414 "dk4dir08.ctr" dk4mem_free(nn); } } } else { #line 418 "dk4dir08.ctr" back = 0; } } else { #line 421 "dk4dir08.ctr" back = 0; } } else { #line 424 "dk4dir08.ctr" back = 0; } } else { #line 427 "dk4dir08.ctr" back = 0; } } } } } } while(NULL != de); closedir(dir); } else { #line 436 "dk4dir08.ctr" dk4error_set_idetails(erp, DK4_E_OPENDIR_FAILED, errno); } #line 438 "dk4dir08.ctr" return back; #endif } #endif dk4_dir_c8_t * dk4dir_c8_open(const char *path, int om, dk4_er_t *erp) { dk4_dir_c8_t *back = NULL; int ok = 0; #line 451 "dk4dir08.ctr" if (NULL != path) { #if DK4_ON_WINDOWS || DK4_HAVE_DIRENT_H back = dk4mem_new(dk4_dir_c8_t, 1, erp); if (NULL != back) { back->path = NULL; back->s_dir = NULL; back->i_dir = NULL; back->s_file = NULL; back->i_file = NULL; back->path = dk4str8_dup(path, erp); back->maxlen = 0; if (NULL != back->path) { back->s_dir = dk4sto_open(erp); if (NULL != back->s_dir) { if (0 != (DK4_DIR_OPEN_SORTED & om)) { dk4sto_set_comp(back->s_dir, dk4dir_c8_compare, 0); } back->i_dir = dk4sto_it_open(back->s_dir, erp); if (NULL != back->i_dir) { back->s_file = dk4sto_open(erp); if (NULL != back->s_file) { if (0 != (DK4_DIR_OPEN_SORTED & om)) { dk4sto_set_comp(back->s_file, dk4dir_c8_compare, 0); } back->i_file = dk4sto_it_open(back->s_file, erp); if (NULL != back->i_file) { ok = dk4dir_c8_fill_directory(back, om, erp); } } } } } if (0 == ok) { dk4dir_c8_close(back); back = NULL; } } #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); #endif } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 495 "dk4dir08.ctr" return back; } void dk4dir_c8_reset(dk4_dir_c8_t *dptr) { #line 504 "dk4dir08.ctr" if (NULL != dptr) { if ((NULL != dptr->s_dir) && (NULL != dptr->i_dir)) { dk4sto_it_reset(dptr->i_dir); } if ((NULL != dptr->s_file) && (NULL != dptr->i_file)) { dk4sto_it_reset(dptr->i_file); } } #line 513 "dk4dir08.ctr" } const char * dk4dir_c8_get_path(dk4_dir_c8_t *dptr) { const char *back = NULL; #line 522 "dk4dir08.ctr" if (NULL != dptr) { back = (const char *)(dptr->path); } #line 526 "dk4dir08.ctr" return back; } const char * dk4dir_c8_next_dir(dk4_dir_c8_t *dptr) { const char *back = NULL; #line 536 "dk4dir08.ctr" if (NULL != dptr) { if ((NULL != dptr->s_dir) && (NULL != dptr->i_dir)) { back = (const char *)dk4sto_it_next(dptr->i_dir); } } #line 542 "dk4dir08.ctr" return back; } const char * dk4dir_c8_next_file(dk4_dir_c8_t *dptr) { const char *back = NULL; #line 552 "dk4dir08.ctr" if (NULL != dptr) { if ((NULL != dptr->s_file) && (NULL != dptr->i_file)) { back = (const char *)dk4sto_it_next(dptr->i_file); } } #line 558 "dk4dir08.ctr" return back; } void dk4dir_c8_skip_files(dk4_dir_c8_t *dptr) { #line 567 "dk4dir08.ctr" if (NULL != dptr) { dk4dir_c8_clean_storage(dptr->s_file, dptr->i_file); dptr->s_file = NULL; dptr->i_file = NULL; } #line 572 "dk4dir08.ctr" } size_t dk4dir_c8_get_max_entry_length(dk4_dir_c8_t *dptr) { size_t back = 0; #line 581 "dk4dir08.ctr" if (NULL != dptr) { back = dptr->maxlen; } #line 585 "dk4dir08.ctr" return back; }