diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-08 15:36:11 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-08 15:36:11 -0900 |
| commit | 2d0251490bde79ec11da842f736a54d962ec1900 (patch) | |
| tree | 61f55e7a36b5f8160d5d2dc076bea7c61ade3549 /src | |
| parent | b578527f48d74de2e6836650586ec2824094dde9 (diff) | |
| download | tinyrad-2d0251490bde79ec11da842f736a54d962ec1900.tar.gz tinyrad-2d0251490bde79ec11da842f736a54d962ec1900.tar.bz2 tinyrad-2d0251490bde79ec11da842f736a54d962ec1900.tar.xz tinyrad-2d0251490bde79ec11da842f736a54d962ec1900.zip | |
tinyrad: adding file functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/tinyrad.c | 185 |
1 files changed, 182 insertions, 3 deletions
diff --git a/src/tinyrad.c b/src/tinyrad.c index 8c5a822..5a1fd78 100644 --- a/src/tinyrad.c +++ b/src/tinyrad.c | |||
| @@ -47,6 +47,13 @@ | |||
| 47 | #include <strings.h> | 47 | #include <strings.h> |
| 48 | #include <unistd.h> | 48 | #include <unistd.h> |
| 49 | #include <getopt.h> | 49 | #include <getopt.h> |
| 50 | #include <sys/types.h> | ||
| 51 | #include <sys/stat.h> | ||
| 52 | #include <sys/uio.h> | ||
| 53 | #include <unistd.h> | ||
| 54 | #include <assert.h> | ||
| 55 | #include <fcntl.h> | ||
| 56 | #include <errno.h> | ||
| 50 | 57 | ||
| 51 | 58 | ||
| 52 | /////////////////// | 59 | /////////////////// |
| @@ -62,14 +69,60 @@ | |||
| 62 | 69 | ||
| 63 | ////////////////// | 70 | ////////////////// |
| 64 | // // | 71 | // // |
| 72 | // Data Types // | ||
| 73 | // // | ||
| 74 | ////////////////// | ||
| 75 | #pragma mark - Data Types | ||
| 76 | |||
| 77 | typedef struct my_file_buffer | ||
| 78 | { | ||
| 79 | int fd; // file descriptor | ||
| 80 | int padint; | ||
| 81 | char * eol; // end of current line | ||
| 82 | char * bol; // beginning of current line | ||
| 83 | size_t len; // length of current buffer | ||
| 84 | char buff[256]; // raw buffer | ||
| 85 | char * filename; | ||
| 86 | } MyFile; | ||
| 87 | |||
| 88 | |||
| 89 | typedef struct my_request_av | ||
| 90 | { | ||
| 91 | char * attr_name; | ||
| 92 | char * attr_value; | ||
| 93 | } MyRequestAV; | ||
| 94 | |||
| 95 | |||
| 96 | ////////////////// | ||
| 97 | // // | ||
| 65 | // Prototypes // | 98 | // Prototypes // |
| 66 | // // | 99 | // // |
| 67 | ////////////////// | 100 | ////////////////// |
| 68 | #pragma mark - Prototypes | 101 | #pragma mark - Prototypes |
| 69 | 102 | ||
| 70 | int main( int argc, char * argv[] ); | 103 | int |
| 104 | main( | ||
| 105 | int argc, | ||
| 106 | char * argv[] ); | ||
| 107 | |||
| 108 | |||
| 109 | void | ||
| 110 | my_file_close( | ||
| 111 | MyFile * fb ); | ||
| 112 | |||
| 113 | |||
| 114 | MyFile * | ||
| 115 | my_file_open( | ||
| 116 | const char * filename ); | ||
| 117 | |||
| 118 | |||
| 119 | int | ||
| 120 | my_file_readline( | ||
| 121 | MyFile * fb ); | ||
| 71 | 122 | ||
| 72 | void my_usage( void ); | 123 | |
| 124 | void | ||
| 125 | my_usage( void ); | ||
| 73 | 126 | ||
| 74 | 127 | ||
| 75 | ///////////////// | 128 | ///////////////// |
| @@ -93,10 +146,11 @@ int main(int argc, char * argv[]) | |||
| 93 | uint8_t dictloaded; | 146 | uint8_t dictloaded; |
| 94 | 147 | ||
| 95 | // getopt options | 148 | // getopt options |
| 96 | static char short_opt[] = "D:d:hI:qVv"; | 149 | static char short_opt[] = "D:d:f:hI:qVv"; |
| 97 | static struct option long_opt[] = | 150 | static struct option long_opt[] = |
| 98 | { | 151 | { |
| 99 | {"debug", optional_argument, NULL, 'd' }, | 152 | {"debug", optional_argument, NULL, 'd' }, |
| 153 | {"file", optional_argument, NULL, 'f' }, | ||
| 100 | {"help", no_argument, NULL, 'h' }, | 154 | {"help", no_argument, NULL, 'h' }, |
| 101 | {"quiet", no_argument, NULL, 'q' }, | 155 | {"quiet", no_argument, NULL, 'q' }, |
| 102 | {"silent", no_argument, NULL, 'q' }, | 156 | {"silent", no_argument, NULL, 'q' }, |
| @@ -228,12 +282,137 @@ int main(int argc, char * argv[]) | |||
| 228 | } | 282 | } |
| 229 | 283 | ||
| 230 | 284 | ||
| 285 | void | ||
| 286 | my_file_close( | ||
| 287 | MyFile * fb ) | ||
| 288 | { | ||
| 289 | if (!(fb)) | ||
| 290 | return; | ||
| 291 | |||
| 292 | if (fb->fd != -1) | ||
| 293 | close(fb->fd); | ||
| 294 | |||
| 295 | if ((fb->filename)) | ||
| 296 | free(fb->filename); | ||
| 297 | |||
| 298 | free(fb); | ||
| 299 | |||
| 300 | return; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | MyFile * | ||
| 305 | my_file_open( | ||
| 306 | const char * filename ) | ||
| 307 | { | ||
| 308 | MyFile * fb; | ||
| 309 | struct stat sb; | ||
| 310 | int rc; | ||
| 311 | |||
| 312 | assert(filename != NULL); | ||
| 313 | |||
| 314 | // verify file exists | ||
| 315 | if ((rc = stat(filename, &sb)) == -1) | ||
| 316 | { | ||
| 317 | our_error(PROGRAM_NAME, NULL, "%s: %s", filename, strerror(errno)); | ||
| 318 | return(NULL); | ||
| 319 | }; | ||
| 320 | if ((sb.st_mode & S_IFMT) != S_IFREG) | ||
| 321 | { | ||
| 322 | our_error(PROGRAM_NAME, NULL, "%s: is not a file", filename); | ||
| 323 | return(NULL); | ||
| 324 | }; | ||
| 325 | |||
| 326 | // allocate initial | ||
| 327 | if ((fb = malloc(sizeof(MyFile))) == NULL) | ||
| 328 | { | ||
| 329 | our_error(PROGRAM_NAME, NULL, "out of virtual memory"); | ||
| 330 | return(NULL); | ||
| 331 | }; | ||
| 332 | memset(fb, 0, sizeof(MyFile)); | ||
| 333 | fb->fd = -1; | ||
| 334 | |||
| 335 | // copy filename | ||
| 336 | if ((fb->filename = strdup(filename)) == NULL) | ||
| 337 | { | ||
| 338 | our_error(PROGRAM_NAME, NULL, "out of virtual memory"); | ||
| 339 | my_file_close(fb); | ||
| 340 | return(NULL); | ||
| 341 | }; | ||
| 342 | |||
| 343 | // open file | ||
| 344 | if ((fb->fd = open(filename, O_RDONLY)) == -1) | ||
| 345 | { | ||
| 346 | our_error(PROGRAM_NAME, NULL, "%s: %s", filename, strerror(errno)); | ||
| 347 | my_file_close(fb); | ||
| 348 | return(NULL); | ||
| 349 | }; | ||
| 350 | |||
| 351 | return(fb); | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | int | ||
| 356 | my_file_readline( | ||
| 357 | MyFile * fb ) | ||
| 358 | { | ||
| 359 | size_t offset; | ||
| 360 | size_t pos; | ||
| 361 | size_t size; | ||
| 362 | ssize_t rc; | ||
| 363 | |||
| 364 | assert(fb != 0); | ||
| 365 | |||
| 366 | // adjusts beginning of line | ||
| 367 | fb->bol = ((fb->eol)) ? &fb->eol[1] : fb->buff; | ||
| 368 | |||
| 369 | // look for end line | ||
| 370 | if ((fb->eol = strchr(fb->bol, '\n')) != NULL) | ||
| 371 | { | ||
| 372 | fb->eol[0] = '\0'; | ||
| 373 | return(1); | ||
| 374 | }; | ||
| 375 | |||
| 376 | // shift buffer | ||
| 377 | offset = (size_t)(fb->bol = fb->buff); | ||
| 378 | for(pos = 0; ((pos+offset) < fb->len); pos++) | ||
| 379 | fb->buff[pos] = fb->buff[pos+offset]; | ||
| 380 | fb->buff[pos] = '\0'; | ||
| 381 | fb->len -= offset; | ||
| 382 | |||
| 383 | // attempt to fill buffer | ||
| 384 | size = sizeof(fb->buff) - fb->len - 1; | ||
| 385 | if ((rc = read(fb->fd, &fb->buff[fb->len], size)) == -1) | ||
| 386 | { | ||
| 387 | our_error(PROGRAM_NAME, NULL, "%s: %s", fb->filename, strerror(errno)); | ||
| 388 | return(-1); | ||
| 389 | }; | ||
| 390 | fb->len += (size_t)rc; | ||
| 391 | fb->buff[fb->len] = '\0'; | ||
| 392 | fb->bol = fb->buff; | ||
| 393 | if (!(fb->len)) | ||
| 394 | return(0); | ||
| 395 | |||
| 396 | // look for end line | ||
| 397 | if ((fb->eol = strchr(fb->bol, '\n')) != NULL) | ||
| 398 | { | ||
| 399 | fb->eol[0] = '\0'; | ||
| 400 | return(1); | ||
| 401 | }; | ||
| 402 | |||
| 403 | fb->eol = &fb->bol[strlen(fb->bol)]; | ||
| 404 | |||
| 405 | return(0); | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 231 | void my_usage(void) | 409 | void my_usage(void) |
| 232 | { | 410 | { |
| 233 | printf("Usage: %s [OPTIONS] url\n", PROGRAM_NAME); | 411 | printf("Usage: %s [OPTIONS] url\n", PROGRAM_NAME); |
| 234 | printf("OPTIONS:\n"); | 412 | printf("OPTIONS:\n"); |
| 235 | printf(" -D dictionary include dictionary\n"); | 413 | printf(" -D dictionary include dictionary\n"); |
| 236 | printf(" -d level, --debug=level print debug messages\n"); | 414 | printf(" -d level, --debug=level print debug messages\n"); |
| 415 | printf(" -f file, --file=file attribute list\n"); | ||
| 237 | printf(" -h, --help print this help and exit\n"); | 416 | printf(" -h, --help print this help and exit\n"); |
| 238 | printf(" -I path add path to dictionary search paths\n"); | 417 | printf(" -I path add path to dictionary search paths\n"); |
| 239 | printf(" -q, --quiet, --silent do not print messages\n"); | 418 | printf(" -q, --quiet, --silent do not print messages\n"); |
