Open a single file given its path */
| 411 | |
| 412 | /* Open a single file given its path */ |
| 413 | _TINYDIR_FUNC |
| 414 | int tinydir_file_open(tinydir_file *file, const char *path) { |
| 415 | tinydir_dir dir; |
| 416 | int result = 0; |
| 417 | int found = 0; |
| 418 | char dir_name_buf[_TINYDIR_PATH_MAX]; |
| 419 | char file_name_buf[_TINYDIR_FILENAME_MAX]; |
| 420 | char *dir_name; |
| 421 | char *base_name; |
| 422 | #ifdef _WIN32 |
| 423 | char drive_buf[_TINYDIR_PATH_MAX]; |
| 424 | char ext_buf[_TINYDIR_FILENAME_MAX]; |
| 425 | #endif |
| 426 | |
| 427 | if (file == NULL || path == NULL || strlen(path) == 0) { |
| 428 | errno = EINVAL; |
| 429 | return -1; |
| 430 | } |
| 431 | if (strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX) { |
| 432 | errno = ENAMETOOLONG; |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | /* Get the parent path */ |
| 437 | #ifdef _WIN32 |
| 438 | if (_splitpath_s(path, drive_buf, sizeof drive_buf, dir_name_buf, |
| 439 | sizeof dir_name_buf, file_name_buf, sizeof file_name_buf, |
| 440 | ext_buf, sizeof ext_buf)) { |
| 441 | errno = EINVAL; |
| 442 | return -1; |
| 443 | } |
| 444 | /* Concatenate the drive letter and dir name to form full dir name */ |
| 445 | strcat(drive_buf, dir_name_buf); |
| 446 | dir_name = drive_buf; |
| 447 | /* Concatenate the file name and extension to form base name */ |
| 448 | strcat(file_name_buf, ext_buf); |
| 449 | base_name = file_name_buf; |
| 450 | #else |
| 451 | strcpy(dir_name_buf, path); |
| 452 | dir_name = dirname(dir_name_buf); |
| 453 | strcpy(file_name_buf, path); |
| 454 | base_name = basename(file_name_buf); |
| 455 | #endif |
| 456 | |
| 457 | /* Open the parent directory */ |
| 458 | if (tinydir_open(&dir, dir_name) == -1) { |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | /* Read through the parent directory and look for the file */ |
| 463 | while (dir.has_next) { |
| 464 | if (tinydir_readfile(&dir, file) == -1) { |
| 465 | result = -1; |
| 466 | goto bail; |
| 467 | } |
| 468 | if (strcmp(file->name, base_name) == 0) { |
| 469 | /* File found */ |
| 470 | found = 1; |
nothing calls this directly
no test coverage detected