3 #endif /* HAVE_CONFIG_H */
11 #endif /* HAVE_FCNTL_H */
16 #include <netatalk/endian.h>
23 char *forkname[] = { "data", "resource" };
24 static char forkbuf[8192];
25 static char *name[] = { "unhex",
35 static int from_open(int un, char *file, struct FHeader *fh, int flags)
41 return( hqx_open( file, O_RDONLY, fh, flags ));
45 return( bin_open( file, O_RDONLY, fh, flags ));
49 return( nad_open( file, O_RDONLY, fh, flags ));
53 return( single_open( file, O_RDONLY, fh, flags ));
60 static ssize_t from_read(int un, int fork, char *buf, size_t len)
66 return( hqx_read( fork, buf, len ));
69 return( bin_read( fork, buf, len ));
72 return( nad_read( fork, buf, len ));
76 return( single_read( fork, buf, len ));
83 static int from_close(int un)
89 return( hqx_close( KEEP ));
92 return( bin_close( KEEP ));
95 return( nad_close( KEEP ));
99 return( single_close( KEEP ));
106 static int to_open(int to, char *file, struct FHeader *fh, int flags)
113 return( nad_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
118 return( bin_open( file, O_RDWR|O_CREAT|O_EXCL, fh, flags ));
126 static ssize_t to_write(int to, int fork, size_t bufc)
133 return( nad_write( fork, forkbuf, bufc ));
138 return( bin_write( fork, forkbuf, bufc ));
146 static int to_close(int to, int keepflag)
153 return( nad_close( keepflag ));
158 return( bin_close( keepflag ));
166 static int megatron( char *path, int module, char *newname, int flags)
175 * If the source file is not stdin, make sure it exists and
176 * that it is not a directory.
179 if ( strcmp( path, STDIN ) != 0 ) {
180 if ( stat( path, &st ) < 0 ) {
184 if ( S_ISDIR( st.st_mode )) {
185 fprintf( stderr, "%s is a directory.\n", path );
191 * Open the source file and fill in the file header structure.
194 memset( &fh, 0, sizeof( fh ));
195 if ( from_open( module, path, &fh, flags ) < 0 ) {
199 if ( flags & OPTION_HEADERONLY ) {
204 printf("name: %s\n",fh.name);
205 printf("comment: %s\n",fh.comment);
206 memcpy(&buf, &fh.finder_info.fdCreator, sizeof(u_int32_t));
207 printf("creator: '%4s'\n", buf);
208 memcpy(&buf, &fh.finder_info.fdType, sizeof(u_int32_t));
209 printf("type: '%4s'\n", buf);
210 for(i=0; i < NUMFORKS; ++i)
211 printf("fork length[%d]: %u\n", i, ntohl(fh.forklen[i]));
212 t = AD_DATE_TO_UNIX(fh.create_date);
213 printf("creation date: %s", ctime(&t));
214 t = AD_DATE_TO_UNIX(fh.mod_date);
215 printf("modification date: %s", ctime(&t));
216 t = AD_DATE_TO_UNIX(fh.backup_date);
217 printf("backup date: %s", ctime(&t));
218 return( from_close( module ));
222 * Open the target file and write out the file header info.
223 * set the header to the new filename if it has been supplied.
227 strcpy(fh.name, newname);
229 if ( to_open( module, path, &fh, flags ) < 0 ) {
230 (void)from_close( module );
235 * Read in and write out the data and resource forks.
238 for ( fork = 0; fork < NUMFORKS ; fork++ ) {
240 while(( bufc = from_read( module, fork, forkbuf, sizeof( forkbuf )))
242 if ( to_write( module, fork, bufc ) != bufc ) {
243 fprintf( stderr, "%s: Probable write error\n", path );
244 to_close( module, TRASH );
245 (void)from_close( module );
251 fprintf( stderr, "megatron: forkred is \t\t%d\n", forkred );
252 fprintf( stderr, "megatron: fh.forklen[%d] is \t%d\n", fork,
253 ntohl( fh.forklen[ fork ] ));
255 if (( bufc < 0 ) || ( forkred != ntohl( fh.forklen[ fork ] ))) {
256 fprintf( stderr, "%s: Problem with input, dude\n", path );
257 to_close( module, TRASH );
258 (void)from_close( module );
264 * Close up the files, and get out of here.
267 if ( to_close( module, KEEP ) < 0 ) {
268 perror( "megatron:" );
269 (void)to_close( module, TRASH );
271 return( from_close( module ));
274 int main(int argc, char **argv)
278 int converts = sizeof(name) / sizeof(char *);
281 char *progname, newname[ADEDLEN_NAME + 1];
283 progname = strrchr( argv[ 0 ], '/' );
284 if (( progname == NULL ) || ( *progname == '\0' )) {
285 progname = argv[ 0 ];
289 if ( CONVERTS != converts ) {
290 fprintf( stderr, "megatron: list of program links messed up\n" );
295 for ( c = 0 ; (( c < converts ) && ( module < 0 )) ; ++c ) {
296 if ( strcmp( name[ c ], progname ) == 0 ) module = c;
298 if ( module == -1 ) module = ( converts - 1 );
299 if ((module == NADHEADER) || (module == BINHEADER))
300 flags |= OPTION_HEADERONLY;
303 return( megatron( STDIN, module, newname, flags ));
307 for ( c = 1 ; c < argc ; ++c ) {
308 if ( strcmp( argv [ c ], "--version" ) == 0 ) {
309 printf("%s (Netatalk %s megatron)\n", argv[0], VERSION);
312 if ( strcmp( argv [ c ], "-v" ) == 0 ) {
313 printf("%s (Netatalk %s megatron)\n", argv[0], VERSION);
316 if ( strcmp( argv [ c ], "--header" ) == 0 ) {
317 flags |= OPTION_HEADERONLY;
320 if ( strcmp( argv [ c ], "--filename" ) == 0 ) {
321 if(++c < argc) strncpy(newname,argv[c], ADEDLEN_NAME);
324 if (strcmp(argv[c], "--stdout") == 0) {
325 flags |= OPTION_STDOUT;
328 if (strcmp(argv[c], "--euc") == 0) {
329 flags |= OPTION_EUCJP;
332 if (strcmp(argv[c], "--sjis") == 0) {
333 flags |= OPTION_SJIS;
336 rc = megatron( argv[ c ], module, newname, flags);