lsindex: Fixed wrong errno code to be shown

* When we want to inspect errno after readdir() (or fs_read_index_dir(),
  which is the same), we need to reset errno beforehand as documented
  in the OpenGroup Base.
* Minor coding style, and license header fixes.
* This fixes the wrong error code from #16301, but not the underlying
  issue.
This commit is contained in:
Axel Dörfler
2020-06-25 22:21:23 +02:00
parent 60162d0118
commit 1487a24238
+31 -16
View File
@@ -1,10 +1,13 @@
// lsindex - for Haiku /*
// * Copyright 2002-2020, Haiku, Inc. All Rights Reserved.
// authors, in order of contribution: * Distributed under the terms of the MIT License.
// [email protected] *
// [email protected] * Authors:
// [email protected] * [email protected]
// * [email protected]
* Axel Dörfler, [email protected]
*/
#include <TypeConstants.h> #include <TypeConstants.h>
#include <fs_info.h> #include <fs_info.h>
@@ -137,11 +140,13 @@ print_index_verbose_stat(const index_info &info, char *name)
// Created // Created
char string[30]; char string[30];
strftime(string, sizeof(string), "%Y-%m-%d %H:%M", localtime(&info.creation_time)); strftime(string, sizeof(string), "%Y-%m-%d %H:%M",
localtime(&info.creation_time));
printf("%s ", string); printf("%s ", string);
// Modified // Modified
strftime(string, sizeof(string), "%Y-%m-%d %H:%M", localtime(&info.modification_time)); strftime(string, sizeof(string), "%Y-%m-%d %H:%M",
localtime(&info.modification_time));
printf("%s", string); printf("%s", string);
// User // User
@@ -166,20 +171,23 @@ main(int argc, char **argv)
if (!strcmp(argv[i], "--help")) { if (!strcmp(argv[i], "--help")) {
print_help(); print_help();
return 0; return 0;
} else if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) }
if (!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v"))
verbose = true; verbose = true;
else if (!strcmp(argv[i], "--long") || !strcmp(argv[i], "-l")) else if (!strcmp(argv[i], "--long") || !strcmp(argv[i], "-l"))
longListing = true; longListing = true;
else if (!strcmp(argv[i], "--mkindex")) else if (!strcmp(argv[i], "--mkindex"))
mkindexOutput = true; mkindexOutput = true;
else { else {
fprintf(stderr, "%s: option %s is not understood (use --help for help)\n", argv[0], argv[i]); fprintf(stderr, "%s: option %s is not understood (use --help "
"for help)\n", argv[0], argv[i]);
return -1; return -1;
} }
} else { } else {
device = dev_for_path(argv[i]); device = dev_for_path(argv[i]);
if (device < 0) { if (device < 0) {
fprintf(stderr, "%s: can't get information about volume: %s\n", argv[0], argv[i]); fprintf(stderr, "%s: can't get information about volume: %s\n",
argv[0], argv[i]);
return -1; return -1;
} }
} }
@@ -198,10 +206,13 @@ main(int argc, char **argv)
} }
while (1) { while (1) {
// We have to reset errno before calling fs_read_index_dir().
errno = 0;
dirent *index = fs_read_index_dir(indices); dirent *index = fs_read_index_dir(indices);
if (index == NULL) { if (index == NULL) {
if (errno != B_ENTRY_NOT_FOUND && errno != B_OK) { if (errno != B_ENTRY_NOT_FOUND && errno != B_OK) {
printf("%s: fs_read_index_dir: (%d) %s\n", argv[0], errno, strerror(errno)); printf("%s: fs_read_index_dir: (%d) %s\n", argv[0], errno,
strerror(errno));
return errno; return errno;
} }
break; break;
@@ -211,7 +222,8 @@ main(int argc, char **argv)
index_info info; index_info info;
if (fs_stat_index(device, index->d_name, &info) != B_OK) { if (fs_stat_index(device, index->d_name, &info) != B_OK) {
printf("%s: fs_stat_index(): (%d) %s\n", argv[0], errno, strerror(errno)); printf("%s: fs_stat_index(): (%d) %s\n", argv[0], errno,
strerror(errno));
return errno; return errno;
} }
@@ -219,8 +231,11 @@ main(int argc, char **argv)
print_index_verbose_stat(info, index->d_name); print_index_verbose_stat(info, index->d_name);
else if (longListing) else if (longListing)
print_index_long_stat(info, index->d_name); print_index_long_stat(info, index->d_name);
else /* mkindexOutput */ else {
printf("mkindex -t %s '%s'\n", print_index_type(info, true), index->d_name); // mkindex output
printf("mkindex -t %s '%s'\n", print_index_type(info, true),
index->d_name);
}
} else } else
printf("%s\n", index->d_name); printf("%s\n", index->d_name);
} }