| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172 |
- /**
- * Test program for PhysicsFS. May only work on Unix.
- *
- * Please see the file LICENSE in the source's root directory.
- *
- * This file written by Ryan C. Gordon.
- */
- #if HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #if (defined __MWERKS__)
- #include <SIOUX.h>
- #endif
- #if (defined PHYSFS_HAVE_READLINE)
- #include <unistd.h>
- #include <readline/readline.h>
- #include <readline/history.h>
- #endif
- #include <time.h>
- #include "physfs.h"
- #define TEST_VERSION_MAJOR 1
- #define TEST_VERSION_MINOR 0
- #define TEST_VERSION_PATCH 2
- static FILE *history_file = NULL;
- static PHYSFS_uint32 do_buffer_size = 0;
- static void output_versions(void)
- {
- PHYSFS_Version compiled;
- PHYSFS_Version linked;
- PHYSFS_VERSION(&compiled);
- PHYSFS_getLinkedVersion(&linked);
- printf("test_physfs version %d.%d.%d.\n"
- " Compiled against PhysicsFS version %d.%d.%d,\n"
- " and linked against %d.%d.%d.\n\n",
- TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH,
- (int) compiled.major, (int) compiled.minor, (int) compiled.patch,
- (int) linked.major, (int) linked.minor, (int) linked.patch);
- } /* output_versions */
- static void output_archivers(void)
- {
- const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
- const PHYSFS_ArchiveInfo **i;
- printf("Supported archive types:\n");
- if (*rc == NULL)
- printf(" * Apparently, NONE!\n");
- else
- {
- for (i = rc; *i != NULL; i++)
- {
- printf(" * %s: %s\n Written by %s.\n %s\n",
- (*i)->extension, (*i)->description,
- (*i)->author, (*i)->url);
- } /* for */
- } /* else */
- printf("\n");
- } /* output_archivers */
- static int cmd_quit(char *args)
- {
- return(0);
- } /* cmd_quit */
- static int cmd_init(char *args)
- {
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- if (PHYSFS_init(args))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_init */
- static int cmd_deinit(char *args)
- {
- if (PHYSFS_deinit())
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_deinit */
- static int cmd_addarchive(char *args)
- {
- char *ptr = strrchr(args, ' ');
- int appending = atoi(ptr + 1);
- *ptr = '\0';
- if (*args == '\"')
- {
- args++;
- *(ptr - 1) = '\0';
- } /* if */
- /*printf("[%s], [%d]\n", args, appending);*/
- if (PHYSFS_addToSearchPath(args, appending))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_addarchive */
- static int cmd_removearchive(char *args)
- {
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- if (PHYSFS_removeFromSearchPath(args))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_removearchive */
- static int cmd_enumerate(char *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());
- else
- {
- int file_count;
- char **i;
- for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
- printf("%s\n", *i);
- printf("\n total (%d) files.\n", file_count);
- PHYSFS_freeList(rc);
- } /* else */
- return(1);
- } /* cmd_enumerate */
- static int cmd_getdirsep(char *args)
- {
- printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
- return(1);
- } /* cmd_getdirsep */
- static int cmd_getlasterror(char *args)
- {
- printf("last error is [%s].\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_getlasterror */
- static int cmd_getcdromdirs(char *args)
- {
- char **rc = PHYSFS_getCdRomDirs();
- if (rc == NULL)
- printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
- else
- {
- int dir_count;
- char **i;
- for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
- printf("%s\n", *i);
- printf("\n total (%d) drives.\n", dir_count);
- PHYSFS_freeList(rc);
- } /* else */
- return(1);
- } /* cmd_getcdromdirs */
- static int cmd_getsearchpath(char *args)
- {
- char **rc = PHYSFS_getSearchPath();
- if (rc == NULL)
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- else
- {
- int dir_count;
- char **i;
- for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
- printf("%s\n", *i);
- printf("\n total (%d) directories.\n", dir_count);
- PHYSFS_freeList(rc);
- } /* else */
- return(1);
- } /* cmd_getcdromdirs */
- static int cmd_getbasedir(char *args)
- {
- printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
- return(1);
- } /* cmd_getbasedir */
- static int cmd_getuserdir(char *args)
- {
- printf("User dir is [%s].\n", PHYSFS_getUserDir());
- return(1);
- } /* cmd_getuserdir */
- static int cmd_getwritedir(char *args)
- {
- printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
- return(1);
- } /* cmd_getwritedir */
- static int cmd_setwritedir(char *args)
- {
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- if (PHYSFS_setWriteDir(args))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_setwritedir */
- static int cmd_permitsyms(char *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);
- } /* cmd_permitsyms */
- static int cmd_setbuffer(char *args)
- {
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- do_buffer_size = (unsigned int) atoi(args);
- if (do_buffer_size)
- {
- printf("Further tests will set a (%lu) size buffer.\n",
- (unsigned long) do_buffer_size);
- } /* if */
- else
- {
- printf("Further tests will NOT use a buffer.\n");
- } /* else */
- return(1);
- } /* cmd_setbuffer */
- static int cmd_stressbuffer(char *args)
- {
- int num;
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- num = atoi(args);
- if (num < 0)
- printf("buffer must be greater than or equal to zero.\n");
- else
- {
- PHYSFS_file *f;
- int rndnum;
- printf("Stress testing with (%d) byte buffer...\n", num);
- f = PHYSFS_openWrite("test.txt");
- if (f == NULL)
- printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError());
- else
- {
- int i, j;
- char buf[37];
- char buf2[37];
- if (!PHYSFS_setBuffer(f, num))
- {
- printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- PHYSFS_delete("test.txt");
- return(1);
- } /* if */
- strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789");
- srand((unsigned int) time(NULL));
- for (i = 0; i < 10; i++)
- {
- for (j = 0; j < 10000; j++)
- {
- PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
- PHYSFS_uint32 left = 36 - right;
- if (PHYSFS_write(f, buf, left, 1) != 1)
- {
- printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
- if (rndnum == 42)
- {
- if (!PHYSFS_flush(f))
- {
- printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- if (PHYSFS_write(f, buf + left, 1, right) != right)
- {
- printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
- if (rndnum == 42)
- {
- if (!PHYSFS_flush(f))
- {
- printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- } /* for */
- if (!PHYSFS_flush(f))
- {
- printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* for */
- if (!PHYSFS_close(f))
- {
- printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
- return(1); /* oh well. */
- } /* if */
- printf(" ... test file written ...\n");
- f = PHYSFS_openRead("test.txt");
- if (f == NULL)
- {
- printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* if */
- if (!PHYSFS_setBuffer(f, num))
- {
- printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- for (i = 0; i < 10; i++)
- {
- for (j = 0; j < 10000; j++)
- {
- PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0));
- PHYSFS_uint32 left = 36 - right;
- if (PHYSFS_read(f, buf2, left, 1) != 1)
- {
- printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
- if (rndnum == 42)
- {
- if (!PHYSFS_flush(f))
- {
- printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- if (PHYSFS_read(f, buf2 + left, 1, right) != right)
- {
- printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0));
- if (rndnum == 42)
- {
- if (!PHYSFS_flush(f))
- {
- printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- if (memcmp(buf, buf2, 36) != 0)
- {
- printf("readback is mismatched on iterations (%d, %d).\n", i, j);
- printf("wanted: [");
- for (i = 0; i < 36; i++)
- printf("%c", buf[i]);
- printf("]\n");
- printf(" got: [");
- for (i = 0; i < 36; i++)
- printf("%c", buf2[i]);
- printf("]\n");
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* for */
- if (!PHYSFS_flush(f))
- {
- printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* for */
- printf(" ... test file read ...\n");
- if (!PHYSFS_eof(f))
- printf("PHYSFS_eof() returned true! That's wrong.\n");
- if (!PHYSFS_close(f))
- {
- printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError());
- return(1); /* oh well. */
- } /* if */
- PHYSFS_delete("test.txt");
- printf("stress test completed successfully.\n");
- } /* else */
- } /* else */
- return(1);
- } /* cmd_stressbuffer */
- static int cmd_setsaneconfig(char *args)
- {
- char *org;
- char *appName;
- char *arcExt;
- int inclCD;
- int arcsFirst;
- char *ptr = args;
- /* ugly. */
- org = ptr;
- ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; appName = ptr;
- ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
- ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
- arcsFirst = atoi(ptr);
- if (strcmp(arcExt, "!") == 0)
- arcExt = NULL;
- if (PHYSFS_setSaneConfig(org, appName, arcExt, inclCD, arcsFirst))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_setsaneconfig */
- static int cmd_mkdir(char *args)
- {
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- if (PHYSFS_mkdir(args))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_mkdir */
- static int cmd_delete(char *args)
- {
- if (*args == '\"')
- {
- args++;
- args[strlen(args) - 1] = '\0';
- } /* if */
- if (PHYSFS_delete(args))
- printf("Successful.\n");
- else
- printf("Failure. reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* cmd_delete */
- static int cmd_getrealdir(char *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
- printf("Not found.\n");
- return(1);
- } /* cmd_getrealdir */
- static int cmd_exists(char *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;
- 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;
- 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;
- 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
- {
- if (do_buffer_size)
- {
- if (!PHYSFS_setBuffer(f, do_buffer_size))
- {
- printf("failed to set file buffer. Reason: [%s].\n",
- PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- while (1)
- {
- char buffer[128];
- PHYSFS_sint64 rc;
- PHYSFS_sint64 i;
- rc = PHYSFS_read(f, buffer, 1, sizeof (buffer));
- for (i = 0; i < rc; i++)
- fputc((int) buffer[i], stdout);
- if (rc < sizeof (buffer))
- {
- printf("\n\n");
- if (!PHYSFS_eof(f))
- {
- printf("\n (Error condition in reading. Reason: [%s])\n\n",
- PHYSFS_getLastError());
- } /* if */
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* while */
- } /* else */
- return(1);
- } /* 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;
- PHYSFS_sint64 rc;
- if (do_buffer_size)
- {
- if (!PHYSFS_setBuffer(f, do_buffer_size))
- {
- printf("failed to set file buffer. Reason: [%s].\n",
- PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- bw = strlen(WRITESTR);
- rc = PHYSFS_write(f, WRITESTR, 1, bw);
- if (rc != bw)
- {
- printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
- (int) rc, (int) 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;
- PHYSFS_sint64 rc;
- if (do_buffer_size)
- {
- if (!PHYSFS_setBuffer(f, do_buffer_size))
- {
- printf("failed to set file buffer. Reason: [%s].\n",
- PHYSFS_getLastError());
- PHYSFS_close(f);
- return(1);
- } /* if */
- } /* if */
- bw = strlen(WRITESTR);
- rc = PHYSFS_write(f, WRITESTR, 1, bw);
- if (rc != bw)
- {
- printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",
- (int) rc, (int) bw, PHYSFS_getLastError());
- } /* if */
- else
- {
- printf("Successful.\n");
- } /* else */
- PHYSFS_close(f);
- } /* else */
- return(1);
- } /* cmd_write */
- static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize)
- {
- time_t t = (time_t) modtime;
- char *str = ctime(&t);
- strncpy(modstr, str, strsize);
- modstr[strsize-1] = '\0';
- } /* modTimeToStr */
- static int cmd_getlastmodtime(char *args)
- {
- PHYSFS_sint64 rc = PHYSFS_getLastModTime(args);
- if (rc == -1)
- printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());
- else
- {
- char modstr[64];
- modTimeToStr(rc, modstr, sizeof (modstr));
- printf("Last modified: %s (%ld).\n", modstr, (long) rc);
- } /* else */
- return(1);
- } /* cmd_getLastModTime */
- /* must have spaces trimmed prior to this call. */
- static int count_args(const char *str)
- {
- int retval = 0;
- int in_quotes = 0;
- if (str != NULL)
- {
- for (; *str != '\0'; str++)
- {
- if (*str == '\"')
- in_quotes = !in_quotes;
- else if ((*str == ' ') && (!in_quotes))
- retval++;
- } /* for */
- retval++;
- } /* if */
- return(retval);
- } /* count_args */
- static int cmd_help(char *args);
- typedef struct
- {
- const char *cmd;
- int (*func)(char *args);
- int argcount;
- const char *usage;
- } command_info;
- static const command_info commands[] =
- {
- { "quit", cmd_quit, 0, NULL },
- { "q", cmd_quit, 0, NULL },
- { "help", cmd_help, 0, NULL },
- { "init", cmd_init, 1, "<argv0>" },
- { "deinit", cmd_deinit, 0, NULL },
- { "addarchive", cmd_addarchive, 2, "<archiveLocation> <append>" },
- { "removearchive", cmd_removearchive, 1, "<archiveLocation>" },
- { "enumerate", cmd_enumerate, 1, "<dirToEnumerate>" },
- { "ls", cmd_enumerate, 1, "<dirToEnumerate>" },
- { "getlasterror", cmd_getlasterror, 0, NULL },
- { "getdirsep", cmd_getdirsep, 0, NULL },
- { "getcdromdirs", cmd_getcdromdirs, 0, NULL },
- { "getsearchpath", cmd_getsearchpath, 0, NULL },
- { "getbasedir", cmd_getbasedir, 0, NULL },
- { "getuserdir", cmd_getuserdir, 0, NULL },
- { "getwritedir", cmd_getwritedir, 0, NULL },
- { "setwritedir", cmd_setwritedir, 1, "<newWriteDir>" },
- { "permitsymlinks", cmd_permitsyms, 1, "<1or0>" },
- { "setsaneconfig", cmd_setsaneconfig, 5, "<org> <appName> <arcExt> <includeCdRoms> <archivesFirst>" },
- { "mkdir", cmd_mkdir, 1, "<dirToMk>" },
- { "delete", cmd_delete, 1, "<dirToDelete>" },
- { "getrealdir", cmd_getrealdir, 1, "<fileToFind>" },
- { "exists", cmd_exists, 1, "<fileToCheck>" },
- { "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>" },
- { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>" },
- { "setbuffer", cmd_setbuffer, 1, "<bufferSize>" },
- { "stressbuffer", cmd_stressbuffer, 1, "<bufferSize>" },
- { NULL, NULL, -1, NULL }
- };
- static void output_usage(const char *intro, const command_info *cmdinfo)
- {
- if (cmdinfo->argcount == 0)
- printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
- else
- printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
- } /* output_usage */
- static int cmd_help(char *args)
- {
- const command_info *i;
- printf("Commands:\n");
- for (i = commands; i->cmd != NULL; i++)
- output_usage(" -", i);
- return(1);
- } /* output_cmd_help */
- static void trim_command(const char *orig, char *copy)
- {
- const char *i;
- char *writeptr = copy;
- int spacecount = 0;
- int have_first = 0;
- for (i = orig; *i != '\0'; i++)
- {
- if (*i == ' ')
- {
- if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
- {
- if ((have_first) && (!spacecount))
- {
- spacecount++;
- *writeptr = ' ';
- writeptr++;
- } /* if */
- } /* if */
- } /* if */
- else
- {
- have_first = 1;
- spacecount = 0;
- *writeptr = *i;
- writeptr++;
- } /* else */
- } /* for */
- *writeptr = '\0';
- /*
- printf("\n command is [%s].\n", copy);
- */
- } /* trim_command */
- static int process_command(char *complete_cmd)
- {
- const command_info *i;
- char *cmd_copy;
- char *args;
- int rc = 1;
- if (complete_cmd == NULL) /* can happen if user hits CTRL-D, etc. */
- {
- printf("\n");
- return(0);
- } /* if */
- cmd_copy = malloc(strlen(complete_cmd) + 1);
- if (cmd_copy == NULL)
- {
- printf("\n\n\nOUT OF MEMORY!\n\n\n");
- return(0);
- } /* if */
- trim_command(complete_cmd, cmd_copy);
- args = strchr(cmd_copy, ' ');
- if (args != NULL)
- {
- *args = '\0';
- args++;
- } /* else */
- if (cmd_copy[0] != '\0')
- {
- for (i = commands; i->cmd != NULL; i++)
- {
- if (strcmp(i->cmd, cmd_copy) == 0)
- {
- if ((i->argcount >= 0) && (count_args(args) != i->argcount))
- output_usage("usage:", i);
- else
- rc = i->func(args);
- break;
- } /* if */
- } /* for */
- if (i->cmd == NULL)
- printf("Unknown command. Enter \"help\" for instructions.\n");
- #if (defined PHYSFS_HAVE_READLINE)
- add_history(complete_cmd);
- if (history_file)
- {
- fprintf(history_file, "%s\n", complete_cmd);
- fflush(history_file);
- } /* if */
- #endif
- } /* if */
- free(cmd_copy);
- return(rc);
- } /* process_command */
- static void open_history_file(void)
- {
- #if (defined PHYSFS_HAVE_READLINE)
- #if 0
- const char *envr = getenv("TESTPHYSFS_HISTORY");
- if (!envr)
- return;
- #else
- char envr[256];
- strcpy(envr, PHYSFS_getUserDir());
- strcat(envr, ".testphys_history");
- #endif
- if (access(envr, F_OK) == 0)
- {
- char buf[512];
- FILE *f = fopen(envr, "r");
- if (!f)
- {
- printf("\n\n"
- "Could not open history file [%s] for reading!\n"
- " Will not have past history available.\n\n",
- envr);
- return;
- } /* if */
- do
- {
- fgets(buf, sizeof (buf), f);
- if (buf[strlen(buf) - 1] == '\n')
- buf[strlen(buf) - 1] = '\0';
- add_history(buf);
- } while (!feof(f));
- fclose(f);
- } /* if */
- history_file = fopen(envr, "ab");
- if (!history_file)
- {
- printf("\n\n"
- "Could not open history file [%s] for appending!\n"
- " Will not be able to record this session's history.\n\n",
- envr);
- } /* if */
- #endif
- } /* open_history_file */
- int main(int argc, char **argv)
- {
- char *buf = NULL;
- int rc = 0;
- #if (defined __MWERKS__)
- extern tSIOUXSettings SIOUXSettings;
- SIOUXSettings.asktosaveonclose = 0;
- SIOUXSettings.autocloseonquit = 1;
- SIOUXSettings.rows = 40;
- SIOUXSettings.columns = 120;
- #endif
- printf("\n");
- if (!PHYSFS_init(argv[0]))
- {
- printf("PHYSFS_init() failed!\n reason: %s.\n", PHYSFS_getLastError());
- return(1);
- } /* if */
- output_versions();
- output_archivers();
- open_history_file();
- printf("Enter commands. Enter \"help\" for instructions.\n");
- do
- {
- #if (defined PHYSFS_HAVE_READLINE)
- buf = readline("> ");
- #else
- int i;
- buf = malloc(512);
- memset(buf, '\0', 512);
- printf("> ");
- for (i = 0; i < 511; i++)
- {
- int ch = fgetc(stdin);
- if (ch == EOF)
- {
- strcpy(buf, "quit");
- break;
- } /* if */
- else if ((ch == '\n') || (ch == '\r'))
- {
- buf[i] = '\0';
- break;
- } /* else if */
- else if (ch == '\b')
- {
- if (i > 0)
- i--;
- } /* else if */
- else
- {
- buf[i] = (char) ch;
- } /* else */
- } /* for */
- #endif
- rc = process_command(buf);
- if (buf != NULL)
- free(buf);
- } while (rc);
- if (!PHYSFS_deinit())
- printf("PHYSFS_deinit() failed!\n reason: %s.\n", PHYSFS_getLastError());
- if (history_file)
- fclose(history_file);
- /*
- printf("\n\ntest_physfs written by ryan c. gordon.\n");
- printf(" it makes you shoot teh railgun bettar.\n");
- */
- return(0);
- } /* main */
- /* end of test_physfs.c ... */
|