diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-08 20:05:46 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-08 20:05:46 -0900 |
| commit | 32c2e2ddbdc4bc3622caeabf519f1fb4ca45c914 (patch) | |
| tree | d6c7a7dd08c7802e6e950e8b5588f7a00f638065 | |
| parent | 0f00b5d527ec6c2e7f41f2084126bb6035932a2e (diff) | |
| download | tinyrad-32c2e2ddbdc4bc3622caeabf519f1fb4ca45c914.tar.gz tinyrad-32c2e2ddbdc4bc3622caeabf519f1fb4ca45c914.tar.bz2 tinyrad-32c2e2ddbdc4bc3622caeabf519f1fb4ca45c914.tar.xz tinyrad-32c2e2ddbdc4bc3622caeabf519f1fb4ca45c914.zip | |
libtrutils: adding lfile.c
| -rw-r--r-- | Makefile.am | 7 | ||||
| -rw-r--r-- | include/trutils.h | 24 | ||||
| -rw-r--r-- | lib/libtrutils/lfile.c | 192 | ||||
| -rw-r--r-- | lib/libtrutils/lfile.h | 85 |
4 files changed, 304 insertions, 4 deletions
diff --git a/Makefile.am b/Makefile.am index c721036..554af93 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -274,11 +274,10 @@ lib_libtrutils_a_DEPENDENCIES = Makefile \ | |||
| 274 | lib_libtrutils_a_CPPFLAGS = $(AM_CPPFLAGS) \ | 274 | lib_libtrutils_a_CPPFLAGS = $(AM_CPPFLAGS) \ |
| 275 | -I$(srcdir)/lib/libtinyrad | 275 | -I$(srcdir)/lib/libtinyrad |
| 276 | lib_libtrutils_a_CFLAGS = $(AM_CFLAGS) | 276 | lib_libtrutils_a_CFLAGS = $(AM_CFLAGS) |
| 277 | lib_libtrutils_a_SOURCES = include/tinyrad.h \ | 277 | lib_libtrutils_a_SOURCES = $(noinst_HEADERS) $(include_HEADERS) \ |
| 278 | include/tinyrad_internal.h \ | ||
| 279 | include/tinyrad_compat.h \ | ||
| 280 | include/trutils.h \ | ||
| 281 | lib/libtrutils/libtrutils.h \ | 278 | lib/libtrutils/libtrutils.h \ |
| 279 | lib/libtrutils/lfile.c \ | ||
| 280 | lib/libtrutils/lfile.h \ | ||
| 282 | lib/libtrutils/llogging.c \ | 281 | lib/libtrutils/llogging.c \ |
| 283 | lib/libtrutils/llogging.h | 282 | lib/libtrutils/llogging.h |
| 284 | 283 | ||
diff --git a/include/trutils.h b/include/trutils.h index e9d44bf..4c1a033 100644 --- a/include/trutils.h +++ b/include/trutils.h | |||
| @@ -87,6 +87,8 @@ | |||
| 87 | ////////////////// | 87 | ////////////////// |
| 88 | #pragma mark - Data Types | 88 | #pragma mark - Data Types |
| 89 | 89 | ||
| 90 | typedef struct _trutils_file_buffer TRUtilsFile; | ||
| 91 | |||
| 90 | 92 | ||
| 91 | ///////////////// | 93 | ///////////////// |
| 92 | // // | 94 | // // |
| @@ -105,6 +107,28 @@ extern const char * program_name; | |||
| 105 | ////////////////// | 107 | ////////////////// |
| 106 | #pragma mark - Prototypes | 108 | #pragma mark - Prototypes |
| 107 | 109 | ||
| 110 | //-----------------// | ||
| 111 | // file prototypes // | ||
| 112 | //-----------------// | ||
| 113 | #pragma mark file prototypes | ||
| 114 | |||
| 115 | void | ||
| 116 | trutils_close( | ||
| 117 | TRUtilsFile * fb ); | ||
| 118 | |||
| 119 | |||
| 120 | TRUtilsFile * | ||
| 121 | trutils_open( | ||
| 122 | unsigned opts, | ||
| 123 | const char * filename ); | ||
| 124 | |||
| 125 | |||
| 126 | int | ||
| 127 | trutils_readline( | ||
| 128 | unsigned opts, | ||
| 129 | TRUtilsFile * fb ); | ||
| 130 | |||
| 131 | |||
| 108 | //--------------------// | 132 | //--------------------// |
| 109 | // logging prototypes // | 133 | // logging prototypes // |
| 110 | //--------------------// | 134 | //--------------------// |
diff --git a/lib/libtrutils/lfile.c b/lib/libtrutils/lfile.c new file mode 100644 index 0000000..474f961 --- /dev/null +++ b/lib/libtrutils/lfile.c | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | /* | ||
| 2 | * Tiny RADIUS Client Library | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #define _LIB_LIBTRUTILS_LFILE_C 1 | ||
| 32 | #include "lfile.h" | ||
| 33 | |||
| 34 | /////////////// | ||
| 35 | // // | ||
| 36 | // Headers // | ||
| 37 | // // | ||
| 38 | /////////////// | ||
| 39 | #pragma mark - Headers | ||
| 40 | |||
| 41 | #include <time.h> | ||
| 42 | #include <fcntl.h> | ||
| 43 | #include <sys/types.h> | ||
| 44 | #include <sys/stat.h> | ||
| 45 | #include <sys/uio.h> | ||
| 46 | #include <unistd.h> | ||
| 47 | #include <errno.h> | ||
| 48 | #include <assert.h> | ||
| 49 | |||
| 50 | |||
| 51 | ////////////////// | ||
| 52 | // // | ||
| 53 | // Prototypes // | ||
| 54 | // // | ||
| 55 | ////////////////// | ||
| 56 | #pragma mark - Prototypes | ||
| 57 | |||
| 58 | |||
| 59 | ///////////////// | ||
| 60 | // // | ||
| 61 | // Functions // | ||
| 62 | // // | ||
| 63 | ///////////////// | ||
| 64 | #pragma mark - Functions | ||
| 65 | |||
| 66 | void | ||
| 67 | trutils_close( | ||
| 68 | TRUtilsFile * fb ) | ||
| 69 | { | ||
| 70 | if (!(fb)) | ||
| 71 | return; | ||
| 72 | |||
| 73 | if (fb->fd != -1) | ||
| 74 | close(fb->fd); | ||
| 75 | |||
| 76 | if ((fb->filename)) | ||
| 77 | free(fb->filename); | ||
| 78 | |||
| 79 | free(fb); | ||
| 80 | |||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | TRUtilsFile * | ||
| 86 | trutils_open( | ||
| 87 | unsigned opts, | ||
| 88 | const char * filename ) | ||
| 89 | { | ||
| 90 | TRUtilsFile * fb; | ||
| 91 | struct stat sb; | ||
| 92 | int rc; | ||
| 93 | |||
| 94 | assert(filename != NULL); | ||
| 95 | |||
| 96 | // verify file exists | ||
| 97 | if ((rc = stat(filename, &sb)) == -1) | ||
| 98 | { | ||
| 99 | trutils_error(opts, NULL, "%s: %s", filename, strerror(errno)); | ||
| 100 | return(NULL); | ||
| 101 | }; | ||
| 102 | if ((sb.st_mode & S_IFMT) != S_IFREG) | ||
| 103 | { | ||
| 104 | trutils_error(opts, NULL, "%s: is not a file", filename); | ||
| 105 | return(NULL); | ||
| 106 | }; | ||
| 107 | |||
| 108 | // allocate initial | ||
| 109 | if ((fb = malloc(sizeof(TRUtilsFile))) == NULL) | ||
| 110 | { | ||
| 111 | trutils_error(opts, NULL, "out of virtual memory"); | ||
| 112 | return(NULL); | ||
| 113 | }; | ||
| 114 | memset(fb, 0, sizeof(TRUtilsFile)); | ||
| 115 | fb->fd = -1; | ||
| 116 | |||
| 117 | // copy filename | ||
| 118 | if ((fb->filename = strdup(filename)) == NULL) | ||
| 119 | { | ||
| 120 | trutils_error(opts, NULL, "out of virtual memory"); | ||
| 121 | trutils_close(fb); | ||
| 122 | return(NULL); | ||
| 123 | }; | ||
| 124 | |||
| 125 | // open file | ||
| 126 | if ((fb->fd = open(filename, O_RDONLY)) == -1) | ||
| 127 | { | ||
| 128 | trutils_error(opts, NULL, "%s: %s", filename, strerror(errno)); | ||
| 129 | trutils_close(fb); | ||
| 130 | return(NULL); | ||
| 131 | }; | ||
| 132 | |||
| 133 | return(fb); | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | int | ||
| 138 | trutils_readline( | ||
| 139 | unsigned opts, | ||
| 140 | TRUtilsFile * fb ) | ||
| 141 | { | ||
| 142 | size_t offset; | ||
| 143 | size_t pos; | ||
| 144 | size_t size; | ||
| 145 | ssize_t rc; | ||
| 146 | |||
| 147 | assert(fb != 0); | ||
| 148 | |||
| 149 | // adjusts beginning of line | ||
| 150 | fb->bol = ((fb->eol)) ? &fb->eol[1] : fb->buff; | ||
| 151 | |||
| 152 | // look for end line | ||
| 153 | if ((fb->eol = strchr(fb->bol, '\n')) != NULL) | ||
| 154 | { | ||
| 155 | fb->eol[0] = '\0'; | ||
| 156 | return(1); | ||
| 157 | }; | ||
| 158 | |||
| 159 | // shift buffer | ||
| 160 | offset = (size_t)(fb->bol = fb->buff); | ||
| 161 | for(pos = 0; ((pos+offset) < fb->len); pos++) | ||
| 162 | fb->buff[pos] = fb->buff[pos+offset]; | ||
| 163 | fb->buff[pos] = '\0'; | ||
| 164 | fb->len -= offset; | ||
| 165 | |||
| 166 | // attempt to fill buffer | ||
| 167 | size = sizeof(fb->buff) - fb->len - 1; | ||
| 168 | if ((rc = read(fb->fd, &fb->buff[fb->len], size)) == -1) | ||
| 169 | { | ||
| 170 | trutils_error(opts, NULL, "%s: %s", fb->filename, strerror(errno)); | ||
| 171 | return(-1); | ||
| 172 | }; | ||
| 173 | fb->len += (size_t)rc; | ||
| 174 | fb->buff[fb->len] = '\0'; | ||
| 175 | fb->bol = fb->buff; | ||
| 176 | if (!(fb->len)) | ||
| 177 | return(0); | ||
| 178 | |||
| 179 | // look for end line | ||
| 180 | if ((fb->eol = strchr(fb->bol, '\n')) != NULL) | ||
| 181 | { | ||
| 182 | fb->eol[0] = '\0'; | ||
| 183 | return(1); | ||
| 184 | }; | ||
| 185 | |||
| 186 | fb->eol = &fb->bol[strlen(fb->bol)]; | ||
| 187 | |||
| 188 | return(0); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | /* end of source */ | ||
diff --git a/lib/libtrutils/lfile.h b/lib/libtrutils/lfile.h new file mode 100644 index 0000000..882acd9 --- /dev/null +++ b/lib/libtrutils/lfile.h | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | /* | ||
| 2 | * Tiny RADIUS Client Library | ||
| 3 | * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>. | ||
| 4 | * All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * * Neither the name of David M. Syzdek nor the | ||
| 16 | * names of its contributors may be used to endorse or promote products | ||
| 17 | * derived from this software without specific prior written permission. | ||
| 18 | * | ||
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR | ||
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 29 | * SUCH DAMAGE. | ||
| 30 | */ | ||
| 31 | #ifndef _LIB_LIBTRUTILS_LFILE_H | ||
| 32 | #define _LIB_LIBTRUTILS_LFILE_H 1 | ||
| 33 | |||
| 34 | |||
| 35 | /////////////// | ||
| 36 | // // | ||
| 37 | // Headers // | ||
| 38 | // // | ||
| 39 | /////////////// | ||
| 40 | #pragma mark - Headers | ||
| 41 | |||
| 42 | #include "libtrutils.h" | ||
| 43 | |||
| 44 | #include <stdio.h> | ||
| 45 | #include <stdlib.h> | ||
| 46 | #include <string.h> | ||
| 47 | #include <strings.h> | ||
| 48 | |||
| 49 | |||
| 50 | /////////////////// | ||
| 51 | // // | ||
| 52 | // Definitions // | ||
| 53 | // // | ||
| 54 | /////////////////// | ||
| 55 | #pragma mark - Definitions | ||
| 56 | |||
| 57 | |||
| 58 | ////////////////// | ||
| 59 | // // | ||
| 60 | // Data Types // | ||
| 61 | // // | ||
| 62 | ////////////////// | ||
| 63 | #pragma mark - Data Types | ||
| 64 | |||
| 65 | struct _trutils_file_buffer | ||
| 66 | { | ||
| 67 | int fd; // file descriptor | ||
| 68 | int padint; | ||
| 69 | char * eol; // end of current line | ||
| 70 | char * bol; // beginning of current line | ||
| 71 | size_t len; // length of current buffer | ||
| 72 | char buff[256]; // raw buffer | ||
| 73 | char * filename; | ||
| 74 | }; | ||
| 75 | |||
| 76 | |||
| 77 | ////////////////// | ||
| 78 | // // | ||
| 79 | // Prototypes // | ||
| 80 | // // | ||
| 81 | ////////////////// | ||
| 82 | #pragma mark - Prototypes | ||
| 83 | |||
| 84 | |||
| 85 | #endif /* end of header */ | ||
