2 * $Id: megatron.c,v 1.14 2010-01-27 21:27:53 didg Exp $
7 #endif /* HAVE_CONFIG_H */
10 #include <sys/param.h>
18 #include <netatalk/endian.h>
25 char *forkname[] = { "data", "resource" };
26 static char forkbuf[8192];
27 static char *name[] = { "unhex",
37 static int from_open(int un, char *file, struct FHeader *fh, int flags)
43 return( hqx_open( file, O_RDONLY, fh, flags ));
47 return( bin_open( file, O_RDONLY, fh, flags ));
51 return( nad_open( file, O_RDONLY, fh, flags ));
55 return( single_open( file, O_RDONLY, fh, flags ));
62 static ssize_t from_read(int un, int fork, char *buf, size_t len)
68 return( hqx_read( fork, buf, len ));
71 return( bin_read( fork, buf, len ));
74 return( nad_read( fork, buf, len ));
78 return( single_read( fork, buf, len ));
85 static int from_close(int un)
91 return( hqx_close( KEEP ));
94 return( bin_close( KEEP ));
97 return( nad_close( KEEP ));
101 return( single_close( KEEP ));
108 static int to_open(int to, char *file, struct FHeader *fh, int flags)
115 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
120 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
128 static ssize_t to_write(int to, int fork, size_t bufc)
135 return( nad_write( fork, forkbuf, bufc ));
140 return( bin_write( fork, forkbuf, bufc ));
148 static int to_close(int to, int keepflag)
155 return( nad_close( keepflag ));
160 return( bin_close( keepflag ));
168 static int megatron( char *path, int module, char *newname, int flags)
177 * If the source file is not stdin, make sure it exists and
178 * that it is not a directory.
181 if ( strcmp( path, STDIN ) != 0 ) {
182 if ( stat( path, &st ) < 0 ) {
186 if ( S_ISDIR( st.st_mode )) {
187 fprintf( stderr, "%s is a directory.\n", path );
193 * Open the source file and fill in the file header structure.
196 memset( &fh, 0, sizeof( fh ));
197 if ( from_open( module, path, &fh, flags ) < 0 ) {
201 if ( flags & OPTION_HEADERONLY ) {
206 printf("name: %s\n",fh.name);
207 printf("comment: %s\n",fh.comment);
208 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
209 printf("creator: '%4s'\n", buf);
210 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
211 printf("type: '%4s'\n", buf);
212 for(i=0; i < NUMFORKS; ++i)
213 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
214 t = AD_DATE_TO_UNIX(fh.create_date);
215 printf("creation date: %s", ctime(&t));
216 t = AD_DATE_TO_UNIX(fh.mod_date);
217 printf("modification date: %s", ctime(&t));
218 t = AD_DATE_TO_UNIX(fh.backup_date);
219 printf("backup date: %s", ctime(&t));
220 return( from_close( module ));
224 * Open the target file and write out the file header info.
225 * set the header to the new filename if it has been supplied.
229 strcpy(fh.name, newname);
231 if ( to_open( module, path, &fh, flags ) < 0 ) {
232 (void)from_close( module );
237 * Read in and write out the data and resource forks.
240 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
242 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
244 if ( to_write( module, fork, bufc ) != bufc ) {
245 fprintf( stderr, "%s: Probable write error\n", path );
246 to_close( module, TRASH );
247 (void)from_close( module );
253 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
254 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
255 ntohl( fh.forklen[ fork ] ));
257 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
258 fprintf( stderr, "%s: Problem with input, dude\n", path );
259 to_close( module, TRASH );
260 (void)from_close( module );
266 * Close up the files, and get out of here.
269 if ( to_close( module, KEEP ) < 0 ) {
270 perror( "megatron:" );
271 (void)to_close( module, TRASH );
273 return( from_close( module ));
276 int main(int argc, char **argv)
280 int converts = sizeof(name) / sizeof(char *);
283 char *progname, newname[ADEDLEN_NAME + 1];
285 progname = strrchr( argv[ 0 ], '/' );
286 if (( progname == NULL ) || ( *progname == '\0' )) {
287 progname = argv[ 0 ];
291 if ( CONVERTS != converts ) {
292 fprintf( stderr, "megatron: list of program links messed up\n" );
297 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
298 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
300 if ( module == -1 ) module = ( converts - 1 );
301 if ((module == NADHEADER) || (module == BINHEADER))
302 flags |= OPTION_HEADERONLY;
305 return( megatron( STDIN, module, newname, flags ));
309 for ( c = 1 ; c < argc ; ++c ) {
310 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
311 flags |= OPTION_HEADERONLY;
314 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
315 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
318 if (strcmp(argv[c], "--stdout") == 0) {
319 flags |= OPTION_STDOUT;
322 if (strcmp(argv[c], "--euc") == 0) {
323 flags |= OPTION_EUCJP;
326 if (strcmp(argv[c], "--sjis") == 0) {
327 flags |= OPTION_SJIS;
330 rc = megatron( argv[ c ], module, newname, flags);