10 #include <netatalk/endian.h>
16 char *forkname[] = { "data", "resource" };
17 char *name[] = { "unhex",
27 int from_open( un, file, fh, flags )
36 return( hqx_open( file, O_RDONLY, fh, flags ));
40 return( bin_open( file, O_RDONLY, fh, flags ));
44 return( nad_open( file, O_RDONLY, fh, flags ));
48 return( single_open( file, O_RDONLY, fh, flags ));
55 int from_read( un, fork, buf, len )
65 return( hqx_read( fork, buf, len ));
68 return( bin_read( fork, buf, len ));
71 return( nad_read( fork, buf, len ));
75 return( single_read( fork, buf, len ));
89 return( hqx_close( KEEP ));
92 return( bin_close( KEEP ));
95 return( nad_close( KEEP ));
99 return( single_close( KEEP ));
106 int to_open( to, file, fh, flags )
116 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
121 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
129 int to_write( to, fork, bufc )
139 return( nad_write( fork, forkbuf, bufc ));
144 return( bin_write( fork, forkbuf, bufc ));
152 int to_close( to, keepflag )
161 return( nad_close( keepflag ));
166 return( bin_close( keepflag ));
174 int megatron( path, module, newname, flags )
175 char *path, *newname;
185 * If the source file is not stdin, make sure it exists and
186 * that it is not a directory.
189 if ( strcmp( path, STDIN ) != 0 ) {
190 if ( stat( path, &st ) < 0 ) {
194 if ( S_ISDIR( st.st_mode )) {
195 fprintf( stderr, "%s is a directory.\n", path );
201 * Open the source file and fill in the file header structure.
204 memset( &fh, 0, sizeof( fh ));
205 if ( from_open( module, path, &fh ) < 0 ) {
209 if ( flags & OPTION_HEADERONLY ) {
214 printf("name: %s\n",fh.name);
215 printf("comment: %s\n",fh.comment);
216 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
217 printf("creator: '%4s'\n", buf);
218 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
219 printf("type: '%4s'\n", buf);
220 for(i=0; i < NUMFORKS; ++i)
221 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
222 t = AD_DATE_TO_UNIX(fh.create_date);
223 printf("creation date: %s", ctime(&t));
224 t = AD_DATE_TO_UNIX(fh.mod_date);
225 printf("modification date: %s", ctime(&t));
226 t = AD_DATE_TO_UNIX(fh.backup_date);
227 printf("backup date: %s", ctime(&t));
228 return( from_close( module ));
232 * Open the target file and write out the file header info.
233 * set the header to the new filename if it has been supplied.
237 strcpy(fh.name, newname);
239 if ( to_open( module, path, &fh, flags ) < 0 ) {
240 (void)from_close( module );
245 * Read in and write out the data and resource forks.
248 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
250 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
252 if ( to_write( module, fork, bufc ) != bufc ) {
253 fprintf( stderr, "%s: Probable write error\n", path );
254 to_close( module, TRASH );
255 (void)from_close( module );
261 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
262 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
263 ntohl( fh.forklen[ fork ] ));
265 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
266 fprintf( stderr, "%s: Problem with input, dude\n", path );
267 to_close( module, TRASH );
268 (void)from_close( module );
274 * Close up the files, and get out of here.
277 if ( to_close( module, KEEP ) < 0 ) {
278 perror( "megatron:" );
279 (void)to_close( module, TRASH );
281 return( from_close( module ));
284 int main( argc, argv )
290 int converts = sizeof(name) / sizeof(char *);
293 char *progname, newname[ADEDLEN_NAME + 1];
295 progname = strrchr( argv[ 0 ], '/' );
296 if (( progname == NULL ) || ( progname == '\0' )) {
297 progname = argv[ 0 ];
301 if ( CONVERTS != converts ) {
302 fprintf( stderr, "megatron: list of program links messed up\n" );
307 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
308 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
310 if ( module == -1 ) module = ( converts - 1 );
311 if ((module == NADHEADER) || (module == BINHEADER))
312 flags |= OPTION_HEADERONLY;
315 return( megatron( STDIN, module, newname, flags ));
319 for ( c = 1 ; c < argc ; ++c ) {
320 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
321 flags |= OPTION_HEADERONLY;
324 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
325 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
328 if (strcmp(argv[c], "--stdout") == 0) {
329 flags |= OPTION_STDOUT;
333 if ( rc = megatron( argv[ c ], module, newname, flags) != 0 ) {