Ticket #5832: catattr.diff

File catattr.diff, 3.1 KB (added by ashagarov, 14 years ago)
  • src/bin/catattr.cpp

     
    11/*
     2 * Copyright 2010, Alexander Shagarov, alexander.shagarov@gmail.com.
    23 * Copyright 2005, Stephan Aßmus, superstippi@yellowbites.com.
    34 * Copyright 2004-2009, Axel Dörfler, axeld@pinc-software.de.
    45 * Copyright 2002, Sebastian Nozzi.
     
    1819#include <stdlib.h>
    1920#include <string.h>
    2021#include <unistd.h>
     22#include <getopt.h>
    2123
    2224
    2325/*! Used to present the characters in the raw data view */
     
    244246}
    245247
    246248
     249static int
     250usage(const char* program) {
     251    // Issue usage message
     252    fprintf(stderr, "usage: %s [-P] [--raw|-r] <attribute-name> <file1> "
     253        "[<file2>...]\n"
     254        "\t-P : Don't resolve links\n"
     255        "\t--raw|-r : Get the raw data of attributes\n", program);
     256    // Be's original version -only- returned 1 if the
     257    // amount of parameters was wrong, not if the file
     258    // or attribute couldn't be found (!)
     259    // In all other cases it returned 0
     260    return 1;
     261}
     262
     263
    247264int
    248265main(int argc, char *argv[])
    249266{
     
    253270    else
    254271        program++;
    255272
    256     if (argc > 2) {
    257         int32 attrNameIndex = 1;
    258         bool keepRaw = false;
    259         bool resolveLinks = true;
     273    bool keepRaw = false;
     274    bool resolveLinks = true;
    260275
    261         if (!strcmp(argv[attrNameIndex], "-P")) {
    262             resolveLinks = false;
    263             attrNameIndex++;
     276    const char* short_options = "rP";
     277    const struct option long_options[] = {
     278        {"raw", no_argument, NULL, 'r'},
     279        {NULL, 0, NULL, 0}
     280    };
     281
     282    int rez, option_index;
     283    while ((rez = getopt_long(argc, argv, short_options, long_options,
     284                    &option_index)) != -1) {
     285        switch (rez) {
     286            case 'r':
     287                keepRaw = true;
     288                break;
     289            case 'P':
     290                resolveLinks = false;
     291                break;
     292            default:
     293                return usage(program);
    264294        }
    265        
    266         // see if user wants to get to the raw data of the attribute
    267         if (strcmp(argv[attrNameIndex], "--raw") == 0 ||
    268             strcmp(argv[attrNameIndex], "-r") == 0) {
    269             attrNameIndex++;
    270             keepRaw = true;
    271         }
     295    }
    272296
    273         // Cat the attribute for every file given
    274         for (int32 i = attrNameIndex + 1; i < argc; i++) {
    275             status_t status = catAttr(argv[attrNameIndex], argv[i], keepRaw,
     297    if (optind + 2 > argc)
     298        return usage(program);
     299
     300    const char* attr_name = argv[optind++];
     301    while (optind < argc) {
     302        const char* file_name = argv[optind++];
     303        status_t status = catAttr(attr_name, file_name, keepRaw,
    276304                resolveLinks);
    277             if (status != B_OK) {
    278                 fprintf(stderr, "%s: \"%s\", attribute \"%s\": %s\n",
    279                     program, argv[i], argv[attrNameIndex], strerror(status));
    280             }
     305        if (status != B_OK) {
     306            fprintf(stderr, "%s: \"%s\", attribute \"%s\": %s\n",
     307                    program, file_name, attr_name, strerror(status));
    281308        }
    282     } else {
    283         // Issue usage message
    284         fprintf(stderr, "usage: %s [-P] [--raw|-r] <attribute-name> <file1> "
    285             "[<file2>...]\n"
    286             "\t-P : Don't resolve links\n"
    287             "\t--raw|-r : Get the raw data of attributes\n", program);
    288         // Be's original version -only- returned 1 if the
    289         // amount of parameters was wrong, not if the file
    290         // or attribute couldn't be found (!)
    291         // In all other cases it returned 0
    292         return 1;
    293309    }
    294310
    295311    return 0;