14 #include <netatalk/endian.h>
18 char *forkname[] = { "data", "resource" };
19 char *name[] = { "unhex",
29 int from_open( un, file, fh, flags )
38 return( hqx_open( file, O_RDONLY, fh, flags ));
42 return( bin_open( file, O_RDONLY, fh, flags ));
46 return( nad_open( file, O_RDONLY, fh, flags ));
50 return( single_open( file, O_RDONLY, fh, flags ));
57 int from_read( un, fork, buf, len )
67 return( hqx_read( fork, buf, len ));
70 return( bin_read( fork, buf, len ));
73 return( nad_read( fork, buf, len ));
77 return( single_read( fork, buf, len ));
91 return( hqx_close( KEEP ));
94 return( bin_close( KEEP ));
97 return( nad_close( KEEP ));
101 return( single_close( KEEP ));
108 int to_open( to, file, fh, flags )
118 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
123 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
131 int to_write( to, fork, bufc )
141 return( nad_write( fork, forkbuf, bufc ));
146 return( bin_write( fork, forkbuf, bufc ));
154 int to_close( to, keepflag )
163 return( nad_close( keepflag ));
168 return( bin_close( keepflag ));
176 int megatron( path, module, newname, flags )
177 char *path, *newname;
187 * If the source file is not stdin, make sure it exists and
188 * that it is not a directory.
191 if ( strcmp( path, STDIN ) != 0 ) {
192 if ( stat( path, &st ) < 0 ) {
196 if ( S_ISDIR( st.st_mode )) {
197 fprintf( stderr, "%s is a directory.\n", path );
203 * Open the source file and fill in the file header structure.
206 memset( &fh, 0, sizeof( fh ));
207 if ( from_open( module, path, &fh ) < 0 ) {
211 if ( flags & OPTION_HEADERONLY ) {
216 printf("name: %s\n",fh.name);
217 printf("comment: %s\n",fh.comment);
218 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
219 printf("creator: '%4s'\n", buf);
220 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
221 printf("type: '%4s'\n", buf);
222 for(i=0; i < NUMFORKS; ++i)
223 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
224 t = AD_DATE_TO_UNIX(fh.create_date);
225 printf("creation date: %s", ctime(&t));
226 t = AD_DATE_TO_UNIX(fh.mod_date);
227 printf("modification date: %s", ctime(&t));
228 t = AD_DATE_TO_UNIX(fh.backup_date);
229 printf("backup date: %s", ctime(&t));
230 return( from_close( module ));
234 * Open the target file and write out the file header info.
235 * set the header to the new filename if it has been supplied.
239 strcpy(fh.name, newname);
241 if ( to_open( module, path, &fh, flags ) < 0 ) {
242 (void)from_close( module );
247 * Read in and write out the data and resource forks.
250 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
252 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
254 if ( to_write( module, fork, bufc ) != bufc ) {
255 fprintf( stderr, "%s: Probable write error\n", path );
256 to_close( module, TRASH );
257 (void)from_close( module );
263 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
264 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
265 ntohl( fh.forklen[ fork ] ));
267 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
268 fprintf( stderr, "%s: Problem with input, dude\n", path );
269 to_close( module, TRASH );
270 (void)from_close( module );
276 * Close up the files, and get out of here.
279 if ( to_close( module, KEEP ) < 0 ) {
280 perror( "megatron:" );
281 (void)to_close( module, TRASH );
283 return( from_close( module ));
286 int main( argc, argv )
292 int converts = sizeof(name) / sizeof(char *);
295 char *progname, newname[ADEDLEN_NAME + 1];
297 progname = strrchr( argv[ 0 ], '/' );
298 if (( progname == NULL ) || ( progname == '\0' )) {
299 progname = argv[ 0 ];
303 if ( CONVERTS != converts ) {
304 fprintf( stderr, "megatron: list of program links messed up\n" );
309 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
310 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
312 if ( module == -1 ) module = ( converts - 1 );
313 if ((module == NADHEADER) || (module == BINHEADER))
314 flags |= OPTION_HEADERONLY;
317 return( megatron( STDIN, module, newname, flags ));
321 for ( c = 1 ; c < argc ; ++c ) {
322 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
323 flags |= OPTION_HEADERONLY;
326 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
327 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
330 if (strcmp(argv[c], "--stdout") == 0) {
331 flags |= OPTION_STDOUT;
335 if ( rc = megatron( argv[ c ], module, newname, flags) != 0 ) {