Path: blob/main/crypto/krb5/src/util/support/dir_filenames.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/dir_filenames.c - fetch filenames in a directory */2/*3* Copyright (C) 2018 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132#include "k5-platform.h"3334void35k5_free_filenames(char **fnames)36{37char **fn;3839for (fn = fnames; fn != NULL && *fn != NULL; fn++)40free(*fn);41free(fnames);42}4344/* Resize the filename list and add a name. */45static int46add_filename(char ***fnames, int *n_fnames, const char *name)47{48char **newlist;4950newlist = realloc(*fnames, (*n_fnames + 2) * sizeof(*newlist));51if (newlist == NULL)52return ENOMEM;53*fnames = newlist;54newlist[*n_fnames] = strdup(name);55if (newlist[*n_fnames] == NULL)56return ENOMEM;57(*n_fnames)++;58newlist[*n_fnames] = NULL;59return 0;60}6162static int63compare_with_strcmp(const void *a, const void *b)64{65return strcmp(*(char **)a, *(char **)b);66}6768#ifdef _WIN326970int71k5_dir_filenames(const char *dirname, char ***fnames_out)72{73char *wildcard;74WIN32_FIND_DATA ffd;75HANDLE handle;76char **fnames = NULL;77int n_fnames = 0;7879*fnames_out = NULL;8081if (asprintf(&wildcard, "%s\\*", dirname) < 0)82return ENOMEM;83handle = FindFirstFile(wildcard, &ffd);84free(wildcard);85if (handle == INVALID_HANDLE_VALUE)86return ENOENT;8788do {89if (add_filename(&fnames, &n_fnames, ffd.cFileName) != 0) {90k5_free_filenames(fnames);91FindClose(handle);92return ENOMEM;93}94} while (FindNextFile(handle, &ffd) != 0);9596FindClose(handle);97qsort(fnames, n_fnames, sizeof(*fnames), compare_with_strcmp);98*fnames_out = fnames;99return 0;100}101102#else /* _WIN32 */103104#include <dirent.h>105106int107k5_dir_filenames(const char *dirname, char ***fnames_out)108{109DIR *dir;110struct dirent *ent;111char **fnames = NULL;112int n_fnames = 0;113114*fnames_out = NULL;115116dir = opendir(dirname);117if (dir == NULL)118return ENOENT;119120while ((ent = readdir(dir)) != NULL) {121if (add_filename(&fnames, &n_fnames, ent->d_name) != 0) {122k5_free_filenames(fnames);123closedir(dir);124return ENOMEM;125}126}127128closedir(dir);129qsort(fnames, n_fnames, sizeof(*fnames), compare_with_strcmp);130*fnames_out = fnames;131return 0;132}133134#endif /* not _WIN32 */135136137