14 #include <netatalk/endian.h>
20 char *forkname[] = { "data", "resource" };
21 char *name[] = { "unhex",
31 int from_open( un, file, fh, flags )
40 return( hqx_open( file, O_RDONLY, fh, flags ));
44 return( bin_open( file, O_RDONLY, fh, flags ));
48 return( nad_open( file, O_RDONLY, fh, flags ));
52 return( single_open( file, O_RDONLY, fh, flags ));
59 int from_read( un, fork, buf, len )
69 return( hqx_read( fork, buf, len ));
72 return( bin_read( fork, buf, len ));
75 return( nad_read( fork, buf, len ));
79 return( single_read( fork, buf, len ));
93 return( hqx_close( KEEP ));
96 return( bin_close( KEEP ));
99 return( nad_close( KEEP ));
103 return( single_close( KEEP ));
110 int to_open( to, file, fh, flags )
120 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
125 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
133 int to_write( to, fork, bufc )
143 return( nad_write( fork, forkbuf, bufc ));
148 return( bin_write( fork, forkbuf, bufc ));
156 int to_close( to, keepflag )
165 return( nad_close( keepflag ));
170 return( bin_close( keepflag ));
178 int megatron( path, module, newname, flags )
179 char *path, *newname;
189 * If the source file is not stdin, make sure it exists and
190 * that it is not a directory.
193 if ( strcmp( path, STDIN ) != 0 ) {
194 if ( stat( path, &st ) < 0 ) {
198 if ( S_ISDIR( st.st_mode )) {
199 fprintf( stderr, "%s is a directory.\n", path );
205 * Open the source file and fill in the file header structure.
208 memset( &fh, 0, sizeof( fh ));
209 if ( from_open( module, path, &fh ) < 0 ) {
213 if ( flags & OPTION_HEADERONLY ) {
218 printf("name: %s\n",fh.name);
219 printf("comment: %s\n",fh.comment);
220 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
221 printf("creator: '%4s'\n", buf);
222 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
223 printf("type: '%4s'\n", buf);
224 for(i=0; i < NUMFORKS; ++i)
225 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
226 t = AD_DATE_TO_UNIX(fh.create_date);
227 printf("creation date: %s", ctime(&t));
228 t = AD_DATE_TO_UNIX(fh.mod_date);
229 printf("modification date: %s", ctime(&t));
230 t = AD_DATE_TO_UNIX(fh.backup_date);
231 printf("backup date: %s", ctime(&t));
232 return( from_close( module ));
236 * Open the target file and write out the file header info.
237 * set the header to the new filename if it has been supplied.
241 strcpy(fh.name, newname);
243 if ( to_open( module, path, &fh, flags ) < 0 ) {
244 (void)from_close( module );
249 * Read in and write out the data and resource forks.
252 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
254 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
256 if ( to_write( module, fork, bufc ) != bufc ) {
257 fprintf( stderr, "%s: Probable write error\n", path );
258 to_close( module, TRASH );
259 (void)from_close( module );
265 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
266 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
267 ntohl( fh.forklen[ fork ] ));
269 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
270 fprintf( stderr, "%s: Problem with input, dude\n", path );
271 to_close( module, TRASH );
272 (void)from_close( module );
278 * Close up the files, and get out of here.
281 if ( to_close( module, KEEP ) < 0 ) {
282 perror( "megatron:" );
283 (void)to_close( module, TRASH );
285 return( from_close( module ));
288 int main( argc, argv )
294 int converts = sizeof(name) / sizeof(char *);
297 char *progname, newname[ADEDLEN_NAME + 1];
299 progname = strrchr( argv[ 0 ], '/' );
300 if (( progname == NULL ) || ( progname == '\0' )) {
301 progname = argv[ 0 ];
305 if ( CONVERTS != converts ) {
306 fprintf( stderr, "megatron: list of program links messed up\n" );
311 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
312 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
314 if ( module == -1 ) module = ( converts - 1 );
315 if ((module == NADHEADER) || (module == BINHEADER))
316 flags |= OPTION_HEADERONLY;
319 return( megatron( STDIN, module, newname, flags ));
323 for ( c = 1 ; c < argc ; ++c ) {
324 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
325 flags |= OPTION_HEADERONLY;
328 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
329 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
332 if (strcmp(argv[c], "--stdout") == 0) {
333 flags |= OPTION_STDOUT;
337 if ( rc = megatron( argv[ c ], module, newname, flags) != 0 ) {