[331] | 1 | /*
|
---|
| 2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
| 11 | * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
|
---|
| 12 | * All rights reserved.
|
---|
| 13 | *
|
---|
| 14 | * Redistribution and use in source and binary forms, with or without
|
---|
| 15 | * modification, are permitted provided that the following conditions
|
---|
| 16 | * are met:
|
---|
| 17 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 18 | * notice, this list of conditions and the following disclaimer.
|
---|
| 19 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 20 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 21 | * documentation and/or other materials provided with the distribution.
|
---|
| 22 | *
|
---|
| 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 24 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <stddef.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
| 38 | #include <limits.h>
|
---|
| 39 | #include <string.h>
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include <dirent.h>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #ifndef LPDIR_H
|
---|
| 44 | # include "LPdir.h"
|
---|
| 45 | #endif
|
---|
| 46 |
|
---|
| 47 | /*
|
---|
| 48 | * The POSIXly macro for the maximum number of characters in a file path is
|
---|
| 49 | * NAME_MAX. However, some operating systems use PATH_MAX instead.
|
---|
| 50 | * Therefore, it seems natural to first check for PATH_MAX and use that, and
|
---|
| 51 | * if it doesn't exist, use NAME_MAX.
|
---|
| 52 | */
|
---|
| 53 | #if defined(PATH_MAX)
|
---|
| 54 | # define LP_ENTRY_SIZE PATH_MAX
|
---|
| 55 | #elif defined(NAME_MAX)
|
---|
| 56 | # define LP_ENTRY_SIZE NAME_MAX
|
---|
| 57 | #endif
|
---|
| 58 |
|
---|
| 59 | /*
|
---|
| 60 | * Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
|
---|
| 61 | * exist. It's also possible that NAME_MAX exists but is define to a very
|
---|
| 62 | * small value (HP-UX offers 14), so we need to check if we got a result, and
|
---|
| 63 | * if it meets a minimum standard, and create or change it if not.
|
---|
| 64 | */
|
---|
| 65 | #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
|
---|
| 66 | # undef LP_ENTRY_SIZE
|
---|
| 67 | # define LP_ENTRY_SIZE 255
|
---|
| 68 | #endif
|
---|
| 69 |
|
---|
| 70 | struct LP_dir_context_st {
|
---|
| 71 | DIR *dir;
|
---|
| 72 | char entry_name[LP_ENTRY_SIZE + 1];
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
|
---|
| 76 | {
|
---|
| 77 | struct dirent *direntry = NULL;
|
---|
| 78 |
|
---|
| 79 | if (ctx == NULL || directory == NULL) {
|
---|
| 80 | errno = EINVAL;
|
---|
| 81 | return 0;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | errno = 0;
|
---|
| 85 | if (*ctx == NULL) {
|
---|
| 86 | *ctx = malloc(sizeof(**ctx));
|
---|
| 87 | if (*ctx == NULL) {
|
---|
| 88 | errno = ENOMEM;
|
---|
| 89 | return 0;
|
---|
| 90 | }
|
---|
| 91 | memset(*ctx, 0, sizeof(**ctx));
|
---|
| 92 |
|
---|
| 93 | (*ctx)->dir = opendir(directory);
|
---|
| 94 | if ((*ctx)->dir == NULL) {
|
---|
| 95 | int save_errno = errno; /* Probably not needed, but I'm paranoid */
|
---|
| 96 | free(*ctx);
|
---|
| 97 | *ctx = NULL;
|
---|
| 98 | errno = save_errno;
|
---|
| 99 | return 0;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | direntry = readdir((*ctx)->dir);
|
---|
| 104 | if (direntry == NULL) {
|
---|
| 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | strncpy((*ctx)->entry_name, direntry->d_name,
|
---|
| 109 | sizeof((*ctx)->entry_name) - 1);
|
---|
| 110 | (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
|
---|
| 111 | return (*ctx)->entry_name;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | int LP_find_file_end(LP_DIR_CTX **ctx)
|
---|
| 115 | {
|
---|
| 116 | if (ctx != NULL && *ctx != NULL) {
|
---|
| 117 | int ret = closedir((*ctx)->dir);
|
---|
| 118 |
|
---|
| 119 | free(*ctx);
|
---|
| 120 | switch (ret) {
|
---|
| 121 | case 0:
|
---|
| 122 | return 1;
|
---|
| 123 | case -1:
|
---|
| 124 | return 0;
|
---|
| 125 | default:
|
---|
| 126 | break;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | errno = EINVAL;
|
---|
| 130 | return 0;
|
---|
| 131 | }
|
---|