Ticket #10575: 0001-setarch-list-available-architectures.patch

File 0001-setarch-list-available-architectures.patch, 3.5 KB (added by 0xffea, 10 years ago)

New option added to list all architectures

  • src/bin/setarch.cpp

    From 78963d3c9f9182530e7f2ce14b612b158e22de91 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?David=20H=C3=B6ppner?= <0xffea@gmail.com>
    Date: Sat, 1 Mar 2014 23:08:48 +0800
    Subject: [PATCH] setarch: list available architectures
    
    Fixes #10575
    ---
     src/bin/setarch.cpp | 49 +++++++++++++++++++++++++++++++++++++------------
     1 file changed, 37 insertions(+), 12 deletions(-)
    
    diff --git a/src/bin/setarch.cpp b/src/bin/setarch.cpp
    index b6fc26c..44c27be 100644
    a b const char* kCommandName = __progname;  
    2323
    2424
    2525static const char* kUsage =
    26     "Usage: %s [ <options> ] <architecture> [ <command> ... ]\n"
     26    "Usage: %s [-hl]\n"
     27    "       %s [-p] <architecture> [ <command> ... ]\n"
    2728    "Executes the given command or, by default, a shell with a PATH\n"
    2829    "environment variable modified such that commands for the given\n"
    2930    "architecture will be preferred, respectively used exclusively in case of\n"
    static const char* kUsage =  
    3233    "Options:\n"
    3334    "  -h, --help\n"
    3435    "    Print this usage info.\n"
     36    "  -l, --list-architectures\n"
     37    "    List all architectures.\n"
    3538    "  -p, --print-path\n"
    3639    "    Only print the modified PATH variable value; don't execute any\n"
    3740    "    command.\n"
    static const char* kUsage =  
    4144static void
    4245print_usage_and_exit(bool error)
    4346{
    44     fprintf(error ? stderr : stdout, kUsage, kCommandName);
    45     exit(error ? 1 : 0);
     47    fprintf(error ? stderr : stdout, kUsage, kCommandName, kCommandName);
     48    exit(error ? 1 : 0);
    4649}
    4750
    4851
    int  
    136139main(int argc, const char* const* argv)
    137140{
    138141    bool printPath = false;
     142    bool listArchitectures = false;
    139143
    140144    while (true) {
    141145        static struct option sLongOptions[] = {
    142146            { "help", no_argument, 0, 'h' },
     147            { "list-architectures", no_argument, 0, 'l' },
    143148            { "print-path", no_argument, 0, 'p' },
    144149            { 0, 0, 0, 0 }
    145150        };
    146151
    147152        opterr = 0; // don't print errors
    148         int c = getopt_long(argc, (char**)argv, "+hp",
     153        int c = getopt_long(argc, (char**)argv, "+hlp",
    149154            sLongOptions, NULL);
    150155        if (c == -1)
    151156            break;
    main(int argc, const char* const* argv)  
    155160                print_usage_and_exit(false);
    156161                break;
    157162
     163            case 'l':
     164                listArchitectures = true;
     165                break;
     166
    158167            case 'p':
    159168                printPath = true;
    160169                break;
    main(int argc, const char* const* argv)  
    165174        }
    166175    }
    167176
     177    // only one of listArchitectures, printPath may be specified
     178    if (listArchitectures && printPath)
     179        print_usage_and_exit(true);
     180
     181    // get architectures
     182    BStringList architectures;
     183    status_t error = get_architectures(architectures);
     184    if (error != B_OK) {
     185        fprintf(stderr, "Error: Failed to get architectures: %s\n",
     186            strerror(error));
     187        exit(1);
     188    }
     189
     190    // list architectures
     191    if (listArchitectures) {
     192        if (optind != argc)
     193            print_usage_and_exit(true);
     194
     195        int32 count = architectures.CountStrings();
     196        for (int32 i = 0; i < count; i++)
     197            printf("%s\n", architectures.StringAt(i).String());
     198        return 0;
     199    }
     200
    168201    // The remaining arguments are the architecture and optionally the command
    169202    // to execute.
    170203    if (optind >= argc)
    main(int argc, const char* const* argv)  
    178211        print_usage_and_exit(true);
    179212
    180213    // check the architecture
    181     BStringList architectures;
    182     status_t error = get_architectures(architectures);
    183     if (error != B_OK) {
    184         fprintf(stderr, "Error: Failed to get architectures: %s\n",
    185             strerror(error));
    186         exit(1);
    187     }
    188 
    189214    if (!architectures.HasString(architecture)) {
    190215        fprintf(stderr, "Error: Unsupported architecture \"%s\"\n",
    191216            architecture);