test/test_physfs.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 27 Sep 2017 11:54:25 -0400
changeset 1613 ebe0ee3b7810
parent 1488 f42b226bbe2e
child 1627 fa8e38bcc354
child 1632 e9a16884a594
permissions -rw-r--r--
Bumped version to 3.0.0!
icculus@40
     1
/**
icculus@40
     2
 * Test program for PhysicsFS. May only work on Unix.
icculus@40
     3
 *
icculus@809
     4
 * Please see the file LICENSE.txt in the source's root directory.
icculus@40
     5
 *
icculus@40
     6
 *  This file written by Ryan C. Gordon.
icculus@40
     7
 */
icculus@40
     8
icculus@1455
     9
#define _CRT_SECURE_NO_WARNINGS 1
icculus@1455
    10
icculus@35
    11
#include <stdio.h>
icculus@38
    12
#include <stdlib.h>
icculus@40
    13
#include <errno.h>
icculus@72
    14
#include <string.h>
icculus@72
    15
icculus@164
    16
#if (defined __MWERKS__)
icculus@164
    17
#include <SIOUX.h>
icculus@164
    18
#endif
icculus@164
    19
icculus@214
    20
#if (defined PHYSFS_HAVE_READLINE)
icculus@40
    21
#include <unistd.h>
icculus@214
    22
#include <readline/readline.h>
icculus@214
    23
#include <readline/history.h>
icculus@72
    24
#endif
icculus@72
    25
icculus@244
    26
#include <time.h>
icculus@244
    27
icculus@1242
    28
/* Define this, so the compiler doesn't complain about using old APIs. */
icculus@1242
    29
#define PHYSFS_DEPRECATED
icculus@1242
    30
icculus@35
    31
#include "physfs.h"
icculus@35
    32
icculus@1613
    33
#define TEST_VERSION_MAJOR  3
icculus@1613
    34
#define TEST_VERSION_MINOR  0
icculus@956
    35
#define TEST_VERSION_PATCH  0
icculus@35
    36
icculus@40
    37
static FILE *history_file = NULL;
icculus@508
    38
static PHYSFS_uint32 do_buffer_size = 0;
icculus@40
    39
icculus@38
    40
