Ignore:
Timestamp:
Apr 24, 2011, 10:33:50 AM (12 years ago)
Author:
Sam Hocevar
Message:

tool: implement `get' in abuse-tool.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/tool/abuse-tool.cpp

    r540 r543  
    2020static void Usage();
    2121
     22enum
     23{
     24    CMD_INVALID,
     25    CMD_LIST,
     26    CMD_GET,
     27    CMD_MOVE,
     28    CMD_DEL,
     29    CMD_PUT,
     30    CMD_RENAME,
     31    CMD_GETPCX,
     32    CMD_PUTPCX,
     33};
     34
    2235int main(int argc, char *argv[])
    2336{
     
    2841    }
    2942
    30     char *file = argv[1];
    31     char *cmd = argv[2];
     43    int cmd = !strcmp(argv[2], "list") ? CMD_LIST
     44            : !strcmp(argv[2], "get") ? CMD_GET
     45            : !strcmp(argv[2], "del") ? CMD_DEL
     46            : !strcmp(argv[2], "put") ? CMD_PUT
     47            : !strcmp(argv[2], "move") ? CMD_MOVE
     48            : !strcmp(argv[2], "rename") ? CMD_RENAME
     49            : !strcmp(argv[2], "getpcx") ? CMD_GETPCX
     50            : !strcmp(argv[2], "putpcx") ? CMD_PUTPCX
     51            : CMD_INVALID;
    3252
    33     if (!strcmp(cmd, "list"))
     53    if (cmd == CMD_INVALID)
    3454    {
    35         jFILE fp(file, "rb");
    36         if (fp.open_failure())
    37         {
    38             fprintf(stderr, "ERROR - could not open %s\n", file);
    39             return EXIT_FAILURE;
    40         }
     55        fprintf(stderr, "abuse-tool: unknown command `%s'\n", argv[2]);
     56        return EXIT_FAILURE;
     57    }
    4158
     59    /* Check argument count and file access mode */
     60    char const *mode = "rwb";
     61    int minargc = 3;
     62
     63    switch (cmd)
     64    {
     65    case CMD_LIST:
     66        mode = "rb"; // Read-only access
     67        break;
     68    case CMD_GET:
     69        minargc = 4;
     70        mode = "rb"; // Read-only access
     71        break;
     72    case CMD_PUT:
     73        minargc = 5;
     74        break;
     75    case CMD_MOVE:
     76        minargc = 5;
     77        break;
     78    case CMD_RENAME:
     79        minargc = 5;
     80        break;
     81    case CMD_DEL:
     82        minargc = 4;
     83        break;
     84    case CMD_GETPCX:
     85        minargc = 4;
     86        mode = "rb"; // Read-only access
     87        break;
     88    case CMD_PUTPCX:
     89        minargc = 5;
     90        break;
     91    }
     92
     93    if (argc < minargc)
     94    {
     95        fprintf(stderr, "abuse-tool: too few arguments to command `%s'\n",
     96                         argv[2]);
     97        return EXIT_FAILURE;
     98    }
     99
     100    /* Open the SPEC file */
     101    char const *file = argv[1];
     102
     103    jFILE fp(file, mode);
     104    if (fp.open_failure())
     105    {
     106        fprintf(stderr, "ERROR - could not open %s\n", file);
     107        return EXIT_FAILURE;
     108    }
     109
     110    spec_directory dir(&fp);
     111
     112    /* Now really execute commands */
     113    if (cmd == CMD_LIST)
     114    {
    42115        printf("   id  type     size  name & information\n");
    43116        printf(" ----  ----  -------  ----------------------------\n");
    44117
    45         spec_directory dir(&fp);
    46118        for (int i = 0; i < dir.total; i++)
    47119        {
     
    78150            putchar('\n');
    79151        }
     152
     153        return EXIT_SUCCESS;
    80154    }
    81     /* else if (...) */
    82     else
     155
     156    if (cmd == CMD_GET)
    83157    {
    84         fprintf(stderr, "abuse-tool: unknown command `%s'\n", cmd);
     158        int id = atoi(argv[3]);
     159
     160        if (id < 0 || id >= dir.total)
     161        {
     162            fprintf(stderr, "abuse-tool: id %i not found in %s\n", id, file);
     163            return EXIT_FAILURE;
     164        }
     165
     166        spec_entry *se = dir.entries[id];
     167        fp.seek(se->offset, SEEK_SET);
     168
     169        for (size_t todo = se->size; todo > 0; )
     170        {
     171            uint8_t buf[1024];
     172            int step = Min(todo, 1024);
     173            fp.read(buf, step);
     174            fwrite(buf, step, 1, stdout);
     175            todo -= step;
     176        }
    85177    }
    86178
     
    93185            "Usage: abuse-tool <spec_file> <command> [args...]\n"
    94186            "List of available commands:\n"
    95             "  list -- list the contents of a SPEC file\n");
     187            "  list          list the contents of a SPEC file\n"
     188            "  get <id>      dump entry <id> to stdout\n");
    96189}
    97190
Note: See TracChangeset for help on using the changeset viewer.