5757 * Sorry about that. Feel free to contact me if you have problems.
5858 */
5959
60+ #ifdef __CYGWIN__
61+ #define _WIN32
62+ #endif
63+
6064#include <stdio.h> /* Userland pieces of the ANSI C standard I/O package */
6165#include <stdlib.h> /* Userland prototypes of the ANSI C std lib functions */
66+ #include <stdint.h>
6267#include <string.h> /* Userland prototypes of the string handling funcs */
6368#include <unistd.h> /* Userland prototypes of the Unix std system calls */
6469#include <fcntl.h> /* Flag value for file handling functions */
6570#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 */
6678#include <fnmatch.h>
79+ #endif /* _WIN32 */
6780#include <dirent.h>
6881#include <sys/stat.h>
6982#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__ )
7585#include <sys/sysmacros.h>
7686#endif
7787
@@ -193,7 +203,11 @@ int nodematch(char *pattern, struct filenode *node)
193203 char * start = node -> name ;
194204 /* XXX: ugly realbase is global */
195205 if (pattern [0 ] == '/' ) start = node -> realname + realbase ;
206+ #if defined(_WIN32 ) && !defined(__CYGWIN__ )
207+ return !PathMatchSpec (start , pattern );
208+ #else
196209 return fnmatch (pattern ,start ,FNM_PATHNAME |FNM_PERIOD );
210+ #endif
197211}
198212
199213int findalign (struct filenode * node )
@@ -284,7 +298,7 @@ void dumpri(struct romfh *ri, struct filenode *n, FILE *f)
284298{
285299 int len ;
286300
287- len = strlen (n -> name )+ 1 ;
301+ len = strlen (n -> name ) + 1 ;
288302 memcpy (bigbuf , ri , sizeof (* ri ));
289303 memcpy (bigbuf + 16 , n -> name , len );
290304 if (len & 15 ) {
@@ -299,13 +313,13 @@ void dumpri(struct romfh *ri, struct filenode *n, FILE *f)
299313#if 0
300314 fprintf (stderr , "RI: [at %06x] %08lx, %08lx, %08lx, %08lx [%s]\n" ,
301315 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 ),
304318 n -> name );
305319#endif
306320}
307321
308- void dumpnode (struct filenode * node , FILE * f )
322+ int dumpnode (struct filenode * node , FILE * f )
309323{
310324 struct romfh ri ;
311325 struct filenode * p ;
@@ -336,13 +350,19 @@ void dumpnode(struct filenode *node, FILE *f)
336350 ri .spec = htonl (node -> dirlist .head -> offset );
337351 }
338352 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 )) {
340356 ri .nextfh |= htonl (ROMFH_LNK );
341357 dumpri (& ri , node , f );
342358 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+ }
344362 dumpdataa (bigbuf , node -> size , f );
345- } else if (S_ISREG (node -> modes )) {
363+ }
364+ #endif
365+ else if (S_ISREG (node -> modes )) {
346366 int offset , len , fd , max , avail ;
347367 ri .nextfh |= htonl (ROMFH_REG );
348368 dumpri (& ri , node , f );
@@ -372,7 +392,9 @@ void dumpnode(struct filenode *node, FILE *f)
372392 dumpdata (bigbuf , avail , f );
373393 offset += avail ;
374394 }
375- } else if (S_ISCHR (node -> modes )) {
395+ }
396+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
397+ else if (S_ISCHR (node -> modes )) {
376398 ri .nextfh |= htonl (ROMFH_CHR );
377399 ri .spec = htonl (major (node -> devnode )<<16 |minor (node -> devnode ));
378400 dumpri (& ri , node , f );
@@ -387,16 +409,19 @@ void dumpnode(struct filenode *node, FILE *f)
387409 ri .nextfh |= htonl (ROMFH_SCK );
388410 dumpri (& ri , node , f );
389411 }
412+ #endif
390413
391414 p = node -> dirlist .head ;
392415 while (p -> next ) {
393- dumpnode (p , f );
416+ if (dumpnode (p , f )) {
417+ return 1 ;
418+ }
394419 p = p -> next ;
395420 }
421+ return 0 ;
396422}
397423
398- void dumpall (struct filenode * node , int lastoff , FILE * f )
399- {
424+ int dumpall (struct filenode * node , int lastoff , FILE * f ) {
400425 struct romfh ri ;
401426 struct filenode * p ;
402427
@@ -407,12 +432,16 @@ void dumpall(struct filenode *node, int lastoff, FILE *f)
407432 dumpri (& ri , node , f );
408433 p = node -> dirlist .head ;
409434 while (p -> next ) {
410- dumpnode (p , f );
435+ if (dumpnode (p , f )) {
436+ return 1 ;
437+ }
411438 p = p -> next ;
412439 }
413440 /* Align the whole bunch to ROMBSIZE boundary */
414441 if (lastoff & 1023 )
415442 dumpzero (1024 - (lastoff & 1023 ), f );
443+
444+ return 0 ;
416445}
417446
418447/* Node manipulating functions */
@@ -495,7 +524,11 @@ struct filenode *findnode(struct filenode *node, dev_t dev, ino_t ino)
495524 while (p -> next ) {
496525 found = findnode (p , dev , ino );
497526 if (found )
527+ #if defined(_WIN32 ) && !defined (__CYGWIN__ )
528+ return NULL ;
529+ #else
498530 return found ;
531+ #endif
499532 p = p -> next ;
500533 }
501534 return NULL ;
@@ -579,14 +612,16 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
579612 fprintf (stderr , "ignoring '%s' (lstat failed)\n" , n -> realname );
580613 freenode (n ); continue ;
581614 }
582-
615+ #if !defined( _WIN32 ) || defined( __CYGWIN__ )
583616 /* Handle special names */
584617 if ( n -> name [0 ] == '@' ) {
585618 if (S_ISLNK (sb -> st_mode )) {
586619 /* this is a link to follow at build time */
587620 n -> name = n -> name + 1 ; /* strip off the leading @ */
588621 memset (bigbuf , 0 , sizeof (bigbuf ));
589- readlink (n -> realname , bigbuf , sizeof (bigbuf ));
622+ if (readlink (n -> realname , bigbuf , sizeof (bigbuf ))) {
623+ return -1 ;
624+ }
590625 n -> realname = strdup (bigbuf );
591626
592627 if (lstat (n -> realname , sb )) {
@@ -628,13 +663,17 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
628663 }
629664 }
630665 }
666+ #endif
631667
632668 setnode (n , sb -> st_dev , sb -> st_ino , sb -> st_mode );
669+
670+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
633671 /* Skip unreadable files/dirs */
634672 if (!S_ISLNK (n -> modes ) && access (n -> realname , R_OK )) {
635673 fprintf (stderr , "ignoring '%s' (access failed)\n" , n -> realname );
636674 freenode (n ); continue ;
637675 }
676+ #endif
638677
639678 /* Look up old links */
640679 if ( strcmp (n -> name , "." ) == 0 ) {
@@ -658,9 +697,12 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
658697 n -> size = sb -> st_size ;
659698 } else
660699 curroffset = alignnode (n , curroffset , 0 );
700+
701+ #if !defined(_WIN32 ) || defined(__CYGWIN__ )
661702 if (S_ISLNK (sb -> st_mode )) {
662703 n -> size = sb -> st_size ;
663704 }
705+ #endif
664706 curroffset += spaceneeded (n );
665707 if (S_ISCHR (sb -> st_mode ) || S_ISBLK (sb -> st_mode )) {
666708 n -> devnode = sb -> st_rdev ;
@@ -674,6 +716,9 @@ int processdir(int level, const char *base, const char *dirname, struct stat *sb
674716 curroffset = processdir (level + 1 , n -> realname , dp -> d_name ,
675717 sb , n , root , curroffset );
676718 }
719+
720+ if (curroffset < 0 )
721+ return -1 ;
677722 }
678723 }
679724 if (dirfd ) closedir (dirfd );
@@ -780,7 +825,7 @@ int main(int argc, char *argv[])
780825 }
781826
782827 if (!volname ) {
783- sprintf (buf , "rom %08lx" , time (NULL ));
828+ sprintf (buf , "rom %" PRId64 , ( int64_t ) time (NULL ));
784829 volname = buf ;
785830 }
786831 if (!outf ) {
@@ -802,9 +847,16 @@ int main(int argc, char *argv[])
802847 root = newnode (dir , volname , 0 );
803848 root -> parent = root ;
804849 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+ }
805854 if (verbose )
806855 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+ }
808860
809- exit ( 0 ) ;
861+ return 0 ;
810862}
0 commit comments