Skip to content

Commit

Permalink
More hacks to handle quoted arguments. Write, append, and filelength …
Browse files Browse the repository at this point in the history
…tests. Upped version to 0.1.1.
  • Loading branch information
icculus committed Apr 5, 2002
1 parent 148a933 commit f4d0842
Showing 1 changed file with 197 additions and 10 deletions.
207 changes: 197 additions & 10 deletions test/test_physfs.c
Expand Up @@ -29,7 +29,7 @@

#define TEST_VERSION_MAJOR 0
#define TEST_VERSION_MINOR 1
#define TEST_VERSION_PATCH 0
#define TEST_VERSION_PATCH 1

static FILE *history_file = NULL;

Expand Down Expand Up @@ -80,6 +80,12 @@ static int cmd_quit(char *args)

static int cmd_init(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

if (PHYSFS_init(args))
printf("Successful.\n");
else
Expand Down Expand Up @@ -110,7 +116,7 @@ static int cmd_addarchive(char *args)
{
args++;
*(ptr - 1) = '\0';
}
} /* if */

/*printf("[%s], [%d]\n", args, appending);*/

Expand All @@ -125,6 +131,12 @@ static int cmd_addarchive(char *args)

static int cmd_removearchive(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

if (PHYSFS_removeFromSearchPath(args))
printf("Successful.\n");
else
Expand All @@ -136,7 +148,15 @@ static int cmd_removearchive(char *args)

static int cmd_enumerate(char *args)
{
char **rc = PHYSFS_enumerateFiles(args);
char **rc;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

rc = PHYSFS_enumerateFiles(args);

if (rc == NULL)
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
Expand Down Expand Up @@ -174,7 +194,7 @@ static int cmd_getcdromdirs(char *args)
char **rc = PHYSFS_getCdRomDirs();

if (rc == NULL)
printf("Failure. reason: %s.\n", PHYSFS_getLastError());
printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
else
{
int dir_count;
Expand Down Expand Up @@ -234,6 +254,12 @@ static int cmd_getwritedir(char *args)

static int cmd_setwritedir(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

if (PHYSFS_setWriteDir(args))
printf("Successful.\n");
else
Expand All @@ -245,7 +271,15 @@ static int cmd_setwritedir(char *args)

static int cmd_permitsyms(char *args)
{
int num = atoi(args);
int num;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

num = atoi(args);
PHYSFS_permitSymbolicLinks(num);
printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
return(1);
Expand Down Expand Up @@ -282,6 +316,12 @@ static int cmd_setsaneconfig(char *args)

static int cmd_mkdir(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

if (PHYSFS_mkdir(args))
printf("Successful.\n");
else
Expand All @@ -293,6 +333,12 @@ static int cmd_mkdir(char *args)

static int cmd_delete(char *args)
{
if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

if (PHYSFS_delete(args))
printf("Successful.\n");
else
Expand All @@ -304,7 +350,15 @@ static int cmd_delete(char *args)

static int cmd_getrealdir(char *args)
{
const char *rc = PHYSFS_getRealDir(args);
const char *rc;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

rc = PHYSFS_getRealDir(args);
if (rc)
printf("Found at [%s].\n", rc);
else
Expand All @@ -316,31 +370,63 @@ static int cmd_getrealdir(char *args)

static int cmd_exists(char *args)
{
int rc = PHYSFS_exists(args);
int rc;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

rc = PHYSFS_exists(args);
printf("File %sexists.\n", rc ? "" : "does not ");
return(1);
} /* cmd_exists */


static int cmd_isdir(char *args)
{
int rc = PHYSFS_isDirectory(args);
int rc;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

rc = PHYSFS_isDirectory(args);
printf("File %s a directory.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_isdir */


static int cmd_issymlink(char *args)
{
int rc = PHYSFS_isSymbolicLink(args);
int rc;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

rc = PHYSFS_isSymbolicLink(args);
printf("File %s a symlink.\n", rc ? "is" : "is NOT");
return(1);
} /* cmd_issymlink */


static int cmd_cat(char *args)
{
PHYSFS_file *f = PHYSFS_openRead(args);
PHYSFS_file *f;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

f = PHYSFS_openRead(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
Expand Down Expand Up @@ -373,6 +459,104 @@ static int cmd_cat(char *args)
} /* cmd_cat */


static int cmd_filelength(char *args)
{
PHYSFS_file *f;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

f = PHYSFS_openRead(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
PHYSFS_sint64 len = PHYSFS_fileLength(f);
if (len == -1)
printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
else
printf(" (cast to int) %d bytes.\n", (int) len);

PHYSFS_close(f);
} /* else */

return(1);
} /* cmd_filelength */


#define WRITESTR "The cat sat on the mat.\n\n"

static int cmd_append(char *args)
{
PHYSFS_file *f;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

f = PHYSFS_openAppend(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
size_t bw = strlen(WRITESTR);
PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
if (rc != bw)
{
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", rc, bw,
PHYSFS_getLastError());
} /* if */
else
{
printf("Successful.\n");
} /* else */

PHYSFS_close(f);
} /* else */

return(1);
} /* cmd_append */


static int cmd_write(char *args)
{
PHYSFS_file *f;

if (*args == '\"')
{
args++;
args[strlen(args) - 1] = '\0';
} /* if */

f = PHYSFS_openWrite(args);
if (f == NULL)
printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
else
{
size_t bw = strlen(WRITESTR);
PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
if (rc != bw)
{
printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", rc, bw,
PHYSFS_getLastError());
} /* if */
else
{
printf("Successful.\n");
} /* else */

PHYSFS_close(f);
} /* else */

return(1);
} /* cmd_write */


/* must have spaces trimmed prior to this call. */
static int count_args(const char *str)
{
Expand Down Expand Up @@ -432,6 +616,9 @@ static const command_info commands[] =
{ "isdir", cmd_isdir, 1, "<fileToCheck>" },
{ "issymlink", cmd_issymlink, 1, "<fileToCheck>" },
{ "cat", cmd_cat, 1, "<fileToCat>" },
{ "filelength", cmd_filelength, 1, "<fileToCheck>" },
{ "append", cmd_append, 1, "<fileToAppend>" },
{ "write", cmd_write, 1, "<fileToCreateOrTrash>" },
{ NULL, NULL, -1, NULL }
};

Expand Down

0 comments on commit f4d0842

Please sign in to comment.