57
57
* Sorry about that. Feel free to contact me if you have problems.
58
58
*/
59
59
60
+ #ifdef __CYGWIN__
61
+ #define _WIN32
62
+ #endif
63
+
60
64
#include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */
61
65
#include <stdlib.h> /* Userland prototypes of the ANSI C std lib functions */
66
+ #include <stdint.h>
62
67
#include <string.h> /* Userland prototypes of the string handling funcs */
63
68
#include <unistd.h> /* Userland prototypes of the Unix std system calls */
64
69
#include <fcntl.h> /* Flag value for file handling functions */
65
70
#include <time.h>
71
+ #if defined (_WIN32 ) && !defined(__CYGWIN__ )
72
+ #include <getopt.h>
73
+ #include <winsock2.h>
74
+ #include <shlwapi.h>
75
+ #define lstat stat
76
+ #else
77
+ #include <netinet/in.h> /* Consts & structs defined by the internet system */
66
78
#include <fnmatch.h>
79
+ #endif /* _WIN32 */
67
80
#include <dirent.h>
68
81
#include <sys/stat.h>
69
82
#include <sys/types.h>
70
-
71
- #include <netinet/in.h> /* Consts & structs defined by the internet system */
72
-
73
- /* good old times without autoconf... */
74
- #if defined(__linux__ ) || defined(__sun__ ) || defined(__CYGWIN__ )
83
+ #include <inttypes.h>
84
+ #if defined(__linux__ ) || defined(__sun__ )
75
85
#include <sys/sysmacros.h>
76
86
#endif
77
87
@@ -193,7 +203,11 @@ int nodematch(char *pattern, struct filenode *node)
193
203
char * start = node -> name ;
194
204
/* XXX: ugly realbase is global */
195
205
if (pattern [0 ] == '/' ) start = node -> realname + realbase ;
206
+ #if defined(_WIN32 ) && !defined(__CYGWIN__ )
207
+ return !PathMatchSpec (start , pattern );
208
+ #else
196
209
return fnmatch (pattern ,start ,FNM_PATHNAME |FNM_PERIOD );
210
+ #endif
197
211
}
198
212
199
213
int findalign (struct filenode * node )
@@ -284,7 +298,7 @@ void dumpri(struct romfh *ri, struct filenode *n, FILE *f)
284
298
{
285
299
int len ;
286
300
287
- len = strlen (n -> name )+ 1 ;
301
+ len = strlen (n -> name ) + 1 ;
288
302
memcpy (bigbuf , ri , sizeof (* ri ));
289
303
memcpy (bigbuf + 16 , n -> name , len );
290
304
if (len & 15 ) {
@@ -299,13 +313,13 @@ void dumpri(struct romfh *ri, struct filenode *n, FILE *f)
299
313
#if 0
300
314
fprintf (stderr , "RI: [at %06x] %08lx, %08lx, %08lx, %08lx [%s]\n" ,
301
315
n -> offset ,
302
- ntohl (ri -> nextfh ), ntohl (ri -> spec ),
303
- ntohl (ri -> size ), ntohl (ri -> checksum ),
316
+ ( long unsigned int ) ntohl (ri -> nextfh ), ( long unsigned int ) ntohl (ri -> spec ),
317
+ ( long unsigned int ) ntohl (ri -> size ), ( long unsigned int ) ntohl (ri -> checksum ),
304
318
n -> name );
305
319
#endif
306
320
}
307
321
308
- void dumpnode (struct filenode * node , FILE * f )
322
+ int dumpnode (struct filenode * node , FILE * f )
309
323
{
310
324
struct romfh ri ;
311
325
struct filenode * p ;
@@ -336,13 +350,19 @@ void dumpnode(struct filenode *node, FILE *f)
336
350
ri .spec = htonl (node -> dirlist .head -> offset );
337
351
}
338
352
dumpri (& ri , node , f );
339
- } else if (S_ISLNK (node -> modes )) {
353
+ }
354
+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
355
+ else if (S_ISLNK (node -> modes )) {
340
356
ri .nextfh |= htonl (ROMFH_LNK );
341
357
dumpri (& ri , node , f );
342
358
memset (bigbuf , 0 , sizeof (bigbuf ));
343
- readlink (node -> realname , bigbuf , node -> size );
359
+ if (readlink (node -> realname , bigbuf , node -> size ) < 0 ) {
360
+ return 1 ;
361
+ }
344
362
dumpdataa (bigbuf , node -> size , f );
345
- } else if (S_ISREG (node -> modes )) {
363
+ }
364
+ #endif
365
+ else if (S_ISREG (node -> modes )) {
346
366
int offset , len , fd , max , avail ;
347
367
ri .nextfh |= htonl (ROMFH_REG );
348
368
dumpri (& ri , node , f );
@@ -372,7 +392,9 @@ void dumpnode(struct filenode *node, FILE *f)
372
392
dumpdata (bigbuf , avail , f );
373
393
offset += avail ;
374
394
}
375
- } else if (S_ISCHR (node -> modes )) {
395
+ }
396
+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
397
+ else if (S_ISCHR (node -> modes )) {
376
398
ri .nextfh |= htonl (ROMFH_CHR );
377
399
ri .spec = htonl (major (node -> devnode )<<16 |minor (node -> devnode ));
378
400
dumpri (& ri , node , f );
@@ -387,16 +409,19 @@ void dumpnode(struct filenode *node, FILE *f)
387
409
ri .nextfh |= htonl (ROMFH_SCK );
388
410
dumpri (& ri , node , f );
389
411
}
412
+ #endif
390
413
391
414
p = node -> dirlist .head ;
392
415
while (p -> next ) {
393
- dumpnode (p , f );
416
+ if (dumpnode (p , f )) {
417
+ return 1 ;
418
+ }
394
419
p = p -> next ;
395
420
}
421
+ return 0 ;
396
422
}
397
423
398
- void dumpall (struct filenode * node , int lastoff , FILE * f )
399
- {
424
+ int dumpall (struct filenode * node , int lastoff , FILE * f ) {
400
425
struct romfh ri ;
401
426
struct filenode * p ;
402
427
@@ -407,12 +432,16 @@ void dumpall(struct filenode *node, int lastoff, FILE *f)
407
432
dumpri (& ri , node , f );
408
433
p = node -> dirlist .head ;
409
434
while (p -> next ) {
410
- dumpnode (p , f );
435
+ if (dumpnode (p , f )) {
436
+ return 1 ;
437
+ }
411
438
p = p -> next ;
412
439
}
413
440
/* Align the whole bunch to ROMBSIZE boundary */
414
441
if (lastoff & 1023 )
415
442
dumpzero (1024 - (lastoff & 1023 ), f );
443
+
444
+ return 0 ;
416
445
}
417
446
418
447
/* Node manipulating functions */
@@ -495,7 +524,11 @@ struct filenode *findnode(struct filenode *node, dev_t dev, ino_t ino)
495
524
while (p -> next ) {
496
525
found = findnode (p , dev , ino );
497
526
if (found )
527
+ #if defined(_WIN32 ) && !defined (__CYGWIN__ )
528
+ return NULL ;
529
+ #else
498
530
return found ;
531
+ #endif
499
532
p = p -> next ;
500
533
}
501
534
return NULL ;
@@ -579,14 +612,16 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
579
612
fprintf (stderr , "ignoring '%s' (lstat failed)\n" , n -> realname );
580
613
freenode (n ); continue ;
581
614
}
582
-
615
+ #if !defined( _WIN32 ) || defined( __CYGWIN__ )
583
616
/* Handle special names */
584
617
if ( n -> name [0 ] == '@' ) {
585
618
if (S_ISLNK (sb -> st_mode )) {
586
619
/* this is a link to follow at build time */
587
620
n -> name = n -> name + 1 ; /* strip off the leading @ */
588
621
memset (bigbuf , 0 , sizeof (bigbuf ));
589
- readlink (n -> realname , bigbuf , sizeof (bigbuf ));
622
+ if (readlink (n -> realname , bigbuf , sizeof (bigbuf ))) {
623
+ return -1 ;
624
+ }
590
625
n -> realname = strdup (bigbuf );
591
626
592
627
if (lstat (n -> realname , sb )) {
@@ -628,13 +663,17 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
628
663
}
629
664
}
630
665
}
666
+ #endif
631
667
632
668
setnode (n , sb -> st_dev , sb -> st_ino , sb -> st_mode );
669
+
670
+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
633
671
/* Skip unreadable files/dirs */
634
672
if (!S_ISLNK (n -> modes ) && access (n -> realname , R_OK )) {
635
673
fprintf (stderr , "ignoring '%s' (access failed)\n" , n -> realname );
636
674
freenode (n ); continue ;
637
675
}
676
+ #endif
638
677
639
678
/* Look up old links */
640
679
if ( strcmp (n -> name , "." ) == 0 ) {
@@ -658,9 +697,12 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
658
697
n -> size = sb -> st_size ;
659
698
} else
660
699
curroffset = alignnode (n , curroffset , 0 );
700
+
701
+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
661
702
if (S_ISLNK (sb -> st_mode )) {
662
703
n -> size = sb -> st_size ;
663
704
}
705
+ #endif
664
706
curroffset += spaceneeded (n );
665
707
if (S_ISCHR (sb -> st_mode ) || S_ISBLK (sb -> st_mode )) {
666
708
n -> devnode = sb -> st_rdev ;
@@ -674,6 +716,9 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
674
716
curroffset = processdir (level + 1 , n -> realname , dp -> d_name ,
675
717
sb , n , root , curroffset );
676
718
}
719
+
720
+ if (curroffset < 0 )
721
+ return -1 ;
677
722
}
678
723
}
679
724
if (dirfd ) closedir (dirfd );
@@ -780,7 +825,7 @@ int main(int argc, char *argv[])
780
825
}
781
826
782
827
if (!volname ) {
783
- sprintf (buf , "rom %08lx" , time (NULL ));
828
+ sprintf (buf , "rom %" PRId64 , ( int64_t ) time (NULL ));
784
829
volname = buf ;
785
830
}
786
831
if (!outf ) {
@@ -802,9 +847,16 @@ int main(int argc, char *argv[])
802
847
root = newnode (dir , volname , 0 );
803
848
root -> parent = root ;
804
849
lastoff = processdir (1 , dir , dir , & sb , root , root , spaceneeded (root ));
850
+ if (lastoff < 0 ) {
851
+ fprintf (stderr , "Error while processing directory.\n" );
852
+ return 1 ;
853
+ }
805
854
if (verbose )
806
855
shownode (0 , root , stderr );
807
- dumpall (root , lastoff , f );
856
+ if (dumpall (root , lastoff , f )) {
857
+ fprintf (stderr , "Error while dumping!\n" );
858
+ return 1 ;
859
+ }
808
860
809
- exit ( 0 ) ;
861
+ return 0 ;
810
862
}
0 commit comments