2 * $Id: megatron.c,v 1.13 2009-10-14 02:24:04 didg Exp $
7 #endif /* HAVE_CONFIG_H */
10 #include <sys/param.h>
15 #endif /* HAVE_FCNTL_H */
20 #include <netatalk/endian.h>
27 char *forkname[] = { "data", "resource" };
28 static char forkbuf[8192];
29 static char *name[] = { "unhex",
39 static int from_open(int un, char *file, struct FHeader *fh, int flags)
45 return( hqx_open( file, O_RDONLY, fh, flags ));
49 return( bin_open( file, O_RDONLY, fh, flags ));
53 return( nad_open( file, O_RDONLY, fh, flags ));
57 return( single_open( file, O_RDONLY, fh, flags ));
64 static int from_read(int un, int fork, char *buf, int len)
70 return( hqx_read( fork, buf, len ));
73 return( bin_read( fork, buf, len ));
76 return( nad_read( fork, buf, len ));
80 return( single_read( fork, buf, len ));
87 static int from_close(int un)
93 return( hqx_close( KEEP ));
96 return( bin_close( KEEP ));
99 return( nad_close( KEEP ));
103 return( single_close( KEEP ));
110 static int to_open(int to, char *file, struct FHeader *fh, int flags)
117 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
122 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
130 static int to_write(int to, int fork, int bufc)
137 return( nad_write( fork, forkbuf, bufc ));
142 return( bin_write( fork, forkbuf, bufc ));
150 static int to_close(int to, int keepflag)
157 return( nad_close( keepflag ));
162 return( bin_close( keepflag ));
170 static int megatron( char *path, int module, char *newname, int flags)
176 unsigned int forkred;
179 * If the source file is not stdin, make sure it exists and
180 * that it is not a directory.
183 if ( strcmp( path, STDIN ) != 0 ) {
184 if ( stat( path, &st ) < 0 ) {
188 if ( S_ISDIR( st.st_mode )) {
189 fprintf( stderr, "%s is a directory.\n", path );
195 * Open the source file and fill in the file header structure.
198 memset( &fh, 0, sizeof( fh ));
199 if ( from_open( module, path, &fh, flags ) < 0 ) {
203 if ( flags & OPTION_HEADERONLY ) {
208 printf("name: %s\n",fh.name);
209 printf("comment: %s\n",fh.comment);
210 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
211 printf("creator: '%4s'\n", buf);
212 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
213 printf("type: '%4s'\n", buf);
214 for(i=0; i < NUMFORKS; ++i)
215 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
216 t = AD_DATE_TO_UNIX(fh.create_date);
217 printf("creation date: %s", ctime(&t));
218 t = AD_DATE_TO_UNIX(fh.mod_date);
219 printf("modification date: %s", ctime(&t));
220 t = AD_DATE_TO_UNIX(fh.backup_date);
221 printf("backup date: %s", ctime(&t));
222 return( from_close( module ));
226 * Open the target file and write out the file header info.
227 * set the header to the new filename if it has been supplied.
231 strcpy(fh.name, newname);
233 if ( to_open( module, path, &fh, flags ) < 0 ) {
234 (void)from_close( module );
239 * Read in and write out the data and resource forks.
242 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
244 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
246 if ( to_write( module, fork, bufc ) != bufc ) {
247 fprintf( stderr, "%s: Probable write error\n", path );
248 to_close( module, TRASH );
249 (void)from_close( module );
255 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
256 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
257 ntohl( fh.forklen[ fork ] ));
259 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
260 fprintf( stderr, "%s: Problem with input, dude\n", path );
261 to_close( module, TRASH );
262 (void)from_close( module );
268 * Close up the files, and get out of here.
271 if ( to_close( module, KEEP ) < 0 ) {
272 perror( "megatron:" );
273 (void)to_close( module, TRASH );
275 return( from_close( module ));
278 int main(int argc, char **argv)
282 int converts = sizeof(name) / sizeof(char *);
285 char *progname, newname[ADEDLEN_NAME + 1];
287 progname = strrchr( argv[ 0 ], '/' );
288 if (( progname == NULL ) || ( *progname == '\0' )) {
289 progname = argv[ 0 ];
293 if ( CONVERTS != converts ) {
294 fprintf( stderr, "megatron: list of program links messed up\n" );
299 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
300 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
302 if ( module == -1 ) module = ( converts - 1 );
303 if ((module == NADHEADER) || (module == BINHEADER))
304 flags |= OPTION_HEADERONLY;
307 return( megatron( STDIN, module, newname, flags ));
311 for ( c = 1 ; c < argc ; ++c ) {
312 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
313 flags |= OPTION_HEADERONLY;
316 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
317 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
320 if (strcmp(argv[c], "--stdout") == 0) {
321 flags |= OPTION_STDOUT;
324 if (strcmp(argv[c], "--euc") == 0) {
325 flags |= OPTION_EUCJP;
328 if (strcmp(argv[c], "--sjis") == 0) {
329 flags |= OPTION_SJIS;
332 rc = megatron( argv[ c ], module, newname, flags);