2 * $Id: megatron.c,v 1.9 2002-04-29 01:52:49 morgana Exp $
7 #endif /* HAVE_CONFIG_H */
10 #include <sys/param.h>
15 #endif /* HAVE_FCNTL_H */
20 #include <netatalk/endian.h>
28 char *forkname[] = { "data", "resource" };
29 char *name[] = { "unhex",
39 int from_open( un, file, fh, flags )
48 return( hqx_open( file, O_RDONLY, fh, flags ));
52 return( bin_open( file, O_RDONLY, fh, flags ));
56 return( nad_open( file, O_RDONLY, fh, flags ));
60 return( single_open( file, O_RDONLY, fh, flags ));
67 int from_read( un, fork, buf, len )
77 return( hqx_read( fork, buf, len ));
80 return( bin_read( fork, buf, len ));
83 return( nad_read( fork, buf, len ));
87 return( single_read( fork, buf, len ));
101 return( hqx_close( KEEP ));
104 return( bin_close( KEEP ));
107 return( nad_close( KEEP ));
111 return( single_close( KEEP ));
118 int to_open( to, file, fh, flags )
128 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
133 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
141 int to_write( to, fork, bufc )
151 return( nad_write( fork, forkbuf, bufc ));
156 return( bin_write( fork, forkbuf, bufc ));
164 int to_close( to, keepflag )
173 return( nad_close( keepflag ));
178 return( bin_close( keepflag ));
186 int megatron( path, module, newname, flags )
187 char *path, *newname;
197 * If the source file is not stdin, make sure it exists and
198 * that it is not a directory.
201 if ( strcmp( path, STDIN ) != 0 ) {
202 if ( stat( path, &st ) < 0 ) {
206 if ( S_ISDIR( st.st_mode )) {
207 fprintf( stderr, "%s is a directory.\n", path );
213 * Open the source file and fill in the file header structure.
216 memset( &fh, 0, sizeof( fh ));
217 if ( from_open( module, path, &fh, flags ) < 0 ) {
221 if ( flags & OPTION_HEADERONLY ) {
226 printf("name: %s\n",fh.name);
227 printf("comment: %s\n",fh.comment);
228 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
229 printf("creator: '%4s'\n", buf);
230 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
231 printf("type: '%4s'\n", buf);
232 for(i=0; i < NUMFORKS; ++i)
233 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
234 t = AD_DATE_TO_UNIX(fh.create_date);
235 printf("creation date: %s", ctime(&t));
236 t = AD_DATE_TO_UNIX(fh.mod_date);
237 printf("modification date: %s", ctime(&t));
238 t = AD_DATE_TO_UNIX(fh.backup_date);
239 printf("backup date: %s", ctime(&t));
240 return( from_close( module ));
244 * Open the target file and write out the file header info.
245 * set the header to the new filename if it has been supplied.
249 strcpy(fh.name, newname);
251 if ( to_open( module, path, &fh, flags ) < 0 ) {
252 (void)from_close( module );
257 * Read in and write out the data and resource forks.
260 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
262 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
264 if ( to_write( module, fork, bufc ) != bufc ) {
265 fprintf( stderr, "%s: Probable write error\n", path );
266 to_close( module, TRASH );
267 (void)from_close( module );
273 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
274 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
275 ntohl( fh.forklen[ fork ] ));
277 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
278 fprintf( stderr, "%s: Problem with input, dude\n", path );
279 to_close( module, TRASH );
280 (void)from_close( module );
286 * Close up the files, and get out of here.
289 if ( to_close( module, KEEP ) < 0 ) {
290 perror( "megatron:" );
291 (void)to_close( module, TRASH );
293 return( from_close( module ));
296 int main( argc, argv )
302 int converts = sizeof(name) / sizeof(char *);
305 char *progname, newname[ADEDLEN_NAME + 1];
307 progname = strrchr( argv[ 0 ], '/' );
308 if (( progname == NULL ) || ( progname == '\0' )) {
309 progname = argv[ 0 ];
313 if ( CONVERTS != converts ) {
314 fprintf( stderr, "megatron: list of program links messed up\n" );
319 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
320 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
322 if ( module == -1 ) module = ( converts - 1 );
323 if ((module == NADHEADER) || (module == BINHEADER))
324 flags |= OPTION_HEADERONLY;
327 return( megatron( STDIN, module, newname, flags ));
331 for ( c = 1 ; c < argc ; ++c ) {
332 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
333 flags |= OPTION_HEADERONLY;
336 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
337 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
340 if (strcmp(argv[c], "--stdout") == 0) {
341 flags |= OPTION_STDOUT;
344 if (strcmp(argv[c], "--euc") == 0) {
345 flags |= OPTION_EUCJP;
348 if (strcmp(argv[c], "--sjis") == 0) {
349 flags |= OPTION_SJIS;
352 rc = megatron( argv[ c ], module, newname, flags);