2 * $Id: megatron.c,v 1.7 2002-01-04 04:45:47 sibaz Exp $
7 #endif /* HAVE_CONFIG_H */
10 #include <sys/param.h>
15 #endif /* HAVE_FCNTL_H */
20 #include <atalk/logger.h>
21 #include <netatalk/endian.h>
29 char *forkname[] = { "data", "resource" };
30 char *name[] = { "unhex",
40 int from_open( un, file, fh, flags )
49 return( hqx_open( file, O_RDONLY, fh, flags ));
53 return( bin_open( file, O_RDONLY, fh, flags ));
57 return( nad_open( file, O_RDONLY, fh, flags ));
61 return( single_open( file, O_RDONLY, fh, flags ));
68 int from_read( un, fork, buf, len )
78 return( hqx_read( fork, buf, len ));
81 return( bin_read( fork, buf, len ));
84 return( nad_read( fork, buf, len ));
88 return( single_read( fork, buf, len ));
102 return( hqx_close( KEEP ));
105 return( bin_close( KEEP ));
108 return( nad_close( KEEP ));
112 return( single_close( KEEP ));
119 int to_open( to, file, fh, flags )
129 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
134 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
142 int to_write( to, fork, bufc )
152 return( nad_write( fork, forkbuf, bufc ));
157 return( bin_write( fork, forkbuf, bufc ));
165 int to_close( to, keepflag )
174 return( nad_close( keepflag ));
179 return( bin_close( keepflag ));
187 int megatron( path, module, newname, flags )
188 char *path, *newname;
198 * If the source file is not stdin, make sure it exists and
199 * that it is not a directory.
202 if ( strcmp( path, STDIN ) != 0 ) {
203 if ( stat( path, &st ) < 0 ) {
207 if ( S_ISDIR( st.st_mode )) {
208 fprintf( stderr, "%s is a directory.\n", path );
214 * Open the source file and fill in the file header structure.
217 memset( &fh, 0, sizeof( fh ));
218 if ( from_open( module, path, &fh ) < 0 ) {
222 if ( flags & OPTION_HEADERONLY ) {
227 printf("name: %s\n",fh.name);
228 printf("comment: %s\n",fh.comment);
229 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
230 printf("creator: '%4s'\n", buf);
231 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
232 printf("type: '%4s'\n", buf);
233 for(i=0; i < NUMFORKS; ++i)
234 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
235 t = AD_DATE_TO_UNIX(fh.create_date);
236 printf("creation date: %s", ctime(&t));
237 t = AD_DATE_TO_UNIX(fh.mod_date);
238 printf("modification date: %s", ctime(&t));
239 t = AD_DATE_TO_UNIX(fh.backup_date);
240 printf("backup date: %s", ctime(&t));
241 return( from_close( module ));
245 * Open the target file and write out the file header info.
246 * set the header to the new filename if it has been supplied.
250 strcpy(fh.name, newname);
252 if ( to_open( module, path, &fh, flags ) < 0 ) {
253 (void)from_close( module );
258 * Read in and write out the data and resource forks.
261 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
263 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
265 if ( to_write( module, fork, bufc ) != bufc ) {
266 fprintf( stderr, "%s: Probable write error\n", path );
267 to_close( module, TRASH );
268 (void)from_close( module );
274 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
275 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
276 ntohl( fh.forklen[ fork ] ));
278 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
279 fprintf( stderr, "%s: Problem with input, dude\n", path );
280 to_close( module, TRASH );
281 (void)from_close( module );
287 * Close up the files, and get out of here.
290 if ( to_close( module, KEEP ) < 0 ) {
291 perror( "megatron:" );
292 (void)to_close( module, TRASH );
294 return( from_close( module ));
297 int main( argc, argv )
303 int converts = sizeof(name) / sizeof(char *);
306 char *progname, newname[ADEDLEN_NAME + 1];
308 progname = strrchr( argv[ 0 ], '/' );
309 if (( progname == NULL ) || ( progname == '\0' )) {
310 progname = argv[ 0 ];
314 if ( CONVERTS != converts ) {
315 fprintf( stderr, "megatron: list of program links messed up\n" );
320 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
321 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
323 if ( module == -1 ) module = ( converts - 1 );
324 if ((module == NADHEADER) || (module == BINHEADER))
325 flags |= OPTION_HEADERONLY;
328 return( megatron( STDIN, module, newname, flags ));
332 for ( c = 1 ; c < argc ; ++c ) {
333 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
334 flags |= OPTION_HEADERONLY;
337 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
338 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
341 if (strcmp(argv[c], "--stdout") == 0) {
342 flags |= OPTION_STDOUT;
346 rc = megatron( argv[ c ], module, newname, flags);