static void output_versions(void)
icculus@35
    41
{
icculus@35
    42
    PHYSFS_Version compiled;
icculus@35
    43
    PHYSFS_Version linked;
icculus@35
    44
icculus@35
    45
    PHYSFS_VERSION(&compiled);
icculus@35
    46
    PHYSFS_getLinkedVersion(&linked);
icculus@35
    47
icculus@35
    48
    printf("test_physfs version %d.%d.%d.\n"
icculus@35
    49
           " Compiled against PhysicsFS version %d.%d.%d,\n"
icculus@35
    50
           " and linked against %d.%d.%d.\n\n",
icculus@35
    51
            TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
icculus@132
    52
            (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
icculus@132
    53
            (int) linked.major, (int) linked.minor, (int) linked.patch);
icculus@35
    54
} /* output_versions */
icculus@35
    55
icculus@35
    56
icculus@38
    57
static void output_archivers(void)
icculus@35
    58
{
icculus@35
    59
    const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
icculus@35
    60
    const PHYSFS_ArchiveInfo **i;
icculus@35
    61
icculus@35
    62
    printf("Supported archive types:\n");
icculus@35
    63
    if (*rc == NULL)
icculus@35
    64
        printf(" * Apparently, NONE!\n");
icculus@35
    65
    else
icculus@35
    66
    {
icculus@35
    67
        for (i = rc; *i != NULL; i++)
icculus@35
    68
        {
icculus@35
    69
            printf(" * %s: %s\n    Written by %s.\n    %s\n",
icculus@35
    70
                    (*i)->extension, (*i)->description,
icculus@35
    71
                    (*i)->author, (*i)->url);
icculus@1326
    72
            printf("    %s symbolic links.\n",
icculus@1326
    73
                    (*i)->supportsSymlinks ? "Supports" : "Does not support");
icculus@35
    74
        } /* for */
icculus@35
    75
    } /* else */
icculus@38
    76
icculus@38
    77
    printf("\n");
icculus@35
    78
} /* output_archivers */
icculus@35
    79
icculus@35
    80
icculus@40
    81
static int cmd_quit(char *args)
icculus@38
    82
{
icculus@1016
    83
    return 0;
icculus@38
    84
} /* cmd_quit */
icculus@38
    85
icculus@38
    86
icculus@40
    87
static int cmd_init(char *args)
icculus@40
    88
{
icculus@170
    89
    if (*args == '\"')
icculus@170
    90
    {
icculus@170
    91
        args++;
icculus@170
    92
        args[strlen(args) - 1] = '\0';
icculus@170
    93
    } /* if */
icculus@170
    94
icculus@40
    95
    if (PHYSFS_init(args))
icculus@40
    96
        printf("Successful.\n");
icculus@40
    97
    else
icculus@40
    98
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
    99
icculus@1016
   100
    return 1;
icculus@40
   101
} /* cmd_init */
icculus@40
   102
icculus@40
   103
icculus@40
   104
static int cmd_deinit(char *args)
icculus@40
   105
{
icculus@40
   106
    if (PHYSFS_deinit())
icculus@40
   107
        printf("Successful.\n");
icculus@40
   108
    else
icculus@40
   109
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   110
icculus@1016
   111
    return 1;
icculus@40
   112
} /* cmd_deinit */
icculus@40
   113
icculus@40
   114
icculus@40
   115
static int cmd_addarchive(char *args)
icculus@40
   116
{
icculus@165
   117
    char *ptr = strrchr(args, ' ');
icculus@40
   118
    int appending = atoi(ptr + 1);
icculus@40
   119
    *ptr = '\0';
icculus@40
   120
icculus@165
   121
    if (*args == '\"')
icculus@165
   122
    {
icculus@165
   123
        args++;
icculus@165
   124
        *(ptr - 1) = '\0';
icculus@170
   125
    } /* if */
icculus@165
   126
icculus@165
   127
    /*printf("[%s], [%d]\n", args, appending);*/
icculus@165
   128
icculus@1110
   129
    if (PHYSFS_mount(args, NULL, appending))
icculus@40
   130
        printf("Successful.\n");
icculus@40
   131
    else
icculus@40
   132
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   133
icculus@1016
   134
    return 1;
icculus@40
   135
} /* cmd_addarchive */
icculus@40
   136
icculus@40
   137
icculus@1120
   138
/* wrap free() to avoid calling convention wankery. */
icculus@1120
   139
static void freeBuf(void *buf)
icculus@1120
   140
{
icculus@1120
   141
    free(buf);
icculus@1120
   142
} /* freeBuf */
icculus@1120
   143
icculus@1123
   144
typedef enum
icculus@1123
   145
{
icculus@1123
   146
    MNTTYPE_PATH,
icculus@1123
   147
    MNTTYPE_MEMORY,
icculus@1123
   148
    MNTTYPE_HANDLE
icculus@1123
   149
} MountType;
icculus@1120
   150
icculus@1123
   151
static int cmd_mount_internal(char *args, const MountType mnttype)
icculus@681
   152
{
icculus@681
   153
    char *ptr;
icculus@681
   154
    char *mntpoint = NULL;
icculus@681
   155
    int appending = 0;
icculus@1120
   156
    int rc = 0;
icculus@681
   157
icculus@681
   158
    if (*args == '\"')
icculus@681
   159
    {
icculus@681
   160
        args++;
icculus@681
   161
        ptr = strchr(args, '\"');
icculus@681
   162
        if (ptr == NULL)
icculus@681
   163
        {
icculus@681
   164
            printf("missing string terminator in argument.\n");
icculus@1016
   165
            return 1;
icculus@681
   166
        } /* if */
icculus@681
   167
        *(ptr) = '\0';
icculus@681
   168
    } /* if */
icculus@681
   169
    else
icculus@681
   170
    {
icculus@681
   171
        ptr = strchr(args, ' ');
icculus@681
   172
        *ptr = '\0';
icculus@681
   173
    } /* else */
icculus@681
   174
icculus@681
   175
    mntpoint = ptr + 1;
icculus@681
   176
    if (*mntpoint == '\"')
icculus@681
   177
    {
icculus@681
   178
        mntpoint++;
icculus@681
   179
        ptr = strchr(mntpoint, '\"');
icculus@681
   180
        if (ptr == NULL)
icculus@681
   181
        {
icculus@681
   182
            printf("missing string terminator in argument.\n");
icculus@1016
   183
            return 1;
icculus@681
   184
        } /* if */
icculus@681
   185
        *(ptr) = '\0';
icculus@681
   186
    } /* if */
icculus@681
   187
    else
icculus@681
   188
    {
icculus@681
   189
        ptr = strchr(mntpoint, ' ');
icculus@681
   190
        *(ptr) = '\0';
icculus@681
   191
    } /* else */
icculus@681
   192
    appending = atoi(ptr + 1);
icculus@681
   193
icculus@689
   194
    /*printf("[%s], [%s], [%d]\n", args, mntpoint, appending);*/
icculus@681
   195
icculus@1123
   196
    if (mnttype == MNTTYPE_PATH)
icculus@1120
   197
        rc = PHYSFS_mount(args, mntpoint, appending);
icculus@1123
   198
icculus@1123
   199
    else if (mnttype == MNTTYPE_HANDLE)
icculus@1123
   200
    {
icculus@1123
   201
        PHYSFS_File *f = PHYSFS_openRead(args);
icculus@1123
   202
        if (f == NULL)
icculus@1123
   203
        {
icculus@1123
   204
            printf("PHYSFS_openRead('%s') failed. reason: %s.\n", args, PHYSFS_getLastError());
icculus@1123
   205
            return 1;
icculus@1123
   206
        } /* if */
icculus@1123
   207
icculus@1123
   208
        rc = PHYSFS_mountHandle(f, args, mntpoint, appending);
icculus@1123
   209
        if (!rc)
icculus@1123
   210
            PHYSFS_close(f);
icculus@1123
   211
    } /* else if */
icculus@1123
   212
icculus@1123
   213
    else if (mnttype == MNTTYPE_MEMORY)
icculus@1120
   214
    {
icculus@1120
   215
        FILE *in = fopen(args, "rb");
icculus@1120
   216
        void *buf = NULL;
icculus@1120
   217
        long len = 0;
icculus@1120
   218
icculus@1120
   219
        if (in == NULL)
icculus@1120
   220
        {
icculus@1120
   221
            printf("Failed to open %s to read into memory: %s.\n", args, strerror(errno));
icculus@1120
   222
            return 1;
icculus@1120
   223
        } /* if */
icculus@1120
   224
icculus@1120
   225
        if ( (fseek(in, 0, SEEK_END) != 0) || ((len = ftell(in)) < 0) )
icculus@1120
   226
        {
icculus@1120
   227
            printf("Failed to find size of %s to read into memory: %s.\n", args, strerror(errno));
icculus@1120
   228
            fclose(in);
icculus@1120
   229
            return 1;
icculus@1120
   230
        } /* if */
icculus@1120
   231
icculus@1120
   232
        buf = malloc(len);
icculus@1120
   233
        if (buf == NULL)
icculus@1120
   234
        {
icculus@1120
   235
            printf("Failed to allocate space to read %s into memory: %s.\n", args, strerror(errno));
icculus@1120
   236
            fclose(in);
icculus@1120
   237
            return 1;
icculus@1120
   238
        } /* if */
icculus@1120
   239
icculus@1120
   240
        if ((fseek(in, 0, SEEK_SET) != 0) || (fread(buf, len, 1, in) != 1))
icculus@1120
   241
        {
icculus@1120
   242
            printf("Failed to read %s into memory: %s.\n", args, strerror(errno));
icculus@1120
   243
            fclose(in);
icculus@1120
   244
            free(buf);
icculus@1120
   245
            return 1;
icculus@1120
   246
        } /* if */
icculus@1120
   247
icculus@1120
   248
        fclose(in);
icculus@1120
   249
icculus@1120
   250
        rc = PHYSFS_mountMemory(buf, len, freeBuf, args, mntpoint, appending);
icculus@1120
   251
    } /* else */
icculus@1120
   252
icculus@1120
   253
    if (rc)
icculus@681
   254
        printf("Successful.\n");
icculus@681
   255
    else
icculus@681
   256
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@681
   257
icculus@1016
   258
    return 1;
icculus@1120
   259
} /* cmd_mount_internal */
icculus@1120
   260
icculus@1120
   261
icculus@1120
   262
static int cmd_mount(char *args)
icculus@1120
   263
{
icculus@1123
   264
    return cmd_mount_internal(args, MNTTYPE_PATH);
icculus@681
   265
} /* cmd_mount */
icculus@681
   266
icculus@681
   267
icculus@1120
   268
static int cmd_mount_mem(char *args)
icculus@1120
   269
{
icculus@1123
   270
    return cmd_mount_internal(args, MNTTYPE_MEMORY);
icculus@1120
   271
} /* cmd_mount_mem */
icculus@1120
   272
icculus@1120
   273
icculus@1123
   274
static int cmd_mount_handle(char *args)
icculus@1123
   275
{
icculus@1123
   276
    return cmd_mount_internal(args, MNTTYPE_HANDLE);
icculus@1123
   277
} /* cmd_mount_handle */
icculus@1123
   278
icculus@1282
   279
static int cmd_getmountpoint(char *args)
icculus@1282
   280
{
icculus@1282
   281
    if (*args == '\"')
icculus@1282
   282
    {
icculus@1282
   283
        args++;
icculus@1282
   284
        args[strlen(args) - 1] = '\0';
icculus@1282
   285
    } /* if */
icculus@1282
   286
icculus@1282
   287
    printf("Dir [%s] is mounted at [%s].\n", args, PHYSFS_getMountPoint(args));
icculus@1282
   288
    return 1;
icculus@1282
   289
} /* cmd_getmountpoint */
icculus@1123
   290
icculus@40
   291
static int cmd_removearchive(char *args)
icculus@40
   292
{
icculus@170
   293
    if (*args == '\"')
icculus@170
   294
    {
icculus@170
   295
        args++;
icculus@170
   296
        args[strlen(args) - 1] = '\0';
icculus@170
   297
    } /* if */
icculus@170
   298
icculus@1110
   299
    if (PHYSFS_unmount(args))
icculus@40
   300
        printf("Successful.\n");
icculus@40
   301
    else
icculus@40
   302
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   303
icculus@1016
   304
    return 1;
icculus@40
   305
} /* cmd_removearchive */
icculus@40
   306
icculus@40
   307
icculus@40
   308
static int cmd_enumerate(char *args)
icculus@40
   309
{
icculus@170
   310
    char **rc;
icculus@170
   311
icculus@170
   312
    if (*args == '\"')
icculus@170
   313
    {
icculus@170
   314
        args++;
icculus@170
   315
        args[strlen(args) - 1] = '\0';
icculus@170
   316
    } /* if */
icculus@170
   317
icculus@170
   318
    rc = PHYSFS_enumerateFiles(args);
icculus@40
   319
icculus@40
   320
    if (rc == NULL)
icculus@40
   321
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   322
    else
icculus@40
   323
    {
icculus@40
   324
        int file_count;
icculus@40
   325
        char **i;
icculus@40
   326
        for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
icculus@40
   327
            printf("%s\n", *i);
icculus@40
   328
icculus@40
   329
        printf("\n total (%d) files.\n", file_count);
icculus@40
   330
        PHYSFS_freeList(rc);
icculus@40
   331
    } /* else */
icculus@40
   332
icculus@1016
   333
    return 1;
icculus@40
   334
} /* cmd_enumerate */
icculus@40
   335
icculus@40
   336
icculus@40
   337
static int cmd_getdirsep(char *args)
icculus@40
   338
{
icculus@40
   339
    printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
icculus@1016
   340
    return 1;
icculus@40
   341
} /* cmd_getdirsep */
icculus@40
   342
icculus@40
   343
icculus@40
   344
static int cmd_getlasterror(char *args)
icculus@40
   345
{
icculus@40
   346
    printf("last error is [%s].\n", PHYSFS_getLastError());
icculus@1016
   347
    return 1;
icculus@40
   348
} /* cmd_getlasterror */
icculus@40
   349
icculus@40
   350
icculus@40
   351
static int cmd_getcdromdirs(char *args)
icculus@40
   352
{
icculus@40
   353
    char **rc = PHYSFS_getCdRomDirs();
icculus@40
   354
icculus@40
   355
    if (rc == NULL)
icculus@170
   356
        printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
icculus@40
   357
    else
icculus@40
   358
    {
icculus@40
   359
        int dir_count;
icculus@40
   360
        char **i;
icculus@40
   361
        for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
icculus@40
   362
            printf("%s\n", *i);
icculus@40
   363
icculus@40
   364
        printf("\n total (%d) drives.\n", dir_count);
icculus@40
   365
        PHYSFS_freeList(rc);
icculus@40
   366
    } /* else */
icculus@40
   367
icculus@1016
   368
    return 1;
icculus@40
   369
} /* cmd_getcdromdirs */
icculus@40
   370
icculus@40
   371
icculus@40
   372
static int cmd_getsearchpath(char *args)
icculus@40
   373
{
icculus@40
   374
    char **rc = PHYSFS_getSearchPath();
icculus@40
   375
icculus@40
   376
    if (rc == NULL)
icculus@40
   377
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   378
    else
icculus@40
   379
    {
icculus@40
   380
        int dir_count;
icculus@40
   381
        char **i;
icculus@40
   382
        for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
icculus@40
   383
            printf("%s\n", *i);
icculus@40
   384
icculus@40
   385
        printf("\n total (%d) directories.\n", dir_count);
icculus@40
   386
        PHYSFS_freeList(rc);
icculus@40
   387
    } /* else */
icculus@40
   388
icculus@1016
   389
    return 1;
icculus@40
   390
} /* cmd_getcdromdirs */
icculus@40
   391
icculus@40
   392
icculus@40
   393
static int cmd_getbasedir(char *args)
icculus@40
   394
{
icculus@40
   395
    printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
icculus@1016
   396
    return 1;
icculus@40
   397
} /* cmd_getbasedir */
icculus@40
   398
icculus@40
   399
icculus@40
   400
static int cmd_getuserdir(char *args)
icculus@40
   401
{
icculus@40
   402
    printf("User dir is [%s].\n", PHYSFS_getUserDir());
icculus@1016
   403
    return 1;
icculus@40
   404
} /* cmd_getuserdir */
icculus@40
   405
icculus@40
   406
icculus@1242
   407
static int cmd_getprefdir(char *args)
icculus@1242
   408
{
icculus@1242
   409
    char *org;
icculus@1242
   410
    char *appName;
icculus@1242
   411
    char *ptr = args;
icculus@1242
   412
icculus@1242
   413
    org = ptr;
icculus@1242
   414
    ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
icculus@1242
   415
    printf("Pref dir is [%s].\n", PHYSFS_getPrefDir(org, appName));
icculus@1242
   416
    return 1;
icculus@1242
   417
} /* cmd_getprefdir */
icculus@1242
   418
icculus@1242
   419
icculus@40
   420
static int cmd_getwritedir(char *args)
icculus@40
   421
{
icculus@40
   422
    printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
icculus@1016
   423
    return 1;
icculus@40
   424
} /* cmd_getwritedir */
icculus@40
   425
icculus@40
   426
icculus@40
   427
static int cmd_setwritedir(char *args)
icculus@40
   428
{
icculus@170
   429
    if (*args == '\"')
icculus@170
   430
    {
icculus@170
   431
        args++;
icculus@170
   432
        args[strlen(args) - 1] = '\0';
icculus@170
   433
    } /* if */
icculus@170
   434
icculus@40
   435
    if (PHYSFS_setWriteDir(args))
icculus@40
   436
        printf("Successful.\n");
icculus@40
   437
    else
icculus@40
   438
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   439
icculus@1016
   440
    return 1;
icculus@40
   441
} /* cmd_setwritedir */
icculus@40
   442
icculus@40
   443
icculus@40
   444
static int cmd_permitsyms(char *args)
icculus@40
   445
{
icculus@170
   446
    int num;
icculus@170
   447
icculus@170
   448
    if (*args == '\"')
icculus@170
   449
    {
icculus@170
   450
        args++;
icculus@170
   451
        args[strlen(args) - 1] = '\0';
icculus@170
   452
    } /* if */
icculus@170
   453
icculus@170
   454
    num = atoi(args);
icculus@40
   455
    PHYSFS_permitSymbolicLinks(num);
icculus@40
   456
    printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
icculus@1016
   457
    return 1;
icculus@40
   458
} /* cmd_permitsyms */
icculus@40
   459
icculus@40
   460
icculus@508
   461
static int cmd_setbuffer(char *args)
icculus@508
   462
{
icculus@508
   463
    if (*args == '\"')
icculus@508
   464
    {
icculus@508
   465
        args++;
icculus@508
   466
        args[strlen(args) - 1] = '\0';
icculus@508
   467
    } /* if */
icculus@508
   468
icculus@538
   469
    do_buffer_size = (unsigned int) atoi(args);
icculus@508
   470
    if (do_buffer_size)
icculus@508
   471
    {
icculus@508
   472
        printf("Further tests will set a (%lu) size buffer.\n",
icculus@508
   473
                (unsigned long) do_buffer_size);
icculus@508
   474
    } /* if */
icculus@508
   475
icculus@508
   476
    else
icculus@508
   477
    {
icculus@508
   478
        printf("Further tests will NOT use a buffer.\n");
icculus@508
   479
    } /* else */
icculus@508
   480
icculus@1016
   481
    return 1;
icculus@508
   482
} /* cmd_setbuffer */
icculus@508
   483
icculus@508
   484
icculus@508
   485
static int cmd_stressbuffer(char *args)
icculus@508
   486
{
icculus@508
   487
    int num;
icculus@508
   488
icculus@508
   489
    if (*args == '\"')
icculus@508
   490
    {
icculus@508
   491
        args++;
icculus@508
   492
        args[strlen(args) - 1] = '\0';
icculus@508
   493
    } /* if */
icculus@508
   494
icculus@508
   495
    num = atoi(args);
icculus@508
   496
    if (num < 0)
icculus@508
   497
        printf("buffer must be greater than or equal to zero.\n");
icculus@508
   498
    else
icculus@508
   499
    {
icculus@654
   500
        PHYSFS_File *f;
icculus@508
   501
        int rndnum;
icculus@508
   502
icculus@508
   503
        printf("Stress testing with (%d) byte buffer...\n", num);
icculus@508
   504
        f = PHYSFS_openWrite("test.txt");
icculus@508
   505
        if (f == NULL)
icculus@508
   506
            printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
icculus@508
   507
        else
icculus@508
   508
        {
icculus@508
   509
            int i, j;
icculus@508
   510
            char buf[37];
icculus@508
   511
            char buf2[37];
icculus@508
   512
icculus@508
   513
            if (!PHYSFS_setBuffer(f, num))
icculus@508
   514
            {
icculus@508
   515
                printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   516
                PHYSFS_close(f);
icculus@508
   517
                PHYSFS_delete("test.txt");
icculus@1016
   518
                return 1;
icculus@508
   519
            } /* if */
icculus@508
   520
icculus@508
   521
            strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
icculus@538
   522
            srand((unsigned int) time(NULL));
icculus@508
   523
icculus@508
   524
            for (i = 0; i < 10; i++)
icculus@508
   525
            {
icculus@508
   526
                for (j = 0; j < 10000; j++)
icculus@508
   527
                {
icculus@538
   528
                    PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
icculus@538
   529
                    PHYSFS_uint32 left = 36 - right;
icculus@1102
   530
                    if (PHYSFS_writeBytes(f, buf, left) != left)
icculus@508
   531
                    {
icculus@1102
   532
                        printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   533
                        PHYSFS_close(f);
icculus@1016
   534
                        return 1;
icculus@508
   535
                    } /* if */
icculus@508
   536
icculus@508
   537
                    rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
icculus@508
   538
                    if (rndnum == 42)
icculus@508
   539
                    {
icculus@508
   540
                        if (!PHYSFS_flush(f))
icculus@508
   541
                        {
icculus@508
   542
                            printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   543
                            PHYSFS_close(f);
icculus@1016
   544
                            return 1;
icculus@508
   545
                        } /* if */
icculus@508
   546
                    } /* if */
icculus@508
   547
icculus@1102
   548
                    if (PHYSFS_writeBytes(f, buf + left, right) != right)
icculus@508
   549
                    {
icculus@1102
   550
                        printf("PHYSFS_writeBytes() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   551
                        PHYSFS_close(f);
icculus@1016
   552
                        return 1;
icculus@508
   553
                    } /* if */
icculus@508
   554
icculus@508
   555
                    rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
icculus@508
   556
                    if (rndnum == 42)
icculus@508
   557
                    {
icculus@508
   558
                        if (!PHYSFS_flush(f))
icculus@508
   559
                        {
icculus@508
   560
                            printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   561
                            PHYSFS_close(f);
icculus@1016
   562
                            return 1;
icculus@508
   563
                        } /* if */
icculus@508
   564
                    } /* if */
icculus@508
   565
                } /* for */
icculus@508
   566
icculus@508
   567
                if (!PHYSFS_flush(f))
icculus@508
   568
                {
icculus@508
   569
                    printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   570
                    PHYSFS_close(f);
icculus@1016
   571
                    return 1;
icculus@508
   572
                } /* if */
icculus@508
   573
icculus@508
   574
            } /* for */
icculus@508
   575
icculus@508
   576
            if (!PHYSFS_close(f))
icculus@508
   577
            {
icculus@508
   578
                printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
icculus@1016
   579
                return 1;  /* oh well. */
icculus@508
   580
            } /* if */
icculus@508
   581
icculus@508
   582
            printf(" ... test file written ...\n");
icculus@508
   583
            f = PHYSFS_openRead("test.txt");
icculus@508
   584
            if (f == NULL)
icculus@508
   585
            {
icculus@508
   586
                printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
icculus@1016
   587
                return 1;
icculus@508
   588
            } /* if */
icculus@508
   589
icculus@508
   590
            if (!PHYSFS_setBuffer(f, num))
icculus@508
   591
            {
icculus@508
   592
                printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   593
                PHYSFS_close(f);
icculus@1016
   594
                return 1;
icculus@508
   595
            } /* if */
icculus@508
   596
icculus@508
   597
            for (i = 0; i < 10; i++)
icculus@508
   598
            {
icculus@508
   599
                for (j = 0; j < 10000; j++)
icculus@508
   600
                {
icculus@538
   601
                    PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
icculus@538
   602
                    PHYSFS_uint32 left = 36 - right;
icculus@1102
   603
                    if (PHYSFS_readBytes(f, buf2, left) != left)
icculus@508
   604
                    {
icculus@1102
   605
                        printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   606
                        PHYSFS_close(f);
icculus@1016
   607
                        return 1;
icculus@508
   608
                    } /* if */
icculus@508
   609
icculus@508
   610
                    rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
icculus@508
   611
                    if (rndnum == 42)
icculus@508
   612
                    {
icculus@508
   613
                        if (!PHYSFS_flush(f))
icculus@508
   614
                        {
icculus@508
   615
                            printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   616
                            PHYSFS_close(f);
icculus@1016
   617
                            return 1;
icculus@508
   618
                        } /* if */
icculus@508
   619
                    } /* if */
icculus@508
   620
icculus@1102
   621
                    if (PHYSFS_readBytes(f, buf2 + left, right) != right)
icculus@508
   622
                    {
icculus@1102
   623
                        printf("PHYSFS_readBytes() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   624
                        PHYSFS_close(f);
icculus@1016
   625
                        return 1;
icculus@508
   626
                    } /* if */
icculus@508
   627
icculus@508
   628
                    rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
icculus@508
   629
                    if (rndnum == 42)
icculus@508
   630
                    {
icculus@508
   631
                        if (!PHYSFS_flush(f))
icculus@508
   632
                        {
icculus@508
   633
                            printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   634
                            PHYSFS_close(f);
icculus@1016
   635
                            return 1;
icculus@508
   636
                        } /* if */
icculus@508
   637
                    } /* if */
icculus@508
   638
icculus@508
   639
                    if (memcmp(buf, buf2, 36) != 0)
icculus@508
   640
                    {
icculus@508
   641
                        printf("readback is mismatched on iterations (%d, %d).\n", i, j);
icculus@508
   642
                        printf("wanted: [");
icculus@508
   643
                        for (i = 0; i < 36; i++)
icculus@508
   644
                            printf("%c", buf[i]);
icculus@508
   645
                        printf("]\n");
icculus@508
   646
icculus@508
   647
                        printf("   got: [");
icculus@508
   648
                        for (i = 0; i < 36; i++)
icculus@508
   649
                            printf("%c", buf2[i]);
icculus@508
   650
                        printf("]\n");
icculus@508
   651
                        PHYSFS_close(f);
icculus@1016
   652
                        return 1;
icculus@508
   653
                    } /* if */
icculus@508
   654
                } /* for */
icculus@508
   655
icculus@508
   656
                if (!PHYSFS_flush(f))
icculus@508
   657
                {
icculus@508
   658
                    printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
icculus@508
   659
                    PHYSFS_close(f);
icculus@1016
   660
                    return 1;
icculus@508
   661
                } /* if */
icculus@508
   662
icculus@508
   663
            } /* for */
icculus@508
   664
icculus@508
   665
            printf(" ... test file read ...\n");
icculus@508
   666
icculus@508
   667
            if (!PHYSFS_eof(f))
icculus@508
   668
                printf("PHYSFS_eof() returned true! That's wrong.\n");
icculus@508
   669
icculus@508
   670
            if (!PHYSFS_close(f))
icculus@508
   671
            {
icculus@508
   672
                printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
icculus@1016
   673
                return 1;  /* oh well. */
icculus@508
   674
            } /* if */
icculus@508
   675
icculus@508
   676
            PHYSFS_delete("test.txt");
icculus@508
   677
            printf("stress test completed successfully.\n");
icculus@508
   678
        } /* else */
icculus@508
   679
    } /* else */
icculus@508
   680
icculus@1016
   681
    return 1;
icculus@508
   682
} /* cmd_stressbuffer */
icculus@508
   683
icculus@508
   684
icculus@40
   685
static int cmd_setsaneconfig(char *args)
icculus@40
   686
{
icculus@101
   687
    char *org;
icculus@40
   688
    char *appName;
icculus@40
   689
    char *arcExt;
icculus@40
   690
    int inclCD;
icculus@40
   691
    int arcsFirst;
icculus@40
   692
    char *ptr = args;
icculus@40
   693
icculus@40
   694
        /* ugly. */
icculus@101
   695
    org = ptr;
icculus@101
   696
    ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
icculus@40
   697
    ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
icculus@40
   698
    ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
icculus@40
   699
    arcsFirst = atoi(ptr);
icculus@40
   700
icculus@40
   701
    if (strcmp(arcExt, "!") == 0)
icculus@40
   702
        arcExt = NULL;
icculus@40
   703
icculus@101
   704
    if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
icculus@40
   705
        printf("Successful.\n");
icculus@40
   706
    else
icculus@40
   707
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@40
   708
icculus@1016
   709
    return 1;
icculus@40
   710
} /* cmd_setsaneconfig */
icculus@40
   711
icculus@40
   712
icculus@42
   713
static int cmd_mkdir(char *args)
icculus@42
   714
{
icculus@170
   715
    if (*args == '\"')
icculus@170
   716
    {
icculus@170
   717
        args++;
icculus@170
   718
        args[strlen(args) - 1] = '\0';
icculus@170
   719
    } /* if */
icculus@170
   720
icculus@42
   721
    if (PHYSFS_mkdir(args))
icculus@42
   722
        printf("Successful.\n");
icculus@42
   723
    else
icculus@42
   724
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@42
   725
icculus@1016
   726
    return 1;
icculus@42
   727
} /* cmd_mkdir */
icculus@42
   728
icculus@42
   729
icculus@42
   730
static int cmd_delete(char *args)
icculus@42
   731
{
icculus@170
   732
    if (*args == '\"')
icculus@170
   733
    {
icculus@170
   734
        args++;
icculus@170
   735
        args[strlen(args) - 1] = '\0';
icculus@170
   736
    } /* if */
icculus@170
   737
icculus@42
   738
    if (PHYSFS_delete(args))
icculus@42
   739
        printf("Successful.\n");
icculus@42
   740
    else
icculus@42
   741
        printf("Failure. reason: %s.\n", PHYSFS_getLastError());
icculus@42
   742
icculus@1016
   743
    return 1;
icculus@42
   744
} /* cmd_delete */
icculus@42
   745
icculus@42
   746
icculus@42
   747
static int cmd_getrealdir(char *args)
icculus@42
   748
{
icculus@170
   749
    const char *rc;
icculus@170
   750
icculus@170
   751
    if (*args == '\"')
icculus@170
   752
    {
icculus@170
   753
        args++;
icculus@170
   754
        args[strlen(args) - 1] = '\0';
icculus@170
   755
    } /* if */
icculus@170
   756
icculus@170
   757
    rc = PHYSFS_getRealDir(args);
icculus@42
   758
    if (rc)
icculus@42
   759
        printf("Found at [%s].\n", rc);
icculus@42
   760
    else
icculus@42
   761
        printf("Not found.\n");
icculus@42
   762
icculus@1016
   763
    return 1;
icculus@42
   764
} /* cmd_getrealdir */
icculus@42
   765
icculus@42
   766
icculus@42
   767
static int cmd_exists(char *args)
icculus@42
   768
{
icculus@170
   769
    int rc;
icculus@170
   770
icculus@170
   771
    if (*args == '\"')
icculus@170
   772
    {
icculus@170
   773
        args++;
icculus@170
   774
        args[strlen(args) - 1] = '\0';
icculus@170
   775
    } /* if */
icculus@170
   776
icculus@170
   777
    rc = PHYSFS_exists(args);
icculus@42
   778
    printf("File %sexists.\n", rc ? "" : "does not ");
icculus@1016
   779
    return 1;
icculus@42
   780
} /* cmd_exists */
icculus@42
   781
icculus@42
   782
icculus@42
   783
static int cmd_isdir(char *args)
icculus@42
   784
{
icculus@1125
   785
    PHYSFS_Stat statbuf;
icculus@170
   786
    int rc;
icculus@170
   787
icculus@170
   788
    if (*args == '\"')
icculus@170
   789
    {
icculus@170
   790
        args++;
icculus@170
   791
        args[strlen(args) - 1] = '\0';
icculus@170
   792
    } /* if */
icculus@170
   793
icculus@1125
   794
    rc = PHYSFS_stat(args, &statbuf);
icculus@1125
   795
    if (rc)
icculus@1125
   796
        rc = (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
icculus@42
   797
    printf("File %s a directory.\n", rc ? "is" : "is NOT");
icculus@1016
   798
    return 1;
icculus@42
   799
} /* cmd_isdir */
icculus@42
   800
icculus@42
   801
icculus@42
   802
static int cmd_issymlink(char *args)
icculus@42
   803
{
icculus@1125
   804
    PHYSFS_Stat statbuf;
icculus@170
   805
    int rc;
icculus@170
   806
icculus@170
   807
    if (*args == '\"')
icculus@170
   808
    {
icculus@170
   809
        args++;
icculus@170
   810
        args[strlen(args) - 1] = '\0';
icculus@170
   811
    } /* if */
icculus@170
   812
icculus@1125
   813
    rc = PHYSFS_stat(args, &statbuf);
icculus@1125
   814
    if (rc)
icculus@1125
   815
        rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
icculus@42
   816
    printf("File %s a symlink.\n", rc ? "is" : "is NOT");
icculus@1016
   817
    return 1;
icculus@42
   818
} /* cmd_issymlink */
icculus@42
   819
icculus@42
   820
icculus@108
   821
static int cmd_cat(char *args)
icculus@108
   822
{
icculus@654
   823
    PHYSFS_File *f;
icculus@170
   824
icculus@170
   825
    if (*args == '\"')
icculus@170
   826
    {
icculus@170
   827
        args++;
icculus@170
   828
        args[strlen(args) - 1] = '\0';
icculus@170
   829
    } /* if */
icculus@170
   830
icculus@170
   831
    f = PHYSFS_openRead(args);
icculus@108
   832
    if (f == NULL)
icculus@108
   833
        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
icculus@108
   834
    else
icculus@108
   835
    {
icculus@508
   836
        if (do_buffer_size)
icculus@508
   837
        {
icculus@508
   838
            if (!PHYSFS_setBuffer(f, do_buffer_size))
icculus@508
   839
            {
icculus@508
   840
                printf("failed to set file buffer. Reason: [%s].\n",
icculus@508
   841
                        PHYSFS_getLastError());
icculus@508
   842
                PHYSFS_close(f);
icculus@1016
   843
                return 1;
icculus@508
   844
            } /* if */
icculus@508
   845
        } /* if */
icculus@508
   846
icculus@108
   847
        while (1)
icculus@108
   848
        {
icculus@108
   849
            char buffer[128];
icculus@132
   850
            PHYSFS_sint64 rc;
icculus@132
   851
            PHYSFS_sint64 i;
icculus@1102
   852
            rc = PHYSFS_readBytes(f, buffer, sizeof (buffer));
icculus@108
   853
icculus@108
   854
            for (i = 0; i < rc; i++)
icculus@108
   855
                fputc((int) buffer[i], stdout);
icculus@108
   856
icculus@108
   857
            if (rc < sizeof (buffer))
icculus@108
   858
            {
icculus@108
   859
                printf("\n\n");
icculus@108
   860
                if (!PHYSFS_eof(f))
icculus@132
   861
                {
icculus@132
   862
                    printf("\n (Error condition in reading. Reason: [%s])\n\n",
icculus@132
   863
                           PHYSFS_getLastError());
icculus@132
   864
                } /* if */
icculus@108
   865
                PHYSFS_close(f);
icculus@1016
   866
                return 1;
icculus@108
   867
            } /* if */
icculus@108
   868
        } /* while */
icculus@108
   869
    } /* else */
icculus@108
   870
icculus@1016
   871
    return 1;
icculus@108
   872
} /* cmd_cat */
icculus@108
   873
icculus@1464
   874
static int cmd_cat2(char *args)
icculus@1464
   875
{
icculus@1472
   876
    PHYSFS_File *f1 = NULL;
icculus@1472
   877
    PHYSFS_File *f2 = NULL;
icculus@1464
   878
    char *fname1;
icculus@1464
   879
    char *fname2;
icculus@1464
   880
    char *ptr;
icculus@1464
   881
icculus@1464
   882
    fname1 = args;
icculus@1464
   883
    if (*fname1 == '\"')
icculus@1464
   884
    {
icculus@1464
   885
        fname1++;
icculus@1464
   886
        ptr = strchr(fname1, '\"');
icculus@1464
   887
        if (ptr == NULL)
icculus@1464
   888
        {
icculus@1464
   889
            printf("missing string terminator in argument.\n");
icculus@1464
   890
            return 1;
icculus@1464
   891
        } /* if */
icculus@1464
   892
        *(ptr) = '\0';
icculus@1464
   893
    } /* if */
icculus@1464
   894
    else
icculus@1464
   895
    {
icculus@1464
   896
        ptr = strchr(fname1, ' ');
icculus@1464
   897
        *ptr = '\0';
icculus@1464
   898
    } /* else */
icculus@1464
   899
icculus@1464
   900
    fname2 = ptr + 1;
icculus@1464
   901
    if (*fname2 == '\"')
icculus@1464
   902
    {
icculus@1464
   903
        fname2++;
icculus@1464
   904
        ptr = strchr(fname2, '\"');
icculus@1464
   905
        if (ptr == NULL)
icculus@1464
   906
        {
icculus@1464
   907
            printf("missing string terminator in argument.\n");
icculus@1464
   908
            return 1;
icculus@1464
   909
        } /* if */
icculus@1464
   910
        *(ptr) = '\0';
icculus@1464
   911
    } /* if */
icculus@1464
   912
icculus@1464
   913
    if ((f1 = PHYSFS_openRead(fname1)) == NULL)
icculus@1464
   914
        printf("failed to open '%s'. Reason: [%s].\n", fname1, PHYSFS_getLastError());
icculus@1464
   915
    else if ((f2 = PHYSFS_openRead(fname2)) == NULL)
icculus@1464
   916
        printf("failed to open '%s'. Reason: [%s].\n", fname2, PHYSFS_getLastError());
icculus@1464
   917
    else
icculus@1464
   918
    {
icculus@1464
   919
        char *buffer1 = NULL;
icculus@1464
   920
        size_t buffer1len = 0;
icculus@1464
   921
        char *buffer2 = NULL;
icculus@1464
   922
        size_t buffer2len = 0;
icculus@1464
   923
        char *ptr = NULL;
icculus@1488
   924
        size_t i;
icculus@1464
   925
icculus@1464
   926
        if (do_buffer_size)
icculus@1464
   927
        {
icculus@1464
   928
            if (!PHYSFS_setBuffer(f1, do_buffer_size))
icculus@1464
   929
            {
icculus@1464
   930
                printf("failed to set file buffer for '%s'. Reason: [%s].\n",
icculus@1464
   931
                        fname1, PHYSFS_getLastError());
icculus@1464
   932
                PHYSFS_close(f1);
icculus@1464
   933
                PHYSFS_close(f2);
icculus@1464
   934
                return 1;
icculus@1464
   935
            } /* if */
icculus@1464
   936
            else if (!PHYSFS_setBuffer(f2, do_buffer_size))
icculus@1464
   937
            {
icculus@1464
   938
                printf("failed to set file buffer for '%s'. Reason: [%s].\n",
icculus@1464
   939
                        fname2, PHYSFS_getLastError());
icculus@1464
   940
                PHYSFS_close(f1);
icculus@1464
   941
                PHYSFS_close(f2);
icculus@1464
   942
                return 1;
icculus@1464
   943
            } /* if */
icculus@1464
   944
        } /* if */
icculus@1464
   945
icculus@1464
   946
icculus@1464
   947
        do
icculus@1464
   948
        {
icculus@1464
   949
            int readlen = 128;
icculus@1464
   950
            PHYSFS_sint64 rc;
icculus@1464
   951
icculus@1464
   952
            ptr = realloc(buffer1, buffer1len + readlen);
icculus@1464
   953
            if (!ptr)
icculus@1464
   954
            {
icculus@1464
   955
                printf("(Out of memory.)\n\n");
icculus@1464
   956
                free(buffer1);
icculus@1464
   957
                free(buffer2);
icculus@1464
   958
                PHYSFS_close(f1);
icculus@1464
   959
                PHYSFS_close(f2);
icculus@1464
   960
                return 1;
icculus@1464
   961
            } /* if */
icculus@1464
   962
icculus@1464
   963
            buffer1 = ptr;
icculus@1464
   964
            rc = PHYSFS_readBytes(f1, buffer1 + buffer1len, readlen);
icculus@1464
   965
            if (rc < 0)
icculus@1464
   966
            {
icculus@1464
   967
                printf("(Error condition in reading '%s'. Reason: [%s])\n\n",
icculus@1464
   968
                       fname1, PHYSFS_getLastError());
icculus@1464
   969
                free(buffer1);
icculus@1464
   970
                free(buffer2);
icculus@1464
   971
                PHYSFS_close(f1);
icculus@1464
   972
                PHYSFS_close(f2);
icculus@1464
   973
                return 1;
icculus@1464
   974
            } /* if */
icculus@1481
   975
            buffer1len += (size_t) rc;
icculus@1464
   976
icculus@1464
   977
            ptr = realloc(buffer2, buffer2len + readlen);
icculus@1464
   978
            if (!ptr)
icculus@1464
   979
            {
icculus@1464
   980
                printf("(Out of memory.)\n\n");
icculus@1464
   981
                free(buffer1);
icculus@1464
   982
                free(buffer2);
icculus@1464
   983
                PHYSFS_close(f1);
icculus@1464
   984
                PHYSFS_close(f2);
icculus@1464
   985
                return 1;
icculus@1464
   986
            } /* if */
icculus@1464
   987
icculus@1464
   988
            buffer2 = ptr;
icculus@1464
   989
            rc = PHYSFS_readBytes(f2, buffer2 + buffer2len, readlen);
icculus@1464
   990
            if (rc < 0)
icculus@1464
   991
            {
icculus@1464
   992
                printf("(Error condition in reading '%s'. Reason: [%s])\n\n",
icculus@1464
   993
                       fname2, PHYSFS_getLastError());
icculus@1464
   994
                free(buffer1);
icculus@1464
   995
                free(buffer2);
icculus@1464
   996
                PHYSFS_close(f1);
icculus@1464
   997
                PHYSFS_close(f2);
icculus@1464
   998
                return 1;
icculus@1464
   999
            } /* if */
icculus@1481
  1000
            buffer2len += (size_t) rc;
icculus@1464
  1001
        } while (!PHYSFS_eof(f1) || !PHYSFS_eof(f2));
icculus@1464
  1002
icculus@1464
  1003
        printf("file '%s' ...\n\n", fname1);
icculus@1464
  1004
        for (i = 0; i < buffer1len; i++)
icculus@1464
  1005
            fputc((int) buffer1[i], stdout);
icculus@1464
  1006
        free(buffer1);
icculus@1464
  1007
icculus@1464
  1008
        printf("\n\nfile '%s' ...\n\n", fname2);
icculus@1464
  1009
        for (i = 0; i < buffer2len; i++)
icculus@1464
  1010
            fputc((int) buffer2[i], stdout);
icculus@1464
  1011
        free(buffer2);
icculus@1464
  1012
icculus@1464
  1013
        printf("\n\n");
icculus@1464
  1014
    } /* else */
icculus@1464
  1015
icculus@1464
  1016
    if (f1)
icculus@1464
  1017
        PHYSFS_close(f1);
icculus@1464
  1018
icculus@1464
  1019
    if (f2)
icculus@1464
  1020
        PHYSFS_close(f2);
icculus@1464
  1021
icculus@1464
  1022
    return 1;
icculus@1464
  1023
} /* cmd_cat2 */
icculus@1464
  1024
icculus@108
  1025
icculus@1063
  1026
#define CRC32_BUFFERSIZE 512
icculus@1063
  1027
static int cmd_crc32(char *args)
icculus@1063
  1028
{
icculus@1063
  1029
    PHYSFS_File *f;
icculus@1063
  1030
icculus@1063
  1031
    if (*args == '\"')
icculus@1063
  1032
    {
icculus@1063
  1033
        args++;
icculus@1063
  1034
        args[strlen(args) - 1] = '\0';
icculus@1063
  1035
    } /* if */
icculus@1063
  1036
icculus@1063
  1037
    f = PHYSFS_openRead(args);
icculus@1063
  1038
    if (f == NULL)
icculus@1063
  1039
        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
icculus@1063
  1040
    else
icculus@1063
  1041
    {
icculus@1063
  1042
        PHYSFS_uint8 buffer[CRC32_BUFFERSIZE];
icculus@1063
  1043
        PHYSFS_uint32 crc = -1;
icculus@1063
  1044
        PHYSFS_sint64 bytesread;
icculus@1063
  1045
icculus@1102
  1046
        while ((bytesread = PHYSFS_readBytes(f, buffer, CRC32_BUFFERSIZE)) > 0)
icculus@1063
  1047
        {
icculus@1063
  1048
            PHYSFS_uint32 i, bit;
icculus@1063
  1049
            for (i = 0; i < bytesread; i++)
icculus@1063
  1050
            {
icculus@1063
  1051
                for (bit = 0; bit < 8; bit++, buffer[i] >>= 1)
icculus@1063
  1052
                    crc = (crc >> 1) ^ (((crc ^ buffer[i]) & 1) ? 0xEDB88320 : 0);
icculus@1063
  1053
            } /* for */
icculus@1063
  1054
        } /* while */
icculus@1063
  1055
icculus@1063
  1056
        if (bytesread < 0)
icculus@1063
  1057
        {
icculus@1063
  1058
            printf("error while reading. Reason: [%s].\n",
icculus@1063
  1059
                   PHYSFS_getLastError());
icculus@1063
  1060
            return 1;
icculus@1063
  1061
        } /* if */
icculus@1063
  1062
icculus@1063
  1063
        PHYSFS_close(f);
icculus@1063
  1064
icculus@1063
  1065
        crc ^= -1;
icculus@1063
  1066
        printf("CRC32 for %s: 0x%08X\n", args, crc);
icculus@1063
  1067
    } /* else */
icculus@1063
  1068
icculus@1063
  1069
    return 1;
icculus@1063
  1070
} /* cmd_crc32 */
icculus@1063
  1071
icculus@1063
  1072
icculus@170
  1073
static int cmd_filelength(char *args)
icculus@170
  1074
{
icculus@654
  1075
    PHYSFS_File *f;
icculus@170
  1076
icculus@170
  1077
    if (*args == '\"')
icculus@170
  1078
    {
icculus@170
  1079
        args++;
icculus@170
  1080
        args[strlen(args) - 1] = '\0';
icculus@170
  1081
    } /* if */
icculus@170
  1082
icculus@170
  1083
    f = PHYSFS_openRead(args);
icculus@170
  1084
    if (f == NULL)
icculus@170
  1085
        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
icculus@170
  1086
    else
icculus@170
  1087
    {
icculus@170
  1088
        PHYSFS_sint64 len = PHYSFS_fileLength(f);
icculus@170
  1089
        if (len == -1)
icculus@170
  1090
            printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
icculus@170
  1091
        else
icculus@170
  1092
            printf(" (cast to int) %d bytes.\n", (int) len);
icculus@170
  1093
icculus@170
  1094
        PHYSFS_close(f);
icculus@170
  1095
    } /* else */
icculus@170
  1096
icculus@1016
  1097
    return 1;
icculus@170
  1098
} /* cmd_filelength */
icculus@170
  1099
icculus@170
  1100
#define WRITESTR "The cat sat on the mat.\n\n"
icculus@170
  1101
icculus@170
  1102
static int cmd_append(char *args)
icculus@170
  1103
{
icculus@654
  1104
    PHYSFS_File *f;
icculus@170
  1105
icculus@170
  1106
    if (*args == '\"')
icculus@170
  1107
    {
icculus@170
  1108
        args++;
icculus@170
  1109
        args[strlen(args) - 1] = '\0';
icculus@170
  1110
    } /* if */
icculus@170
  1111
icculus@170
  1112
    f = PHYSFS_openAppend(args);
icculus@170
  1113
    if (f == NULL)
icculus@170
  1114
        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
icculus@170
  1115
    else
icculus@170
  1116
    {
icculus@508
  1117
        size_t bw;
icculus@508
  1118
        PHYSFS_sint64 rc;
icculus@508
  1119
icculus@508
  1120
        if (do_buffer_size)
icculus@508
  1121
        {
icculus@508
  1122
            if (!PHYSFS_setBuffer(f, do_buffer_size))
icculus@508
  1123
            {
icculus@508
  1124
                printf("failed to set file buffer. Reason: [%s].\n",
icculus@508
  1125
                        PHYSFS_getLastError());
icculus@508
  1126
                PHYSFS_close(f);
icculus@1016
  1127
                return 1;
icculus@508
  1128
            } /* if */
icculus@508
  1129
        } /* if */
icculus@508
  1130
icculus@508
  1131
        bw = strlen(WRITESTR);
icculus@1102
  1132
        rc = PHYSFS_writeBytes(f, WRITESTR, bw);
icculus@170
  1133
        if (rc != bw)
icculus@170
  1134
        {
icculus@182
  1135
            printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
icculus@182
  1136
                   (int) rc, (int) bw, PHYSFS_getLastError());
icculus@170
  1137
        } /* if */
icculus@170
  1138
        else
icculus@170
  1139
        {
icculus@170
  1140
            printf("Successful.\n");
icculus@170
  1141
        } /* else */
icculus@170
  1142
icculus@170
  1143
        PHYSFS_close(f);
icculus@170
  1144
    } /* else */
icculus@170
  1145
icculus@1016
  1146
    return 1;
icculus@170
  1147
} /* cmd_append */
icculus@170
  1148
icculus@170
  1149
icculus@170
  1150
static int cmd_write(char *args)
icculus@170
  1151
{
icculus@654
  1152
    PHYSFS_File *f;
icculus@170
  1153
icculus@170
  1154
    if (*args == '\"')
icculus@170
  1155
    {
icculus@170
  1156
        args++;
icculus@170
  1157
        args[strlen(args) - 1] = '\0';
icculus@170
  1158
    } /* if */
icculus@170
  1159
icculus@170
  1160
    f = PHYSFS_openWrite(args);
icculus@170
  1161
    if (f == NULL)
icculus@170
  1162
        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
icculus@170
  1163
    else
icculus@170
  1164
    {
icculus@508
  1165
        size_t bw;
icculus@508
  1166
        PHYSFS_sint64 rc;
icculus@508
  1167
icculus@508
  1168
        if (do_buffer_size)
icculus@508
  1169
        {
icculus@508
  1170
            if (!PHYSFS_setBuffer(f, do_buffer_size))
icculus@508
  1171
            {
icculus@508
  1172
                printf("failed to set file buffer. Reason: [%s].\n",
icculus@508
  1173
                        PHYSFS_getLastError());
icculus@508
  1174
                PHYSFS_close(f);
icculus@1016
  1175
                return 1;
icculus@508
  1176
            } /* if */
icculus@508
  1177
        } /* if */
icculus@508
  1178
icculus@508
  1179
        bw = strlen(WRITESTR);
icculus@1102
  1180
        rc = PHYSFS_writeBytes(f, WRITESTR, bw);
icculus@170
  1181
        if (rc != bw)
icculus@170
  1182
        {
icculus@182
  1183
            printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
icculus@182
  1184
                   (int) rc, (int) bw, PHYSFS_getLastError());
icculus@170
  1185
        } /* if */
icculus@170
  1186
        else
icculus@170
  1187
        {
icculus@170
  1188
            printf("Successful.\n");
icculus@170
  1189
        } /* else */
icculus@170
  1190
icculus@170
  1191
        PHYSFS_close(f);
icculus@170
  1192
    } /* else */
icculus@170
  1193
icculus@1016
  1194
    return 1;
icculus@170
  1195
} /* cmd_write */
icculus@170
  1196
icculus@170
  1197
icculus@1054
  1198
static char* modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
icculus@244
  1199
{
icculus@1263
  1200
    if (modtime < 0)
icculus@1263
  1201
        strncpy(modstr, "Unknown\n", strsize);
icculus@1263
  1202
    else
icculus@1263
  1203
    {
icculus@1263
  1204
        time_t t = (time_t) modtime;
icculus@1263
  1205
        char *str = ctime(&t);
icculus@1263
  1206
        strncpy(modstr, str, strsize);
icculus@1263
  1207
    } /* else */
icculus@1263
  1208
icculus@244
  1209
    modstr[strsize-1] = '\0';
icculus@1054
  1210
    return modstr;
icculus@244
  1211
} /* modTimeToStr */
icculus@244
  1212
icculus@244
  1213
icculus@244
  1214
static int cmd_getlastmodtime(char *args)
icculus@244
  1215
{
icculus@1105
  1216
    PHYSFS_Stat statbuf;
icculus@1106
  1217
    if (!PHYSFS_stat(args, &statbuf))
icculus@244
  1218
        printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
icculus@244
  1219
    else
icculus@244
  1220
    {
icculus@244
  1221
        char modstr[64];
icculus@1105
  1222
        modTimeToStr(statbuf.modtime, modstr, sizeof (modstr));
icculus@1105
  1223
        printf("Last modified: %s (%ld).\n", modstr, (long) statbuf.modtime);
icculus@244
  1224
    } /* else */
icculus@244
  1225
icculus@1016
  1226
    return 1;
icculus@244
  1227
} /* cmd_getLastModTime */
icculus@244
  1228
icculus@1054
  1229
static int cmd_stat(char *args)
icculus@1054
  1230
{
icculus@1054
  1231
    PHYSFS_Stat stat;
icculus@1054
  1232
    char timestring[65];
icculus@1054
  1233
icculus@1054
  1234
    if (*args == '\"')
icculus@1054
  1235
    {
icculus@1054
  1236
        args++;
icculus@1054
  1237
        args[strlen(args) - 1] = '\0';
icculus@1054
  1238
    } /* if */
icculus@1054
  1239
icculus@1106
  1240
    if(!PHYSFS_stat(args, &stat))
icculus@1054
  1241
    {
icculus@1054
  1242
        printf("failed to stat. Reason [%s].\n", PHYSFS_getLastError());
icculus@1054
  1243
        return 1;
icculus@1054
  1244
    } /* if */
icculus@1054
  1245
icculus@1054
  1246
    printf("Filename: %s\n", args);
icculus@1054
  1247
    printf("Size %d\n",(int) stat.filesize);
icculus@1054
  1248
icculus@1054
  1249
    if(stat.filetype == PHYSFS_FILETYPE_REGULAR)
icculus@1054
  1250
        printf("Type: File\n");
icculus@1054
  1251
    else if(stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
icculus@1054
  1252
        printf("Type: Directory\n");
icculus@1054
  1253
    else if(stat.filetype == PHYSFS_FILETYPE_SYMLINK)
icculus@1054
  1254
        printf("Type: Symlink\n");
icculus@1054
  1255
    else
icculus@1054
  1256
        printf("Type: Unknown\n");
icculus@1054
  1257
icculus@1054
  1258
    printf("Created at: %s", modTimeToStr(stat.createtime, timestring, 64));
icculus@1054
  1259
    printf("Last modified at: %s", modTimeToStr(stat.modtime, timestring, 64));
icculus@1054
  1260
    printf("Last accessed at: %s", modTimeToStr(stat.accesstime, timestring, 64));
icculus@1054
  1261
    printf("Readonly: %s\n", stat.readonly ? "true" : "false");
icculus@1054
  1262
icculus@1054
  1263
    return 1;
icculus@1054
  1264
} /* cmd_filelength */
icculus@1054
  1265
icculus@1054
  1266
icculus@244
  1267
icculus@40
  1268
/* must have spaces trimmed prior to this call. */
icculus@40
  1269
static int count_args(const char *str)
icculus@40
  1270
{
icculus@40
  1271
    int retval = 0;
icculus@165
  1272
    int in_quotes = 0;
icculus@40
  1273
icculus@40
  1274
    if (str != NULL)
icculus@40
  1275
    {
icculus@40
  1276
        for (; *str != '\0'; str++)
icculus@40
  1277
        {
icculus@165
  1278
            if (*str == '\"')
icculus@165
  1279
                in_quotes = !in_quotes;
icculus@165
  1280
            else if ((*str == ' ') && (!in_quotes))
icculus@40
  1281
                retval++;
icculus@40
  1282
        } /* for */
icculus@40
  1283
        retval++;
icculus@40
  1284
    } /* if */
icculus@40
  1285
icculus@1016
  1286
    return retval;
icculus@40
  1287
} /* count_args */
icculus@40
  1288
icculus@40
  1289
icculus@40
  1290
static int cmd_help(char *args);
icculus@40
  1291
icculus@38
  1292
typedef struct
icculus@38
  1293
{
icculus@38
  1294
    const char *cmd;
icculus@40
  1295
    int (*func)(char *args);
icculus@40
  1296
    int argcount;
icculus@40
  1297
    const char *usage;
icculus@38
  1298
} command_info;
icculus@38
  1299
icculus@40
  1300
static const command_info commands[] =
icculus@38
  1301
{
icculus@40
  1302
    { "quit",           cmd_quit,           0, NULL                         },
icculus@40
  1303
    { "q",              cmd_quit,           0, NULL                         },
icculus@40
  1304
    { "help",           cmd_help,           0, NULL                         },
icculus@40
  1305
    { "init",           cmd_init,           1, "<argv0>"                    },
icculus@40
  1306
    { "deinit",         cmd_deinit,         0, NULL                         },
icculus@40
  1307
    { "addarchive",     cmd_addarchive,     2, "<archiveLocation> <append>" },
icculus@681
  1308
    { "mount",          cmd_mount,          3, "<archiveLocation> <mntpoint> <append>" },
icculus@1120
  1309
    { "mountmem",       cmd_mount_mem,      3, "<archiveLocation> <mntpoint> <append>" },
icculus@1123
  1310
    { "mounthandle",    cmd_mount_handle,   3, "<archiveLocation> <mntpoint> <append>" },
icculus@40
  1311
    { "removearchive",  cmd_removearchive,  1, "<archiveLocation>"          },
icculus@1110
  1312
    { "unmount",        cmd_removearchive,  1, "<archiveLocation>"          },
icculus@40
  1313
    { "enumerate",      cmd_enumerate,      1, "<dirToEnumerate>"           },
icculus@374
  1314
    { "ls",             cmd_enumerate,      1, "<dirToEnumerate>"           },
icculus@40
  1315
    { "getlasterror",   cmd_getlasterror,   0, NULL                         },
icculus@40
  1316
    { "getdirsep",      cmd_getdirsep,      0, NULL                         },
icculus@40
  1317
    { "getcdromdirs",   cmd_getcdromdirs,   0, NULL                         },
icculus@40
  1318
    { "getsearchpath",  cmd_getsearchpath,  0, NULL                         },
icculus@40
  1319
    { "getbasedir",     cmd_getbasedir,     0, NULL                         },
icculus@40
  1320
    { "getuserdir",     cmd_getuserdir,     0, NULL                         },
icculus@1242
  1321
    { "getprefdir",     cmd_getprefdir,     2, "<org> <app>"                },
icculus@40
  1322
    { "getwritedir",    cmd_getwritedir,    0, NULL                         },
icculus@40
  1323
    { "setwritedir",    cmd_setwritedir,    1, "<newWriteDir>"              },
icculus@40
  1324
    { "permitsymlinks", cmd_permitsyms,     1, "<1or0>"                     },
icculus@495
  1325
    { "setsaneconfig",  cmd_setsaneconfig,  5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
icculus@42
  1326
    { "mkdir",          cmd_mkdir,          1, "<dirToMk>"                  },
icculus@42
  1327
    { "delete",         cmd_delete,         1, "<dirToDelete>"              },
icculus@42
  1328
    { "getrealdir",     cmd_getrealdir,     1, "<fileToFind>"               },
icculus@42
  1329
    { "exists",         cmd_exists,         1, "<fileToCheck>"              },
icculus@42
  1330
    { "isdir",          cmd_isdir,          1, "<fileToCheck>"              },
icculus@42
  1331
    { "issymlink",      cmd_issymlink,      1, "<fileToCheck>"              },
icculus@108
  1332
    { "cat",            cmd_cat,            1, "<fileToCat>"                },
icculus@1464
  1333
    { "cat2",           cmd_cat2,           2, "<fileToCat1> <fileToCat2>"  },
icculus@170
  1334
    { "filelength",     cmd_filelength,     1, "<fileToCheck>"              },
icculus@1054
  1335
    { "stat",           cmd_stat,           1, "<fileToStat>"               },
icculus@170
  1336
    { "append",         cmd_append,         1, "<fileToAppend>"             },
icculus@170
  1337
    { "write",          cmd_write,          1, "<fileToCreateOrTrash>"      },
icculus@244
  1338
    { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>"            },
icculus@508
  1339
    { "setbuffer",      cmd_setbuffer,      1, "<bufferSize>"               },
icculus@508
  1340
    { "stressbuffer",   cmd_stressbuffer,   1, "<bufferSize>"               },
icculus@1063
  1341
    { "crc32",          cmd_crc32,          1, "<fileToHash>"               },
icculus@1282
  1342
    { "getmountpoint",  cmd_getmountpoint,  1, "<dir>"                      },
icculus@40
  1343
    { NULL,             NULL,              -1, NULL                         }
icculus@38
  1344
};
icculus@38
  1345
icculus@244
  1346
icculus@40
  1347
static void output_usage(const char *intro, const command_info *cmdinfo)
icculus@40
  1348
{
icculus@40
  1349
    if (cmdinfo->argcount == 0)
icculus@40
  1350
        printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
icculus@40
  1351
    else
icculus@40
  1352
        printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
icculus@40
  1353
} /* output_usage */
icculus@40
  1354
icculus@40
  1355
icculus@40
  1356
static int cmd_help(char *args)
icculus@40
  1357
{
icculus@40
  1358
    const command_info *i;
icculus@40
  1359
icculus@40
  1360
    printf("Commands:\n");
icculus@40
  1361
    for (i = commands; i->cmd != NULL; i++)
icculus@40
  1362
        output_usage("  -", i);
icculus@40
  1363
icculus@1016
  1364
    return 1;
icculus@40
  1365
} /* output_cmd_help */
icculus@40
  1366
icculus@40
  1367
icculus@40
  1368
static void trim_command(const char *orig, char *copy)
icculus@40
  1369
{
icculus@40
  1370
    const char *i;
icculus@40
  1371
    char *writeptr = copy;
icculus@40
  1372
    int spacecount = 0;
icculus@40
  1373
    int have_first = 0;
icculus@40
  1374
icculus@40
  1375
    for (i = orig; *i != '\0'; i++)
icculus@40
  1376
    {
icculus@40
  1377
        if (*i == ' ')
icculus@40
  1378
        {
icculus@40
  1379
            if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
icculus@40
  1380
            {
icculus@40
  1381
                if ((have_first) && (!spacecount))
icculus@40
  1382
                {
icculus@40
  1383
                    spacecount++;
icculus@40
  1384
                    *writeptr = ' ';
icculus@40
  1385
                    writeptr++;
icculus@40
  1386
                } /* if */
icculus@40
  1387
            } /* if */
icculus@40
  1388
        } /* if */
icculus@40
  1389
        else
icculus@40
  1390
        {
icculus@40
  1391
            have_first = 1;
icculus@40
  1392
            spacecount = 0;
icculus@40
  1393
            *writeptr = *i;
icculus@40
  1394
            writeptr++;
icculus@40
  1395
        } /* else */
icculus@40
  1396
    } /* for */
icculus@40
  1397
icculus@40
  1398
    *writeptr = '\0';
icculus@40
  1399
icculus@40
  1400
    /*
icculus@40
  1401
    printf("\n command is [%s].\n", copy);
icculus@40
  1402
    */
icculus@40
  1403
} /* trim_command */
icculus@40
  1404
icculus@38
  1405
icculus@38
  1406
static int process_command(char *complete_cmd)
icculus@38
  1407
{
icculus@40
  1408
    const command_info *i;
icculus@500
  1409
    char *cmd_copy;
icculus@40
  1410
    char *args;
icculus@38
  1411
    int rc = 1;
icculus@38
  1412
icculus@500
  1413
    if (complete_cmd == NULL)  /* can happen if user hits CTRL-D, etc. */
icculus@500
  1414
    {
icculus@500
  1415
        printf("\n");
icculus@1016
  1416
        return 0;
icculus@500
  1417
    } /* if */
icculus@500
  1418
icculus@689
  1419
    cmd_copy = (char *) malloc(strlen(complete_cmd) + 1);
icculus@40
  1420
    if (cmd_copy == NULL)
icculus@38
  1421
    {
icculus@40
  1422
        printf("\n\n\nOUT OF MEMORY!\n\n\n");
icculus@1016
  1423
        return 0;
icculus@38
  1424
    } /* if */
icculus@40
  1425
icculus@40
  1426
    trim_command(complete_cmd, cmd_copy);
icculus@40
  1427
    args = strchr(cmd_copy, ' ');
icculus@40
  1428
    if (args != NULL)
icculus@38
  1429
    {
icculus@40
  1430
        *args = '\0';
icculus@40
  1431
        args++;
icculus@38
  1432
    } /* else */
icculus@38
  1433
icculus@40
  1434
    if (cmd_copy[0] != '\0')
icculus@38
  1435
    {
icculus@40
  1436
        for (i = commands; i->cmd != NULL; i++)
icculus@38
  1437
        {
icculus@40
  1438
            if (strcmp(i->cmd, cmd_copy) == 0)
icculus@40
  1439
            {
icculus@40
  1440
                if ((i->argcount >= 0) && (count_args(args) != i->argcount))
icculus@40
  1441
                    output_usage("usage:", i);
icculus@40
  1442
                else
icculus@40
  1443
                    rc = i->func(args);
icculus@40
  1444
                break;
icculus@40
  1445
            } /* if */
icculus@40
  1446
        } /* for */
icculus@40
  1447
icculus@40
  1448
        if (i->cmd == NULL)
icculus@40
  1449
            printf("Unknown command. Enter \"help\" for instructions.\n");
icculus@40
  1450
icculus@214
  1451
#if (defined PHYSFS_HAVE_READLINE)
icculus@40
  1452
        add_history(complete_cmd);
icculus@40
  1453
        if (history_file)
icculus@40
  1454
        {
icculus@40
  1455
            fprintf(history_file, "%s\n", complete_cmd);
icculus@40
  1456
            fflush(history_file);
icculus@38
  1457
        } /* if */
icculus@72
  1458
#endif
icculus@72
  1459
icculus@40
  1460
    } /* if */
icculus@38
  1461
icculus@40
  1462
    free(cmd_copy);
icculus@1016
  1463
    return rc;
icculus@38
  1464
} /* process_command */
icculus@38
  1465
icculus@38
  1466
icculus@40
  1467
static void open_history_file(void)
icculus@40
  1468
{
icculus@214
  1469
#if (defined PHYSFS_HAVE_READLINE)
icculus@42
  1470
#if 0
icculus@40
  1471
    const char *envr = getenv("TESTPHYSFS_HISTORY");
icculus@40
  1472
    if (!envr)
icculus@40
  1473
        return;
icculus@42
  1474
#else
icculus@42
  1475
    char envr[256];
icculus@42
  1476
    strcpy(envr, PHYSFS_getUserDir());
icculus@42
  1477
    strcat(envr, ".testphys_history");
icculus@42
  1478
#endif
icculus@40
  1479
icculus@40
  1480
    if (access(envr, F_OK) == 0)
icculus@40
  1481
    {
icculus@40
  1482
        char buf[512];
icculus@40
  1483
        FILE *f = fopen(envr, "r");
icculus@40
  1484
        if (!f)
icculus@40
  1485
        {
icculus@40
  1486
            printf("\n\n"
icculus@40
  1487
                   "Could not open history file [%s] for reading!\n"
icculus@40
  1488
                   "  Will not have past history available.\n\n",
icculus@40
  1489
                    envr);
icculus@40
  1490
            return;
icculus@40
  1491
        } /* if */
icculus@40
  1492
icculus@40
  1493
        do
icculus@40
  1494
        {
icculus@1027
  1495
            if (fgets(buf, sizeof (buf), f) == NULL)
icculus@1027
  1496
                break;
icculus@1027
  1497
icculus@40
  1498
            if (buf[strlen(buf) - 1] == '\n')
icculus@40
  1499
                buf[strlen(buf) - 1] = '\0';
icculus@40
  1500
            add_history(buf);
icculus@40
  1501
        } while (!feof(f));
icculus@40
  1502
icculus@40
  1503
        fclose(f);
icculus@40
  1504
    } /* if */
icculus@40
  1505
icculus@40
  1506
    history_file = fopen(envr, "ab");
icculus@40
  1507
    if (!history_file)
icculus@40
  1508
    {
icculus@40
  1509
        printf("\n\n"
icculus@40
  1510
               "Could not open history file [%s] for appending!\n"
icculus@40
  1511
               "  Will not be able to record this session's history.\n\n",
icculus@40
  1512
                envr);
icculus@40
  1513
    } /* if */
icculus@72
  1514
#endif
icculus@40
  1515
} /* open_history_file */
icculus@40
  1516
icculus@40
  1517
icculus@35
  1518
int main(int argc, char **argv)
icculus@35
  1519
{
icculus@38
  1520
    char *buf = NULL;
icculus@38
  1521
    int rc = 0;
icculus@38
  1522
icculus@164
  1523
#if (defined __MWERKS__)
icculus@164
  1524
    extern tSIOUXSettings SIOUXSettings;
icculus@164
  1525
    SIOUXSettings.asktosaveonclose = 0;
icculus@164
  1526
    SIOUXSettings.autocloseonquit = 1;
icculus@164
  1527
    SIOUXSettings.rows = 40;
icculus@164
  1528
    SIOUXSettings.columns = 120;
icculus@164
  1529
#endif
icculus@164
  1530
icculus@38
  1531
    printf("\n");
icculus@38
  1532
icculus@35
  1533
    if (!PHYSFS_init(argv[0]))
icculus@35
  1534
    {
icculus@40
  1535
        printf("PHYSFS_init() failed!\n  reason: %s.\n", PHYSFS_getLastError());
icculus@1016
  1536
        return 1;
icculus@35
  1537
    } /* if */
icculus@35
  1538
icculus@35
  1539
    output_versions();
icculus@35
  1540
    output_archivers();
icculus@35
  1541
icculus@40
  1542
    open_history_file();
icculus@40
  1543
icculus@38
  1544
    printf("Enter commands. Enter \"help\" for instructions.\n");
icculus@1054
  1545
    fflush(stdout);
icculus@38
  1546
icculus@38
  1547
    do
icculus@38
  1548
    {
icculus@214
  1549
#if (defined PHYSFS_HAVE_READLINE)
icculus@38
  1550
        buf = readline("> ");
icculus@72
  1551
#else
icculus@74
  1552
        int i;
icculus@689
  1553
        buf = (char *) malloc(512);
icculus@72
  1554
        memset(buf, '\0', 512);
icculus@74
  1555
        printf("> ");
icculus@1054
  1556
        fflush(stdout);
icculus@74
  1557
        for (i = 0; i < 511; i++)
icculus@74
  1558
        {
icculus@74
  1559
            int ch = fgetc(stdin);
icculus@74
  1560
            if (ch == EOF)
icculus@74
  1561
            {
icculus@74
  1562
                strcpy(buf, "quit");
icculus@74
  1563
                break;
icculus@74
  1564
            } /* if */
icculus@74
  1565
            else if ((ch == '\n') || (ch == '\r'))
icculus@74
  1566
            {
icculus@74
  1567
                buf[i] = '\0';
icculus@74
  1568
                break;
icculus@74
  1569
            } /* else if */
icculus@74
  1570
            else if (ch == '\b')
icculus@74
  1571
            {
icculus@74
  1572
                if (i > 0)
icculus@74
  1573
                    i--;
icculus@74
  1574
            } /* else if */
icculus@74
  1575
            else
icculus@74
  1576
            {
icculus@74
  1577
                buf[i] = (char) ch;
icculus@74
  1578
            } /* else */
icculus@74
  1579
        } /* for */
icculus@72
  1580
#endif
icculus@74
  1581
icculus@38
  1582
        rc = process_command(buf);
icculus@1054
  1583
        fflush(stdout);
icculus@500
  1584
        if (buf != NULL)
icculus@500
  1585
            free(buf);
icculus@38
  1586
    } while (rc);
icculus@38
  1587
icculus@40
  1588
    if (!PHYSFS_deinit())
icculus@40
  1589
        printf("PHYSFS_deinit() failed!\n  reason: %s.\n", PHYSFS_getLastError());
icculus@40
  1590
icculus@40
  1591
    if (history_file)
icculus@40
  1592
        fclose(history_file);
icculus@40
  1593
icculus@40
  1594
/*
icculus@40
  1595
    printf("\n\ntest_physfs written by ryan c. gordon.\n");
icculus@40
  1596
    printf(" it makes you shoot teh railgun bettar.\n");
icculus@40
  1597
*/
icculus@40
  1598
icculus@1016
  1599
    return 0;
icculus@35
  1600
} /* main */
icculus@35
  1601
icculus@35
  1602
/* end of test_physfs.c ... */
icculus@35
  1603