3 #endif /* HAVE_CONFIG_H */
14 #include <netatalk/endian.h>
21 char *forkname[] = { "data", "resource" };
22 static char forkbuf[8192];
23 static char *name[] = { "unhex",
33 static int from_open(int un, char *file, struct FHeader *fh, int 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 static ssize_t from_read(int un, int fork, char *buf, size_t len)
64 return( hqx_read( fork, buf, len ));
67 return( bin_read( fork, buf, len ));
70 return( nad_read( fork, buf, len ));
74 return( single_read( fork, buf, len ));
81 static int from_close(int un)
87 return( hqx_close( KEEP ));
90 return( bin_close( KEEP ));
93 return( nad_close( KEEP ));
97 return( single_close( KEEP ));
104 static int to_open(int to, char *file, struct FHeader *fh, int flags)
111 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
116 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
124 static ssize_t to_write(int to, int fork, size_t bufc)
131 return( nad_write( fork, forkbuf, bufc ));
136 return( bin_write( fork, forkbuf, bufc ));
144 static int to_close(int to, int keepflag)
151 return( nad_close( keepflag ));
156 return( bin_close( keepflag ));
164 static int megatron( char *path, int module, char *newname, int flags)
173 * If the source file is not stdin, make sure it exists and
174 * that it is not a directory.
177 if ( strcmp( path, STDIN ) != 0 ) {
178 if ( stat( path, &st ) < 0 ) {
182 if ( S_ISDIR( st.st_mode )) {
183 fprintf( stderr, "%s is a directory.\n", path );
189 * Open the source file and fill in the file header structure.
192 memset( &fh, 0, sizeof( fh ));
193 if ( from_open( module, path, &fh, flags ) < 0 ) {
197 if ( flags & OPTION_HEADERONLY ) {
202 printf("name: %s\n",fh.name);
203 printf("comment: %s\n",fh.comment);
204 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
205 printf("creator: '%4s'\n", buf);
206 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
207 printf("type: '%4s'\n", buf);
208 for(i=0; i < NUMFORKS; ++i)
209 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
210 t = AD_DATE_TO_UNIX(fh.create_date);
211 printf("creation date: %s", ctime(&t));
212 t = AD_DATE_TO_UNIX(fh.mod_date);
213 printf("modification date: %s", ctime(&t));
214 t = AD_DATE_TO_UNIX(fh.backup_date);
215 printf("backup date: %s", ctime(&t));
216 return( from_close( module ));
220 * Open the target file and write out the file header info.
221 * set the header to the new filename if it has been supplied.
225 strcpy(fh.name, newname);
227 if ( to_open( module, path, &fh, flags ) < 0 ) {
228 (void)from_close( module );
233 * Read in and write out the data and resource forks.
236 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
238 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
240 if ( to_write( module, fork, bufc ) != bufc ) {
241 fprintf( stderr, "%s: Probable write error\n", path );
242 to_close( module, TRASH );
243 (void)from_close( module );
249 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
250 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
251 ntohl( fh.forklen[ fork ] ));
253 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
254 fprintf( stderr, "%s: Problem with input, dude\n", path );
255 to_close( module, TRASH );
256 (void)from_close( module );
262 * Close up the files, and get out of here.
265 if ( to_close( module, KEEP ) < 0 ) {
266 perror( "megatron:" );
267 (void)to_close( module, TRASH );
269 return( from_close( module ));
272 int main(int argc, char **argv)
276 int converts = sizeof(name) / sizeof(char *);
279 char *progname, newname[ADEDLEN_NAME + 1];
281 progname = strrchr( argv[ 0 ], '/' );
282 if (( progname == NULL ) || ( *progname == '\0' )) {
283 progname = argv[ 0 ];
287 if ( CONVERTS != converts ) {
288 fprintf( stderr, "megatron: list of program links messed up\n" );
293 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
294 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
296 if ( module == -1 ) module = ( converts - 1 );
297 if ((module == NADHEADER) || (module == BINHEADER))
298 flags |= OPTION_HEADERONLY;
301 return( megatron( STDIN, module, newname, flags ));
305 for ( c = 1 ; c < argc ; ++c ) {
306 if ( strcmp( argv [ c ], "--version" ) == 0 ) {
307 printf("%s (Netatalk %s megatron)\n", argv[0], VERSION);
310 if ( strcmp( argv [ c ], "-v" ) == 0 ) {
311 printf("%s (Netatalk %s megatron)\n", argv[0], VERSION);
314 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
315 flags |= OPTION_HEADERONLY;
318 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
319 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
322 if (strcmp(argv[c], "--stdout") == 0) {
323 flags |= OPTION_STDOUT;
326 if (strcmp(argv[c], "--euc") == 0) {
327 flags |= OPTION_EUCJP;
330 if (strcmp(argv[c], "--sjis") == 0) {
331 flags |= OPTION_SJIS;
334 rc = megatron( argv[ c ], module, newname, flags);