Patch from Alexander Shagarovi (ticket #5832): use getopt() for command line parsing. I fixed some code style and simplified a bit.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36739 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2010-05-08 14:33:04 +00:00
parent 38135af9af
commit 96aa451087
+45 -32
View File
@@ -1,4 +1,5 @@
/* /*
* Copyright 2010, Alexander Shagarov, [email protected].
* Copyright 2005, Stephan Aßmus, [email protected]. * Copyright 2005, Stephan Aßmus, [email protected].
* Copyright 2004-2009, Axel Dörfler, [email protected]. * Copyright 2004-2009, Axel Dörfler, [email protected].
* Copyright 2002, Sebastian Nozzi. * Copyright 2002, Sebastian Nozzi.
@@ -14,6 +15,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -244,6 +246,21 @@ catAttr(const char *attribute, const char *fileName, bool keepRaw = false,
} }
static int
usage(const char* program) {
// Issue usage message
fprintf(stderr, "usage: %s [-P] [--raw|-r] <attribute-name> <file1> "
"[<file2>...]\n"
"\t-P : Don't resolve links\n"
"\t--raw|-r : Get the raw data of attributes\n", program);
// Be's original version -only- returned 1 if the
// amount of parameters was wrong, not if the file
// or attribute couldn't be found (!)
// In all other cases it returned 0
return 1;
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@@ -253,43 +270,39 @@ main(int argc, char *argv[])
else else
program++; program++;
if (argc > 2) { bool keepRaw = false;
int32 attrNameIndex = 1; bool resolveLinks = true;
bool keepRaw = false; const struct option longOptions[] = {
bool resolveLinks = true; {"raw", no_argument, NULL, 'r'},
{NULL, 0, NULL, 0}
};
if (!strcmp(argv[attrNameIndex], "-P")) { int rez;
resolveLinks = false; while ((rez = getopt_long(argc, argv, "rP", longOptions, NULL)) != -1) {
attrNameIndex++; switch (rez) {
} case 'r':
keepRaw = true;
// see if user wants to get to the raw data of the attribute break;
if (strcmp(argv[attrNameIndex], "--raw") == 0 || case 'P':
strcmp(argv[attrNameIndex], "-r") == 0) { resolveLinks = false;
attrNameIndex++; break;
keepRaw = true; default:
return usage(program);
} }
}
// Cat the attribute for every file given if (optind + 2 > argc)
for (int32 i = attrNameIndex + 1; i < argc; i++) { return usage(program);
status_t status = catAttr(argv[attrNameIndex], argv[i], keepRaw,
const char* attr_name = argv[optind++];
while (optind < argc) {
const char* file_name = argv[optind++];
status_t status = catAttr(attr_name, file_name, keepRaw,
resolveLinks); resolveLinks);
if (status != B_OK) { if (status != B_OK) {
fprintf(stderr, "%s: \"%s\", attribute \"%s\": %s\n", fprintf(stderr, "%s: \"%s\", attribute \"%s\": %s\n",
program, argv[i], argv[attrNameIndex], strerror(status)); program, file_name, attr_name, strerror(status));
}
} }
} else {
// Issue usage message
fprintf(stderr, "usage: %s [-P] [--raw|-r] <attribute-name> <file1> "
"[<file2>...]\n"
"\t-P : Don't resolve links\n"
"\t--raw|-r : Get the raw data of attributes\n", program);
// Be's original version -only- returned 1 if the
// amount of parameters was wrong, not if the file
// or attribute couldn't be found (!)
// In all other cases it returned 0
return 1;
} }
return 0; return 0;