15 #include <netatalk/endian.h>
23 char *forkname[] = { "data", "resource" };
24 char *name[] = { "unhex",
34 int from_open( un, file, fh, 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 int from_read( un, fork, buf, len )
72 return( hqx_read( fork, buf, len ));
75 return( bin_read( fork, buf, len ));
78 return( nad_read( fork, buf, len ));
82 return( single_read( fork, buf, len ));
96 return( hqx_close( KEEP ));
99 return( bin_close( KEEP ));
102 return( nad_close( KEEP ));
106 return( single_close( KEEP ));
113 int to_open( to, file, fh, flags )
123 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
128 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
136 int to_write( to, fork, bufc )
146 return( nad_write( fork, forkbuf, bufc ));
151 return( bin_write( fork, forkbuf, bufc ));
159 int to_close( to, keepflag )
168 return( nad_close( keepflag ));
173 return( bin_close( keepflag ));
181 int megatron( path, module, newname, flags )
182 char *path, *newname;
192 * If the source file is not stdin, make sure it exists and
193 * that it is not a directory.
196 if ( strcmp( path, STDIN ) != 0 ) {
197 if ( stat( path, &st ) < 0 ) {
201 if ( S_ISDIR( st.st_mode )) {
202 fprintf( stderr, "%s is a directory.\n", path );
208 * Open the source file and fill in the file header structure.
211 memset( &fh, 0, sizeof( fh ));
212 if ( from_open( module, path, &fh ) < 0 ) {
216 if ( flags & OPTION_HEADERONLY ) {
221 printf("name: %s\n",fh.name);
222 printf("comment: %s\n",fh.comment);
223 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
224 printf("creator: '%4s'\n", buf);
225 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
226 printf("type: '%4s'\n", buf);
227 for(i=0; i < NUMFORKS; ++i)
228 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
229 t = AD_DATE_TO_UNIX(fh.create_date);
230 printf("creation date: %s", ctime(&t));
231 t = AD_DATE_TO_UNIX(fh.mod_date);
232 printf("modification date: %s", ctime(&t));
233 t = AD_DATE_TO_UNIX(fh.backup_date);
234 printf("backup date: %s", ctime(&t));
235 return( from_close( module ));
239 * Open the target file and write out the file header info.
240 * set the header to the new filename if it has been supplied.
244 strcpy(fh.name, newname);
246 if ( to_open( module, path, &fh, flags ) < 0 ) {
247 (void)from_close( module );
252 * Read in and write out the data and resource forks.
255 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
257 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
259 if ( to_write( module, fork, bufc ) != bufc ) {
260 fprintf( stderr, "%s: Probable write error\n", path );
261 to_close( module, TRASH );
262 (void)from_close( module );
268 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
269 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
270 ntohl( fh.forklen[ fork ] ));
272 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
273 fprintf( stderr, "%s: Problem with input, dude\n", path );
274 to_close( module, TRASH );
275 (void)from_close( module );
281 * Close up the files, and get out of here.
284 if ( to_close( module, KEEP ) < 0 ) {
285 perror( "megatron:" );
286 (void)to_close( module, TRASH );
288 return( from_close( module ));
291 int main( argc, argv )
297 int converts = sizeof(name) / sizeof(char *);
300 char *progname, newname[ADEDLEN_NAME + 1];
302 progname = strrchr( argv[ 0 ], '/' );
303 if (( progname == NULL ) || ( progname == '\0' )) {
304 progname = argv[ 0 ];
308 if ( CONVERTS != converts ) {
309 fprintf( stderr, "megatron: list of program links messed up\n" );
314 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
315 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
317 if ( module == -1 ) module = ( converts - 1 );
318 if ((module == NADHEADER) || (module == BINHEADER))
319 flags |= OPTION_HEADERONLY;
322 return( megatron( STDIN, module, newname, flags ));
326 for ( c = 1 ; c < argc ; ++c ) {
327 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
328 flags |= OPTION_HEADERONLY;
331 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
332 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
335 if (strcmp(argv[c], "--stdout") == 0) {
336 flags |= OPTION_STDOUT;
340 rc = megatron( argv[ c ], module, newname, flags);