15 #include <netatalk/endian.h>
19 char *forkname[] = { "data", "resource" };
20 char *name[] = { "unhex",
30 int from_open( un, file, fh, flags )
39 return( hqx_open( file, O_RDONLY, fh, flags ));
43 return( bin_open( file, O_RDONLY, fh, flags ));
47 return( nad_open( file, O_RDONLY, fh, flags ));
51 return( single_open( file, O_RDONLY, fh, flags ));
58 int from_read( un, fork, buf, 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 ));
92 return( hqx_close( KEEP ));
95 return( bin_close( KEEP ));
98 return( nad_close( KEEP ));
102 return( single_close( KEEP ));
109 int to_open( to, file, fh, flags )
119 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
124 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
132 int to_write( to, fork, bufc )
142 return( nad_write( fork, forkbuf, bufc ));
147 return( bin_write( fork, forkbuf, bufc ));
155 int to_close( to, keepflag )
164 return( nad_close( keepflag ));
169 return( bin_close( keepflag ));
177 int megatron( path, module, newname, flags )
178 char *path, *newname;
188 * If the source file is not stdin, make sure it exists and
189 * that it is not a directory.
192 if ( strcmp( path, STDIN ) != 0 ) {
193 if ( stat( path, &st ) < 0 ) {
197 if ( S_ISDIR( st.st_mode )) {
198 fprintf( stderr, "%s is a directory.\n", path );
204 * Open the source file and fill in the file header structure.
207 memset( &fh, 0, sizeof( fh ));
208 if ( from_open( module, path, &fh ) < 0 ) {
212 if ( flags & OPTION_HEADERONLY ) {
217 printf("name: %s\n",fh.name);
218 printf("comment: %s\n",fh.comment);
219 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
220 printf("creator: '%4s'\n", buf);
221 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
222 printf("type: '%4s'\n", buf);
223 for(i=0; i < NUMFORKS; ++i)
224 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
225 t = AD_DATE_TO_UNIX(fh.create_date);
226 printf("creation date: %s", ctime(&t));
227 t = AD_DATE_TO_UNIX(fh.mod_date);
228 printf("modification date: %s", ctime(&t));
229 t = AD_DATE_TO_UNIX(fh.backup_date);
230 printf("backup date: %s", ctime(&t));
231 return( from_close( module ));
235 * Open the target file and write out the file header info.
236 * set the header to the new filename if it has been supplied.
240 strcpy(fh.name, newname);
242 if ( to_open( module, path, &fh, flags ) < 0 ) {
243 (void)from_close( module );
248 * Read in and write out the data and resource forks.
251 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
253 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
255 if ( to_write( module, fork, bufc ) != bufc ) {
256 fprintf( stderr, "%s: Probable write error\n", path );
257 to_close( module, TRASH );
258 (void)from_close( module );
264 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
265 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
266 ntohl( fh.forklen[ fork ] ));
268 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
269 fprintf( stderr, "%s: Problem with input, dude\n", path );
270 to_close( module, TRASH );
271 (void)from_close( module );
277 * Close up the files, and get out of here.
280 if ( to_close( module, KEEP ) < 0 ) {
281 perror( "megatron:" );
282 (void)to_close( module, TRASH );
284 return( from_close( module ));
287 int main( argc, argv )
293 int converts = sizeof(name) / sizeof(char *);
296 char *progname, newname[ADEDLEN_NAME + 1];
298 progname = strrchr( argv[ 0 ], '/' );
299 if (( progname == NULL ) || ( progname == '\0' )) {
300 progname = argv[ 0 ];
304 if ( CONVERTS != converts ) {
305 fprintf( stderr, "megatron: list of program links messed up\n" );
310 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
311 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
313 if ( module == -1 ) module = ( converts - 1 );
314 if ((module == NADHEADER) || (module == BINHEADER))
315 flags |= OPTION_HEADERONLY;
318 return( megatron( STDIN, module, newname, flags ));
322 for ( c = 1 ; c < argc ; ++c ) {
323 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
324 flags |= OPTION_HEADERONLY;
327 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
328 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
331 if (strcmp(argv[c], "--stdout") == 0) {
332 flags |= OPTION_STDOUT;
336 rc = megatron( argv[ c ], module, newname, flags);