Hello! I fix catattr.cpp with getopt_long(). Now it works with catattr -r -P attr file -- Best regards Alexander.
Index: src/bin/catattr.cpp =================================================================== --- src/bin/catattr.cpp (revision 36704) +++ src/bin/catattr.cpp (working copy) @@ -1,4 +1,5 @@ /* + * Copyright 2010, Alexander Shagarov, alexander.shagarov@xxxxxxxxxx * Copyright 2005, Stephan Aßmus, superstippi@xxxxxxxxxxxxxxxx * Copyright 2004-2009, Axel Dörfler, axeld@xxxxxxxxxxxxxxxxx * Copyright 2002, Sebastian Nozzi. @@ -18,6 +19,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <getopt.h> /*! Used to present the characters in the raw data view */ @@ -244,6 +246,21 @@ } +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 main(int argc, char *argv[]) { @@ -253,43 +270,42 @@ else program++; - if (argc > 2) { - int32 attrNameIndex = 1; - bool keepRaw = false; - bool resolveLinks = true; + bool keepRaw = false; + bool resolveLinks = true; - if (!strcmp(argv[attrNameIndex], "-P")) { - resolveLinks = false; - attrNameIndex++; + const char* short_options = "rP"; + const struct option long_options[] = { + {"raw", no_argument, NULL, 'r'}, + {NULL, 0, NULL, 0} + }; + + int rez, option_index; + while ((rez = getopt_long(argc, argv, short_options, long_options, + &option_index)) != -1) { + switch (rez) { + case 'r': + keepRaw = true; + break; + case 'P': + resolveLinks = false; + break; + default: + return usage(program); } - - // see if user wants to get to the raw data of the attribute - if (strcmp(argv[attrNameIndex], "--raw") == 0 || - strcmp(argv[attrNameIndex], "-r") == 0) { - attrNameIndex++; - keepRaw = true; - } + } - // Cat the attribute for every file given - for (int32 i = attrNameIndex + 1; i < argc; i++) { - status_t status = catAttr(argv[attrNameIndex], argv[i], keepRaw, + if (optind + 2 > argc) + return usage(program); + + 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); - if (status != B_OK) { - fprintf(stderr, "%s: \"%s\", attribute \"%s\": %s\n", - program, argv[i], argv[attrNameIndex], strerror(status)); - } + if (status != B_OK) { + fprintf(stderr, "%s: \"%s\", attribute \"%s\": %s\n", + 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;