📄 tmsea.c
字号:
sprintf(buf,"\n {\n Dir, Path%d, 0, 0, Null\n }",NumElements);
write(fo, buf, strlen(buf));
/* fill in path names in temp output file 2 */
sprintf(buf,"Char Path%d[] = \"%s\";\n",
NumElements,outpath);
write(fh, buf, strlen(buf));
}
/************************************************************************
//
*************************************************************************/
static void InitArchieveImage(){
out_name_temp = (char *)malloc( strlen(TEMP_BASE)+5);
out_namet = (char *)malloc( strlen(TEMP_BASE)+3);
out_nameh = (char *)malloc( strlen(TEMP_BASE)+3);
sprintf(out_name_temp,"%s.tmp",TEMP_BASE);
sprintf(out_namet,"%s.t",TEMP_BASE);
sprintf(out_nameh,"%s.h",TEMP_BASE);
/* unlink the files just in case they already exist */
chmod(out_name_temp,0x1FF);
chmod(out_namet,0x1FF);
chmod(out_nameh,0x1FF);
unlink(out_name_temp);
unlink(out_namet);
unlink(out_nameh);
/* open/create specified file this is temp file */
fo = open(out_name_temp,O_RDWR|O_CREAT|O_BINARY,0x1FF);
if (fo == -1)
{
printf("%s: cannot create output file %s\n", progname, out_name_temp);
exit(1);
}
/* open temp output file #2 */
ft = open(out_namet,O_RDWR|O_CREAT|O_BINARY,0x1FF);
if (ft == -1)
{
printf("%s: cannot create temp output file %s\n", progname, out_namet);
return;
}
/* open temp output file #3 */
fh = open(out_nameh,O_RDWR|O_CREAT|O_BINARY,0x1FF);
if (fh == -1)
{
printf("%s: cannot create temp output file %s\n", progname, out_nameh);
return;
}
/* initialize the header file */
sprintf(buf,"#include \"tmlib/tmtypes.h\"\n\n");
write(fh, buf, strlen(buf));
sprintf(buf,"#define File 1\n#define Dir 0\n\n");
write(fh, buf, strlen(buf));
if(NoCompression == False)
sprintf(buf,"#define _COMPRESSION_ 1\n\n");
else
sprintf(buf,"#undef _COMPRESSION_ \n\n");
write(fh, buf, strlen(buf));
if(verbose == True)
sprintf(buf,"#define _VERBOSE_ 1\n\n");
else
sprintf(buf,"#undef _VERBOSE_ \n\n");
write(fh, buf, strlen(buf));
sprintf(buf,"struct FileDirList\n{\n Int File_Dir; \n");
write(fh, buf, strlen(buf));
sprintf(buf," Char *Pathname; \n UInt32 FileLength;\n");
write(fh, buf, strlen(buf));
sprintf(buf," UInt32 CompLen;\n Byte *Data; \n};\n\n");
write(fh, buf, strlen(buf));
/* initialize temp output file (data struct file) */
sprintf(buf,"struct FileDirList FDList[FDLIST_ARRAY_LENGTH] =\n{");
write(fo, buf, strlen(buf));
/* initialize t file (data file) */
sprintf(buf,"{.data }\n{.align 4}\n");
write(ft, buf, strlen(buf));
}
/* create initial output directory structure */
static void createOutDir(char *outputdir){
int count = strlen("/");
/* while data to read */
while (count <= strlen(outputdir))
{
/* if this prev subdir or final dir */
if(outputdir[count] == '/' || outputdir[count] == '\0'){
char *buffer= (char *)malloc( count+1);
/* copy dir to buffer */
memcpy(buffer,outputdir, count);
buffer[count] = '\0';
/* copy dir to h file */
copy_dir(buffer);
NumElements++;
free(buffer);
}
count++;
}
}
/* copy the file system */
static void copy_fs(char *inpath, char *outpath){
DIR *dirp;
struct stat buf;
struct dirent *direntp;
dirp = opendir( inpath );
if (dirp) {
while ( (direntp = readdir( dirp )) != NULL ) {
if (strcmp(direntp->d_name,".") != 0 && strcmp(direntp->d_name,"..") != 0) {
char *in_buffer= (char *)malloc( strlen(inpath)+strlen(direntp->d_name)+2);
char *out_buffer= (char *)malloc( strlen(outpath)+strlen(direntp->d_name)+2);
sprintf(in_buffer,"%s/%s",inpath,direntp->d_name);
sprintf(out_buffer,"%s/%s",outpath,direntp->d_name);
if (stat(in_buffer,&buf) == -1) printf("STAT ERROR \"%s\"\n", in_buffer);
if (S_ISDIR(buf.st_mode)) {
copy_dir(out_buffer);
NumElements++;
copy_fs(in_buffer,out_buffer);
}
else {
copy_file(in_buffer,out_buffer);
NumElements++;
}
free(in_buffer);
free(out_buffer);
}
}
(void)closedir( dirp );
}
}
/* finish the archieve image */
static void FinishArchieveImage(){
struct stat sbuf;
int size;
/* finish header file (Path file) */
sprintf(buf,"\n\n#define FDLIST_ARRAY_LENGTH %d\n\n",NumElements);
write(fh, buf, strlen(buf));
/* finish temp output file (data struct file) */
sprintf(buf,"\n};\n");
write(fo, buf, strlen(buf));
/* concatinate end of temp output file to header file */
/* seek to start of f2 */
lseek(fo,0,SEEK_SET);
/* copy from f2 to fo */
do {
size= read (fo, buf, BLOCKSIZE);
write (fh, buf, size);
} while (size > 0);
/* close temp output file */
if (fo) close(fo);
/* close t output file */
if (ft) close(ft);
/* close heaer output file */
if (fh) close(fh);
/* delete temp files */
unlink(out_name_temp);
/* change mode of output files */
chmod(out_namet,0x1FF);
chmod(out_nameh,0x1FF);
}
static void ReadArchieveDir(char *input_dir, char *output_dir){
InitArchieveImage();
createOutDir(output_dir);
copy_fs(input_dir,output_dir);
FinishArchieveImage();
}
void print_help(){
printf("usage: %s input-dir [-od output-dir] [-e[l|b]] [-host type] \\\n", progname);
printf(" [-nostandard] [-flashbsp] [-v] [-o output-file] [-cflags \"ccstring\"] \n");
printf(" [-ldflags \"ldstring\"] [-tcs \"tcspath\"] [-cpath \"sourcepath\"] \n");
printf(" \n");
printf(" '%s' creates a Self Extacting Archieve (SEA) output file. Given a current\n",progname);
printf(" input directory and its corresponding archieve directory, the input directory \n");
printf(" will be embedded in a compressed form in a specified output file.\n");
printf(" The output file can be downloaded and run, where the input directory will be\n");
printf(" be uncompressed and placed in the archieve output directory.\n");
printf(" \n");
printf(" input-dir : directory from which input data will be read.\n");
printf(" -od output-dir : directory to which output data will be written in archieve.\n");
printf(" default '/flash' - if used with flash must begin with '/flash'.\n");
printf(" -e[lb] : specify endianness of SEA output file.\n");
printf(" -eb for big endian (default).\n");
printf(" -el for little endian.\n");
printf(" -host type : Specify one of the following types of host processor \n");
printf(" nohost : No Host (default)\n");
printf(" tmsim : TM Simulator is Host\n");
printf(" Win95 : Win95 is Host\n");
printf(" MacOS : MacOS is Host\n");
printf(" WinNT : WinNT is Host\n");
printf(" -nostandard : Disable default usage of 'TriMedia' Flash.\n");
printf(" -flashbsp : Use BSP as the flash file system hardware interface (default).\n");
printf(" To disable this option use the -noflashbsp option which causes\n");
printf(" tmSEA to use flash specific driver as specified in the makefile.\n");
printf(" -v : Specify that the auto-generated part of output is verbose.\n");
printf(" -cflags \"cc_string\" : A string of arguements to pass to tmcc when compiling \n");
printf(" sources for SEA application.\n");
printf(" -ldflags \"ld_string\" : A string of arguements to pass to tmcc when linking SEA\n");
printf(" application.\n");
printf(" -o output-file: name of SEA output-file that will be created. default output\n");
printf(" name is 'a.out'. \n");
printf(" -tcs \"tcspath\" : Current TCS path that tmSEA will use to build TriMedia image.\n");
#ifdef _WIN32
printf(" default is C:\\\\TriMedia \n");
#else
printf(" default is /usr/local/tcs \n");
#endif
printf(" -cpath \"sourcepath\" : path of c source file, tmunpackimage.c, that tmSEA will use\n");
#ifdef _WIN32
printf(" to create a TriMeida image. default is .\\\\tmunpackimage \n");
#else
printf(" to create a TriMeida image. default is ./tmunpackimage\n");
#endif
exit(1);
}
int
parse_options(int argc, char *argv[]){
int ax;
Bool mm;
int nargs;
int optx;
void * ptr;
if(!tsaCmdOptParse(argc, argv, Nelts(options), options, Null)){
printf("Parsing options Failen\n" );
exit(-1);
}
/* initialize global variable */
*nostandard_buf = '\0';
/* get first/default parameters */
/* get outputdir value */
outputdir = (char *)tsaCmdOptOption(Opt_outputdir);
/* get endian value */
bigendian = (char *)tsaCmdOptOption(Opt_bigendian);
/* get host value */
host = (char *)tsaCmdOptOption(Opt_host);
/* verbosity */
verbose = (Bool)tsaCmdOptOption(Opt_verbose);
/* get output value */
output = (char *)tsaCmdOptOption(Opt_output);
/* get cflags value */
cflags = (char *)tsaCmdOptOption(Opt_cflags);
/* get ldflags value */
ldflags = (char *)tsaCmdOptOption(Opt_ldflags);
/* get nostandard value */
nostandard = (Bool)tsaCmdOptOption(Opt_nostandard);
/* get flashbsp value */
flashbsp = (Bool)tsaCmdOptOption(Opt_flashbsp);
/* get compression value */
NoCompression = (Bool)tsaCmdOptOption(Opt_NoCompression);
/* get tcspath value */
tcspath = (char *)tsaCmdOptOption(Opt_tcspath);
/* get cpath value */
cpath = (char *)tsaCmdOptOption(Opt_cpath);
/* check to make sure input parameters are correct */
/* must be exactly two arguements */
if(tsaCmdOptNumberOfArguments() !=2){
print_help();
exit(-1);
}
/* parse input arguements as reqired */
/* set up default values */
inputdir = tsaCmdOptArgument(1);
/* parse input options as reqired */
if(strcmp(host,"nohost") == 0)
host_int = tmNoHost;
else if(strcmp(host,"tmsim") == 0)
host_int = tmTmSimHost;
else if(strcmp(host,"Win95") == 0)
host_int = tmWin95Host;
else if(strcmp(host,"MacOS") == 0)
host_int = tmMacOSHost;
else if(strcmp(host,"WinNT") == 0)
host_int = tmWinNTHost;
else {
printf("error: specified host type '%s' not valid\n",ptr);
print_help();
exit(-1);
}
/* if specified host is not no host */
if(host_int != tmNoHost)
printf("Warning: Flashspecific.c file may require changes!\n");
/* check if big Endian or little Endian support required */
if(strcmp(bigendian,"l") == 0)
Big_Endian = False;
else if(strcmp(bigendian,"b") == 0)
Big_Endian = True;
else {
printf("error: specified endianness support '%s' not valid\n",ptr);
print_help();
exit(-1);
}
/* if nostandard value is False */
if(nostandard == False){
strcpy(nostandard_buf,"-lio -tmld -u _FlashFS --");
}
}
int main(int argc, char *argv[])
{
int i = 1;
int ret = 0;
DIR *dirp;
struct dirent *direntp;
/* default program name */
progname = argv[0];
/* extract user input options */
parse_options(argc, argv);
/* remove old output first (if it exists) */
chmod(output,0x1FF);
unlink(output);
printf("Creating temporary directory\n");
/* make temperary sub directory */
mkdir(TEMP_DIR,0x1FF);
chmod(TEMP_DIR,0x1FF);
printf("Copying files to temporary directory\n");
sprintf(buffer,".%stmunpackarch.c",SLASH TEMP_DIR SLASH);
sprintf(tcspath_buffer,"%s%stmunpackarch.c",cpath,SLASH);
unlink(buffer);
copy(tcspath_buffer, buffer);
printf("Packing Archieve\n");
/* read contents of Arhcieve dir */
ReadArchieveDir(inputdir,outputdir);
printf("Building Self Extracting Archieve\n");
/* do a make in temp */
sprintf(buffer,"%s%sbin%stmcc -e%s -host %s %stmunpackarch.c -c tmunpackarch.o %s",tcspath, SLASH, SLASH,bigendian,host,TEMP_DIR SLASH,cflags);
rewrite_slashes( buffer );
printf("%s\n",buffer);
if(system(buffer) != EXIT_SUCCESS){
printf("Fatal Error; Following \"System Call\" failed :\n %s\n",buffer);
goto end;
}
/* copy object file to temp dir */
chmod("tmunpackarch.o",0x1FF);
sprintf(buffer,".%stmunpackarch.o",SLASH TEMP_DIR SLASH);
copy("tmunpackarch.o",buffer);
unlink("tmunpackarch.o");
/* if using standard TriMedia Flash specific source file*/
if (nostandard == False && flashbsp == False ){
sprintf(tcspath_buffer,MY_FLASHSPECIFIC);
sprintf(buffer,"%sFlashSpecific.c",TEMP_DIR SLASH);
unlink(buffer);
ret = copy(tcspath_buffer, buffer);
if(ret == -1)
printf("copy %d from %s to %s\n",ret,tcspath_buffer,buffer);
sprintf(buffer,"%s%sbin%stmcc -e%s -host %s %sFlashSpecific.c -c FlashSpecific.o %s",tcspath, SLASH, SLASH, bigendian,host,TEMP_DIR SLASH,cflags);
rewrite_slashes( buffer );
printf("%s\n",buffer);
if(system(buffer) != EXIT_SUCCESS){
printf("Fatal Error; Following \"System Call\" failed :\n %s\n",buffer);
goto end;
}
/* copy object file to temp dir */
chmod("FlashSpecific.o",0x1FF);
sprintf(buffer,".%sFlashSpecific.o",SLASH TEMP_DIR SLASH);
copy("FlashSpecific.o",buffer);
unlink("FlashSpecific.o");
sprintf(buffer,"%s%sbin%stmcc -e%s -host %s %stmunpackarch.o %sFlashSpecific.o %stmpackedarch.t -o %stmunpackarch.out -lz %s %s",tcspath, SLASH, SLASH,bigendian,host,TEMP_DIR SLASH,TEMP_DIR SLASH,TEMP_DIR SLASH,TEMP_DIR SLASH,ldflags,nostandard_buf);
rewrite_slashes( buffer );
printf("%s\n",buffer);
if(system(buffer) != EXIT_SUCCESS){
printf("Fatal Error; Following \"System Call\" failed :\n %s\n",buffer);
goto end;
}
}
else{
sprintf(buffer,"%s%sbin%stmcc -e%s -host %s %stmunpackarch.o %stmpackedarch.t -o %stmunpackarch.out -lz %s %s",tcspath, SLASH, SLASH,bigendian,host,TEMP_DIR SLASH,TEMP_DIR SLASH,TEMP_DIR SLASH,ldflags,nostandard_buf);
rewrite_slashes( buffer );
printf("%s\n",buffer);
if(system(buffer) != EXIT_SUCCESS){
printf("Fatal Error; Following \"System Call\" failed :\n %s\n",buffer);
goto end;
}
}
printf("Moving %s\n",output);
/* copy tmunpackimage.out file to home dir with new executable name */
sprintf(buffer,"%stmunpackarch.out",TEMP_DIR SLASH);
copy(buffer,output);
end:
sprintf(buffer,".%s",SLASH TEMP_DIR);
dirp = opendir(buffer );
if (dirp) {
while ( (direntp = readdir( dirp )) != NULL ) {
if (strcmp(direntp->d_name,".") != 0 && strcmp(direntp->d_name,"..") != 0) {
sprintf(buffer,"%s%s",TEMP_DIR SLASH ,direntp->d_name);
chmod(buffer,0x1FF);
unlink(buffer);
}
}
(void)closedir(dirp);
}
/* remove temporary directory */
printf("Removing temporary directory\n");
rmdir(TEMP_DIR);
/* exit */
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -