diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-09 22:58:54 -0900 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-09 22:58:54 -0900 |
| commit | ee1db3a9ecb9ec6f7bf6c61cfe431e1646c91608 (patch) | |
| tree | 1fdeadd32efed07438970740ee797b423854b898 | |
| parent | c0f7c7d95c94280335e35dfcf31df74af5565598 (diff) | |
| download | tinyrad-ee1db3a9ecb9ec6f7bf6c61cfe431e1646c91608.tar.gz tinyrad-ee1db3a9ecb9ec6f7bf6c61cfe431e1646c91608.tar.bz2 tinyrad-ee1db3a9ecb9ec6f7bf6c61cfe431e1646c91608.tar.xz tinyrad-ee1db3a9ecb9ec6f7bf6c61cfe431e1646c91608.zip | |
libtinyrad: adding tinyrad_readline()
| -rw-r--r-- | include/tinyrad_internal.h | 12 | ||||
| -rw-r--r-- | lib/libtinyrad/lfile.c | 47 | ||||
| -rw-r--r-- | lib/libtinyrad/libtinyrad.sym | 3 |
3 files changed, 62 insertions, 0 deletions
diff --git a/include/tinyrad_internal.h b/include/tinyrad_internal.h index f3c531f..53a8a92 100644 --- a/include/tinyrad_internal.h +++ b/include/tinyrad_internal.h | |||
| @@ -190,6 +190,18 @@ _TINYRAD_F void * | |||
| 190 | tinyrad_assertions( void ); | 190 | tinyrad_assertions( void ); |
| 191 | 191 | ||
| 192 | 192 | ||
| 193 | //-----------------// | ||
| 194 | // file prototypes // | ||
| 195 | //-----------------// | ||
| 196 | #pragma mark file prototypes | ||
| 197 | |||
| 198 | _TINYRAD_F int | ||
| 199 | tinyrad_readline( | ||
| 200 | int fd, | ||
| 201 | char * str, | ||
| 202 | size_t size ); | ||
| 203 | |||
| 204 | |||
| 193 | //--------------------// | 205 | //--------------------// |
| 194 | // strings prototypes // | 206 | // strings prototypes // |
| 195 | //--------------------// | 207 | //--------------------// |
diff --git a/lib/libtinyrad/lfile.c b/lib/libtinyrad/lfile.c index 1a5da3c..e5bf473 100644 --- a/lib/libtinyrad/lfile.c +++ b/lib/libtinyrad/lfile.c | |||
| @@ -414,6 +414,53 @@ tinyrad_file_readline_split( | |||
| 414 | } | 414 | } |
| 415 | 415 | ||
| 416 | 416 | ||
| 417 | int | ||
| 418 | tinyrad_readline( | ||
| 419 | int fd, | ||
| 420 | char * str, | ||
| 421 | size_t size ) | ||
| 422 | { | ||
| 423 | ssize_t rc; | ||
| 424 | struct stat sb; | ||
| 425 | size_t pos; | ||
| 426 | |||
| 427 | // check file descriptor | ||
| 428 | if (fd == -1) | ||
| 429 | return(TRAD_EINVAL); | ||
| 430 | if ((rc = fstat(fd, &sb)) == -1) | ||
| 431 | return(TRAD_EINVAL); | ||
| 432 | |||
| 433 | // process input (slow, but works on TCP sockets, PIPE, FIFO, and regular files) | ||
| 434 | for(pos = 0; (pos < (size-1)); pos++) | ||
| 435 | { | ||
| 436 | // read data | ||
| 437 | if ((rc = read(fd, &str[pos], 1)) == -1) | ||
| 438 | return(TRAD_EUNKNOWN); | ||
| 439 | |||
| 440 | // process end of file | ||
| 441 | if (rc == 0) | ||
| 442 | { | ||
| 443 | str[pos] = '\0'; | ||
| 444 | return(TRAD_SUCCESS); | ||
| 445 | }; | ||
| 446 | |||
| 447 | // continue if not end of file | ||
| 448 | if (str[pos] != '\n') | ||
| 449 | continue; | ||
| 450 | |||
| 451 | // process end of line | ||
| 452 | str[pos] = '\0'; | ||
| 453 | if ((pos > 0) && (str[pos-1] == '\r')) | ||
| 454 | str[pos-1] = '\0'; | ||
| 455 | return(TRAD_SUCCESS); | ||
| 456 | }; | ||
| 457 | |||
| 458 | str[pos] = '\0'; | ||
| 459 | |||
| 460 | return(TRAD_ENOBUFS); | ||
| 461 | } | ||
| 462 | |||
| 463 | |||
| 417 | /// wrapper around stat() for dictionary processing | 464 | /// wrapper around stat() for dictionary processing |
| 418 | /// | 465 | /// |
| 419 | /// @param[in] path file system path | 466 | /// @param[in] path file system path |
diff --git a/lib/libtinyrad/libtinyrad.sym b/lib/libtinyrad/libtinyrad.sym index e1c596f..b7875ce 100644 --- a/lib/libtinyrad/libtinyrad.sym +++ b/lib/libtinyrad/libtinyrad.sym | |||
| @@ -74,6 +74,9 @@ tinyrad_dict_vendor_info | |||
| 74 | tinyrad_strerror | 74 | tinyrad_strerror |
| 75 | tinyrad_strerror_r | 75 | tinyrad_strerror_r |
| 76 | # | 76 | # |
| 77 | # file functions | ||
| 78 | tinyrad_readline | ||
| 79 | # | ||
| 77 | # memory functions | 80 | # memory functions |
| 78 | tinyrad_binval_alloc | 81 | tinyrad_binval_alloc |
| 79 | tinyrad_binval_dup | 82 | tinyrad_binval_dup |
