]> arthur.barton.de Git - netatalk.git/blob - bin/megatron/nad.c
add a SFM compatible format for adouble
[netatalk.git] / bin / megatron / nad.c
1 /*
2  * $Id: nad.c,v 1.15 2006-09-29 09:39:16 didg Exp $
3  */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif /* HAVE_CONFIG_H */
8
9 #include <sys/types.h>
10 #include <sys/param.h>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <sys/uio.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <dirent.h>
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif /* HAVE_FCNTL_H */
22
23 #include <atalk/adouble.h>
24 #include <atalk/util.h>
25 #include <atalk/volinfo.h>
26 #include <netatalk/endian.h>
27 #include "megatron.h"
28 #include "nad.h"
29
30 struct volinfo  vol;
31 static char             hexdig[] = "0123456789abcdef";
32
33 static char mtou_buf[MAXPATHLEN + 1], utom_buf[MAXPATHLEN + 1];
34 static char *mtoupathcap( mpath )
35     char        *mpath;
36 {
37     char        *m, *u, *umax;
38     int         i = 0;
39
40     m = mpath;
41     u = mtou_buf;
42     umax = u + sizeof(mtou_buf) - 4;
43     while ( *m != '\0' && u < umax) {
44 #if AD_VERSION == AD_VERSION1
45         if ( !isascii( *m ) || *m == '/' || ( i == 0 && *m == '.' )) {
46 #else /* AD_VERSION == AD_VERSION1 */
47         if (!isprint(*m) || *m == '/' || ( i == 0 && (*m == '.' ))) {
48 #endif /* AD_VERSION == AD_VERSION1 */
49             *u++ = ':';
50             *u++ = hexdig[ ( *m & 0xf0 ) >> 4 ];
51             *u++ = hexdig[ *m & 0x0f ];
52         } else {
53 #ifdef DOWNCASE
54             *u++ = ( isupper( *m )) ? tolower( *m ) : *m;
55 #else /* DOWNCASE */
56             *u++ = *m;
57 #endif /* DOWNCASE */
58         }
59         i++;
60         m++;
61     }
62     *u = '\0';
63     return( mtou_buf );
64 }
65
66
67 #define hextoint( c )   ( isdigit( c ) ? c - '0' : c + 10 - 'a' )
68 #define islxdigit(x)    (!isupper(x)&&isxdigit(x))
69
70 static char *utompathcap( upath )
71     char        *upath;
72 {
73     char        *m, *u;
74     int h;
75
76     m = utom_buf;
77     u = upath;
78     while ( *u != '\0' ) {
79         if (*u == ':' && *(u + 1) != '\0' && islxdigit(*(u+1)) &&
80             *(u+2) != '\0' && islxdigit(*(u+2))) {
81           u++;
82           h = hextoint(*u) << 4;
83           u++;
84           h |= hextoint(*u);
85           *m = h;
86         } else {
87 #ifdef DOWNCASE
88           *m = diatolower(*u);
89 #else /* DOWNCASE */
90           *m = *u;
91 #endif /* DOWNCASE */
92         }
93         u++;
94         m++;
95     }
96     *m = '\0';
97     return( utom_buf );
98 }
99
100 static void euc2sjis( int *p1, int *p2) /* agrees w/ Samba on valid codes */
101 {
102     int row_offset, cell_offset;
103     unsigned char c1, c2;
104
105     /* first convert EUC to ISO-2022 */
106     c1 = *p1 & 0x7F;
107     c2 = *p2 & 0x7F;
108
109     /* now convert ISO-2022 to Shift-JIS */
110     row_offset = c1 < 95 ? 112 : 176;
111     cell_offset = c1 % 2 ? (c2 > 95 ? 32 : 31) : 126;
112
113     *p1 = ((c1 + 1) >> 1) + row_offset;
114     *p2 = c2 + cell_offset;
115 }
116
117 static void sjis2euc( int *p1, int *p2)  /* agrees w/ Samba on valid codes */
118 {
119     int row_offset, cell_offset, adjust;
120     unsigned char c1, c2;
121
122     c1 = *p1;
123     c2 = *p2;
124
125     /* first convert Shift-JIS to ISO-2022 */
126     adjust = c2 < 159;
127     row_offset = c1 < 160 ? 112 : 176;
128     cell_offset = adjust ? (c2 > 127 ? 32 : 31) : 126;
129
130     c1 = ((c1 - row_offset) << 1) - adjust;
131     c2 -= cell_offset;
132
133     /* now convert ISO-2022 to EUC */
134     *p1 = c1 | 0x80;
135     *p2 = c2 | 0x80;
136 }
137
138 static char *mtoupatheuc( char *from)
139 {
140     unsigned char *in, *out, *maxout;
141     int p, p2, i = 0;
142
143     in = (unsigned char *) from;
144     out = (unsigned char *) mtou_buf;
145
146     if( *in ) {
147         maxout = out + sizeof( mtou_buf) - 3;
148
149         while( out < maxout ) {
150             p = *in++;
151
152             if( ((0x81 <= p) && (p <= 0x9F))
153              || ((0xE0 <= p) && (p <= 0xEF)) ) {
154                 /* JIS X 0208 */
155                 p2 = *in++;
156                 if( ((0x40 <= p2) && (p2 <= 0x7E))
157                  || ((0x80 <= p2) && (p2 <= 0xFC)) )
158                     sjis2euc( &p, &p2);
159                 *out++ = p;
160                 p = p2;
161
162             } else if( (0xA1 <= p) && (p <= 0xDF) ) {
163                 *out++ = 0x8E;  /* halfwidth katakana */
164             } else if( p < 0x80 ) {
165 #ifdef DOWNCASE
166                 p = ( isupper( p )) ? tolower( p ) : p;
167 #endif /* DOWNCASE */
168             }
169             if( ( p == '/') || ( i == 0 && p == '.' ) ) {
170                 *out++ = ':';
171                 *out++ = hexdig[ ( p & 0xf0 ) >> 4 ];
172                 p = hexdig[ p & 0x0f ];
173             }
174             i++;
175             *out++ = p;
176             if( p )
177                 continue;
178             break;
179         }
180     } else {
181         *out++ = '.';
182         *out = 0;
183     }
184
185     return mtou_buf;
186 }
187
188 static char *utompatheuc( char *from)
189 {
190     unsigned char *in, *out, *maxout;
191     int p, p2;
192
193     in = (unsigned char *) from;
194     out = (unsigned char *) utom_buf;
195     maxout = out + sizeof( utom_buf) - 3;
196
197     while( out < maxout ) {
198         p = *in++;
199
200         if( (0xA1 <= p) && (p <= 0xFE) ) {      /* JIS X 0208 */
201             p2 = *in++;
202             if( (0xA1 <= p2) && (p2 <= 0xFE) )
203                 euc2sjis( &p, &p2);
204             *out++ = p;
205             p = p2;
206         } else if( p == 0x8E ) {                /* halfwidth katakana */
207             p = *in++;
208         } else if( p < 0x80 ) {
209 #ifdef DOWNCASE
210             p = ( isupper( p )) ? tolower( p ) : p;
211 #endif /* DOWNCASE */
212         }
213         if ( p == ':' && *(in) != '\0' && islxdigit( *(in)) &&
214                 *(in+1) != '\0' && islxdigit( *(in+1))) {
215            p = hextoint( *in ) << 4;
216            in++;
217            p |= hextoint( *in );
218            in++;
219         }
220         *out++ = p;
221         if( p )
222             continue;
223         break;
224     }
225
226     return utom_buf;
227 }
228
229 static char *mtoupathsjis( char *from)
230 {
231     unsigned char *in, *out, *maxout;
232     int p, p2, i = 0;
233
234     in = (unsigned char *) from;
235     out = (unsigned char *) mtou_buf;
236
237     if( *in ) {
238         maxout = out + sizeof( mtou_buf) - 3;
239
240         while( out < maxout ) {
241             p = *in++;
242
243             if( ((0x81 <= p) && (p <= 0x9F))
244              || ((0xE0 <= p) && (p <= 0xEF)) ) {
245                 /* JIS X 0208 */
246                 p2 = *in++;
247                 *out++ = p;
248                 p = p2;
249
250             } else if( (0xA1 <= p) && (p <= 0xDF) ) {
251                 ;       /* halfwidth katakana */
252             } else if(p < 0x80 ) {
253 #ifdef DOWNCASE
254                 p = ( isupper( p )) ? tolower( p ) : p;
255 #endif /* DOWNCASE */
256             }
257             if( ( p == '/') || ( i == 0 && p == '.' ) ) {
258                 *out++ = ':';
259                 *out++ = hexdig[ ( p & 0xf0 ) >> 4 ];
260                 p = hexdig[ p & 0x0f ];
261             }
262             i++;
263             *out++ = p;
264             if( p )
265                 continue;
266             break;
267         }
268     } else {
269         *out++ = '.';
270         *out = 0;
271     }
272
273     return mtou_buf;
274 }
275
276 static char *utompathsjis( char *from)
277 {
278     unsigned char *in, *out, *maxout;
279     int p, p2;
280
281     in = (unsigned char *) from;
282     out = (unsigned char *) utom_buf;
283     maxout = out + sizeof( utom_buf) - 3;
284
285     while( out < maxout ) {
286         p = *in++;
287
288         if( (0xA1 <= p) && (p <= 0xFE) ) {      /* JIS X 0208 */
289             p2 = *in++;
290             *out++ = p;
291             p = p2;
292         } else if( p == 0x8E ) {                /* do nothing */
293             ;
294         } else if( p < 0x80 ) {
295 #ifdef DOWNCASE
296            p = ( isupper( p )) ? tolower( p ) : p;
297 #endif /* DOWNCASE */
298         }
299         if ( p == ':' && *(in) != '\0' && islxdigit( *(in)) &&
300                 *(in+1) != '\0' && islxdigit( *(in+1))) {
301            p = hextoint( *in ) << 4;
302            in++;
303            p |= hextoint( *in );
304            in++;
305         }
306         *out++ = p;
307         if( p )
308             continue;
309         break;
310     }
311
312     return utom_buf;
313  }
314
315 static char *utompathiconv(char *upath)
316 {
317     char        *m, *u;
318     u_int16_t    flags = CONV_IGNORE | CONV_UNESCAPEHEX;
319     size_t       outlen;
320     static char  mpath[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
321
322     m = mpath;
323     outlen = strlen(upath);
324
325 #if 0
326     if (vol->v_casefold & AFPVOL_UTOMUPPER)
327         flags |= CONV_TOUPPER;
328     else if (vol->v_casefold & AFPVOL_UTOMLOWER)
329         flags |= CONV_TOLOWER;
330 #endif
331
332     u = upath;
333
334     /* convert charsets */
335     if ((size_t)-1 == ( outlen = convert_charset ( vol.v_volcharset, vol.v_maccharset, vol.v_maccharset, u, outlen, mpath, MAXPATHLEN, &flags)) ) {
336         fprintf( stderr, "Conversion from %s to %s for %s failed.", vol.v_volcodepage, vol.v_maccodepage, u);
337         goto utompath_error;
338     }
339
340     if (flags & CONV_REQMANGLE) 
341         goto utompath_error;
342
343     return(m);
344
345 utompath_error:
346     return(utompathcap( upath ));
347 }
348
349 char *mtoupathiconv(char *mpath)
350 {
351     char        *m, *u;
352     size_t       inplen;
353     size_t       outlen;
354     u_int16_t    flags = 0;
355     static char  upath[MAXPATHLEN +2]; /* for convert_charset dest_len parameter +2 */
356
357     if ( *mpath == '\0' ) {
358         return( "." );
359     }
360
361     /* set conversion flags */
362     if (!(vol.v_flags & AFPVOL_NOHEX))
363         flags |= CONV_ESCAPEHEX;
364     if (!(vol.v_flags & AFPVOL_USEDOTS))
365         flags |= CONV_ESCAPEDOTS;
366
367 #if 0
368     if ((vol->v_casefold & AFPVOL_MTOUUPPER))
369         flags |= CONV_TOUPPER;
370     else if ((vol->v_casefold & AFPVOL_MTOULOWER))
371         flags |= CONV_TOLOWER;
372 #endif
373
374     m = mpath;
375     u = upath;
376
377     inplen = strlen(m);
378     outlen = MAXPATHLEN;
379
380     if ((size_t)-1 == (outlen = convert_charset ( vol.v_maccharset, vol.v_volcharset, vol.v_maccharset, m, inplen, u, outlen, &flags)) ) {
381         fprintf (stderr, "conversion from %s to %s for %s failed.", vol.v_maccodepage, vol.v_volcodepage, mpath);
382         return(mtoupathcap( upath ));
383     }
384
385     return( upath );
386 }
387
388
389  
390 char * (*_mtoupath) ( char *mpath) = mtoupathcap;
391 char * (*_utompath) ( char *upath) = utompathcap;
392
393 /* choose translators for optional character set */
394 void select_charset( int options)
395 {
396
397     if( options & OPTION_EUCJP ) {
398         _mtoupath = mtoupatheuc;
399         _utompath = utompatheuc;
400     } else if( options & OPTION_SJIS ) {
401         _mtoupath = mtoupathsjis;
402         _utompath = utompathsjis;
403     } else {
404         _mtoupath = mtoupathcap;
405         _utompath = utompathcap;
406     }
407 }
408
409
410 #if HEXOUTPUT
411     int                 hexfork[ NUMFORKS ];
412 #endif /* HEXOUTPUT */
413
414 struct nad_file_data {
415     char                macname[ MAXPATHLEN + 1 ];
416     char                adpath[ 2 ][ MAXPATHLEN + 1];
417     int                 offset[ NUMFORKS ];
418     struct adouble      ad;
419 } nad;
420
421 static void initvol(char *path)
422 {
423     if (!loadvolinfo(path, &vol)) {
424         vol_load_charsets(&vol);
425         ad_init(&nad.ad, vol.v_adouble, 0);
426         _mtoupath = mtoupathiconv;
427         _utompath = utompathiconv;
428     }
429     else
430         ad_init(&nad.ad, 0, 0);
431 }
432
433
434 int nad_open( path, openflags, fh, options )
435     char                *path;
436     int                 openflags, options;
437     struct FHeader      *fh;
438 {
439     struct stat         st;
440     int                 fork;
441
442 /*
443  * Depending upon openflags, set up nad.adpath for the open.  If it 
444  * is for write, then stat the current directory to get its mode.
445  * Open the file.  Either fill or grab the adouble information.
446  */
447     select_charset( options);
448     memset(&nad.ad, 0, sizeof(nad.ad));
449
450     if ( openflags == O_RDONLY ) {
451         initvol(path);
452         strcpy( nad.adpath[0], path );
453         strcpy( nad.adpath[1], 
454                 nad.ad.ad_ops->ad_path( nad.adpath[0], ADFLAGS_HF ));
455         for ( fork = 0 ; fork < NUMFORKS ; fork++ ) {
456             if ( stat( nad.adpath[ fork ], &st ) < 0 ) {
457                 if ( errno == ENOENT ) {
458                     fprintf( stderr, "%s is not an adouble file.\n", path );
459                 } else {
460                     perror( "stat of adouble file failed" );
461                 }
462                 return( -1 );
463             }
464         }
465
466 #if DEBUG
467     fprintf(stderr, "%s is adpath[0]\n", nad.adpath[0]);
468     fprintf(stderr, "%s is adpath[1]\n", nad.adpath[1]);
469 #endif /* DEBUG */
470         if ( ad_open( nad.adpath[ 0 ], ADFLAGS_DF|ADFLAGS_HF,
471                 openflags, (int)( st.st_mode & 0666 ), &nad.ad) < 0 ) {
472             perror( nad.adpath[ 0 ] );
473             return( -1 );
474         }
475         return( nad_header_read( fh ));
476
477     } else {
478         initvol (".");
479         strcpy( nad.macname, fh->name );
480         strcpy( nad.adpath[0], mtoupath( nad.macname ));
481         strcpy( nad.adpath[1], 
482                 nad.ad.ad_ops->ad_path( nad.adpath[0], ADFLAGS_HF ));
483 #if DEBUG
484     fprintf(stderr, "%s\n", nad.macname);
485     fprintf(stderr, "%s is adpath[0]\n", nad.adpath[0]);
486     fprintf(stderr, "%s is adpath[1]\n", nad.adpath[1]);
487 #endif /* DEBUG */
488         if ( stat( ".", &st ) < 0 ) {
489             perror( "stat of . failed" );
490             return( -1 );
491         }
492         (void)umask( 0 );
493         if ( ad_open( nad.adpath[ 0 ], ADFLAGS_DF|ADFLAGS_HF,
494                 openflags, (int)( st.st_mode & 0666 ), &nad.ad) < 0 ) {
495             perror( nad.adpath[ 0 ] );
496             return( -1 );
497         }
498         return( nad_header_write( fh ));
499     }
500 }
501
502 int nad_header_read( fh )
503     struct FHeader      *fh;
504 {
505     u_int32_t           temptime;
506     struct stat         st;
507     char                *p;
508
509 #if 0
510     memcpy( nad.macname, ad_entry( &nad.ad, ADEID_NAME ), 
511             ad_getentrylen( &nad.ad, ADEID_NAME ));
512     nad.macname[ ad_getentrylen( &nad.ad, ADEID_NAME ) ] = '\0';
513     strcpy( fh->name, nad.macname );
514 #endif
515
516     /* just in case there's nothing in macname */
517     if (*fh->name == '\0') {
518       if ( NULL == (p = strrchr(nad.adpath[DATA], '/')) )
519         p = nad.adpath[DATA];
520       else p++;
521 #if 0      
522       strcpy(fh->name, utompath(nad.adpath[DATA]));
523 #endif      
524       strcpy(fh->name, utompath(p));
525     }
526
527     if ( stat( nad.adpath[ DATA ], &st ) < 0 ) {
528         perror( "stat of datafork failed" );
529         return( -1 );
530     }
531     fh->forklen[ DATA ] = htonl( st.st_size );
532     fh->forklen[ RESOURCE ] = htonl( ad_getentrylen( &nad.ad, ADEID_RFORK ));
533     fh->comment[0] = '\0';
534
535 #if DEBUG
536     fprintf( stderr, "macname of file\t\t\t%.*s\n", strlen( fh->name ), 
537             fh->name );
538     fprintf( stderr, "size of data fork\t\t%d\n", 
539             ntohl( fh->forklen[ DATA ] ));
540     fprintf( stderr, "size of resource fork\t\t%d\n", 
541             ntohl( fh->forklen[ RESOURCE ] ));
542     fprintf( stderr, "get info comment\t\t\"%s\"\n", fh->comment );
543 #endif /* DEBUG */
544
545     ad_getdate(&nad.ad, AD_DATE_CREATE, &temptime);
546     memcpy( &fh->create_date, &temptime, sizeof( temptime ));
547     ad_getdate(&nad.ad, AD_DATE_MODIFY, &temptime);
548     memcpy( &fh->mod_date, &temptime, sizeof( temptime ));
549     ad_getdate(&nad.ad, AD_DATE_BACKUP, &temptime);
550     memcpy( &fh->backup_date, &temptime, sizeof( temptime ));
551
552 #if DEBUG
553     memcpy( &temptime, &fh->create_date, sizeof( temptime ));
554     temptime = AD_DATE_TO_UNIX(temptime);
555     fprintf( stderr, "create_date seconds\t\t%lu\n", temptime );
556     memcpy( &temptime, &fh->mod_date, sizeof( temptime ));
557     temptime = AD_DATE_TO_UNIX(temptime);
558     fprintf( stderr, "mod_date seconds\t\t%lu\n", temptime );
559     memcpy( &temptime, &fh->backup_date, sizeof( temptime ));
560     temptime = AD_DATE_TO_UNIX(temptime);
561     fprintf( stderr, "backup_date seconds\t\t%lu\n", temptime );
562     fprintf( stderr, "size of finder_info\t\t%d\n", sizeof( fh->finder_info ));
563 #endif /* DEBUG */
564
565     memcpy(&fh->finder_info.fdType,
566             ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_TYPE,
567            sizeof( fh->finder_info.fdType ));
568     memcpy(&fh->finder_info.fdCreator,
569            ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_CREATOR,
570            sizeof( fh->finder_info.fdCreator ));
571     memcpy(&fh->finder_info.fdFlags,
572            ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_FLAGS,
573            sizeof( fh->finder_info.fdFlags ));
574     memcpy(&fh->finder_info.fdLocation,
575            ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_LOC,
576            sizeof( fh->finder_info.fdLocation ));
577     memcpy(&fh->finder_info.fdFldr,
578           ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_FLDR,
579           sizeof( fh->finder_info.fdFldr ));
580     memcpy(&fh->finder_xinfo.fdScript,
581            ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_SCRIPT,
582            sizeof(fh->finder_xinfo.fdScript));
583     memcpy(&fh->finder_xinfo.fdXFlags,
584            ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_XFLAGS,
585            sizeof(fh->finder_xinfo.fdXFlags));
586
587 #if DEBUG
588     {
589         short           flags;
590         fprintf( stderr, "finder_info.fdType\t\t%.*s\n", 
591                 sizeof( fh->finder_info.fdType ), &fh->finder_info.fdType );
592         fprintf( stderr, "finder_info.fdCreator\t\t%.*s\n", 
593                 sizeof( fh->finder_info.fdCreator ),
594                 &fh->finder_info.fdCreator );
595         fprintf( stderr, "nad type and creator\t\t%.*s\n\n", 
596                 sizeof( fh->finder_info.fdType ) + 
597                 sizeof( fh->finder_info.fdCreator ), 
598                 ad_entry( &nad.ad, ADEID_FINDERI ));
599         memcpy(&flags, ad_entry( &nad.ad, ADEID_FINDERI ) + 
600                FINDERIOFF_FLAGS, sizeof( flags ));
601         fprintf( stderr, "nad.ad flags\t\t\t%x\n", flags );
602         fprintf( stderr, "fh flags\t\t\t%x\n", fh->finder_info.fdFlags );
603         fprintf(stderr, "fh script\t\t\t%x\n", fh->finder_xinfo.fdScript);
604         fprintf(stderr, "fh xflags\t\t\t%x\n", fh->finder_xinfo.fdXFlags);
605     }
606 #endif /* DEBUG */
607
608     nad.offset[ DATA ] = nad.offset[ RESOURCE ] = 0;
609
610     return( 0 );
611
612 }
613
614 int nad_header_write( fh )
615     struct FHeader      *fh;
616 {
617     u_int32_t           temptime;
618
619     ad_setentrylen( &nad.ad, ADEID_NAME, strlen( nad.macname ));
620     memcpy( ad_entry( &nad.ad, ADEID_NAME ), nad.macname, 
621             ad_getentrylen( &nad.ad, ADEID_NAME ));
622     ad_setentrylen( &nad.ad, ADEID_COMMENT, strlen( fh->comment ));
623     memcpy( ad_entry( &nad.ad, ADEID_COMMENT ), fh->comment, 
624             ad_getentrylen( &nad.ad, ADEID_COMMENT ));
625     ad_setentrylen( &nad.ad, ADEID_RFORK, ntohl( fh->forklen[ RESOURCE ] ));
626
627 #if DEBUG
628     fprintf( stderr, "ad_getentrylen\n" );
629     fprintf( stderr, "ADEID_FINDERI\t\t\t%d\n", 
630             ad_getentrylen( &nad.ad, ADEID_FINDERI ));
631     fprintf( stderr, "ADEID_RFORK\t\t\t%d\n", 
632             ad_getentrylen( &nad.ad, ADEID_RFORK ));
633     fprintf( stderr, "ADEID_NAME\t\t\t%d\n",
634             ad_getentrylen( &nad.ad, ADEID_NAME ));
635     fprintf( stderr, "ad_entry of ADEID_NAME\t\t%.*s\n",
636             ad_getentrylen( &nad.ad, ADEID_NAME ), 
637             ad_entry( &nad.ad, ADEID_NAME ));
638     fprintf( stderr, "ADEID_COMMENT\t\t\t%d\n",
639              ad_getentrylen( &nad.ad, ADEID_COMMENT ));
640 #endif /* DEBUG */
641
642     memcpy( &temptime, &fh->create_date, sizeof( temptime ));
643     ad_setdate(&nad.ad, AD_DATE_CREATE, temptime);
644     memcpy( &temptime, &fh->mod_date, sizeof( temptime ));
645     ad_setdate(&nad.ad, AD_DATE_MODIFY, temptime);
646
647 #if DEBUG
648     ad_getdate(&nad.ad, AD_DATE_CREATE, &temptime);
649     temptime = AD_DATE_TO_UNIX(temptime);
650     fprintf(stderr, "FILEIOFF_CREATE seconds\t\t%ld\n", temptime );
651     ad_getdate(&nad.ad, AD_DATE_MODIFY, &temptime);
652     temptime = AD_DATE_TO_UNIX(temptime);
653     fprintf(stderr, "FILEIOFF_MODIFY seconds\t\t%ld\n", temptime );
654 #endif /* DEBUG */
655
656     memset( ad_entry( &nad.ad, ADEID_FINDERI ), 0, ADEDLEN_FINDERI );
657     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_TYPE, 
658             &fh->finder_info.fdType, sizeof( fh->finder_info.fdType ));
659     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_CREATOR,
660            &fh->finder_info.fdCreator, sizeof( fh->finder_info.fdCreator ));
661     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_FLAGS,
662             &fh->finder_info.fdFlags, sizeof( fh->finder_info.fdFlags ));
663     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_LOC,
664             &fh->finder_info.fdLocation,sizeof( fh->finder_info.fdLocation ));
665     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_FLDR,
666             &fh->finder_info.fdFldr, sizeof( fh->finder_info.fdFldr ));
667     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_SCRIPT,
668             &fh->finder_xinfo.fdScript, sizeof( fh->finder_xinfo.fdScript ));
669     memcpy( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_XFLAGS,
670             &fh->finder_xinfo.fdXFlags, sizeof( fh->finder_xinfo.fdXFlags));
671
672
673 #if DEBUG
674     {
675         short           flags;
676         memcpy(&flags, ( ad_entry( &nad.ad, ADEID_FINDERI ) + FINDERIOFF_FLAGS),
677                 sizeof( flags ));
678         fprintf( stderr, "nad.ad flags\t\t\t%x\n", flags );
679         fprintf( stderr, "fh flags\t\t\t%x\n", fh->finder_info.fdFlags );
680         fprintf( stderr, "fh xflags\t\t\t%x\n", fh->finder_xinfo.fdXFlags );
681         fprintf( stderr, "type and creator\t\t%.*s\n\n", 
682                 sizeof( fh->finder_info.fdType ) + 
683                 sizeof( fh->finder_info.fdCreator ),
684                 ad_entry( &nad.ad, ADEID_FINDERI ));
685     }
686 #endif /* DEBUG */
687
688 #if HEXOUTPUT
689     hexfork[ DATA ] = open( "datafork", O_WRONLY|O_CREAT, 0622 );
690     hexfork[ RESOURCE ] = open( "resfork", O_WRONLY|O_CREAT, 0622 );
691 #endif /* HEXOUTPUT */
692
693     nad.offset[ DATA ] = nad.offset[ RESOURCE ] = 0;
694     ad_flush( &nad.ad );
695
696     return( 0 );
697 }
698
699 int                     forkeid[] = { ADEID_DFORK, ADEID_RFORK };
700
701 int nad_read( fork, forkbuf, bufc )
702     int                 fork;
703     char                *forkbuf;
704     int                 bufc;
705 {
706     int                 cc = 0;
707
708 #if DEBUG
709     fprintf( stderr, "Entering nad_read\n" );
710 #endif /* DEBUG */
711
712     if (( cc = ad_read( &nad.ad, forkeid[ fork ], nad.offset[ fork ], 
713             forkbuf, bufc)) < 0 )  {
714         perror( "Reading the appledouble file:" );
715         return( cc );
716     }
717     nad.offset[ fork ] += cc;
718
719 #if DEBUG
720     fprintf( stderr, "Exiting nad_read\n" );
721 #endif /* DEBUG */
722
723     return( cc );
724 }
725
726 int nad_write( fork, forkbuf, bufc )
727     int                 fork;
728     char                *forkbuf;
729     int                 bufc;
730 {
731     char                *buf_ptr;
732     int                 writelen;
733     int                 cc = 0;
734
735 #if DEBUG
736     fprintf( stderr, "Entering nad_write\n" );
737 #endif /* DEBUG */
738
739 #if HEXOUTPUT
740     write( hexfork[ fork ], forkbuf, bufc );
741 #endif /* HEXOUTPUT */
742
743     writelen = bufc;
744     buf_ptr = forkbuf;
745
746     while (( writelen > 0 ) && ( cc >= 0 )) {
747         cc =  ad_write( &nad.ad, forkeid[ fork ], nad.offset[ fork ], 
748                 0, buf_ptr, writelen);
749         nad.offset[ fork ] += cc;
750         buf_ptr += cc;
751         writelen -= cc;
752     }
753     if ( cc < 0 ) {
754         perror( "Writing the appledouble file:" );
755         return( cc );
756     }
757
758     return( bufc );
759 }
760
761 int nad_close( status )
762 int                     status;
763 {
764     int                 rv;
765     if ( status == KEEP ) {
766         if (( rv = ad_flush( &nad.ad )) < 0 ) {
767             fprintf( stderr, "nad_close rv for flush %d\n", rv );
768             return( rv );
769         }
770         if (( rv = ad_close( &nad.ad, ADFLAGS_DF|ADFLAGS_HF )) < 0 ) {
771             fprintf( stderr, "nad_close rv for close %d\n", rv );
772             return( rv );
773         }
774     } else if ( status == TRASH ) {
775         if ( unlink( nad.adpath[ 0 ] ) < 0 ) {
776             perror ( nad.adpath[ 0 ] );
777         }
778         if ( unlink( nad.adpath[ 1 ] ) < 0 ) {
779             perror ( nad.adpath[ 1 ] );
780         }
781         return( 0 );
782     } else return( -1 );
783     return( 0 );
784 }