@@ -72,8 +72,25 @@ int binentry_address_compare(const void* a, const void* b)
7272 }
7373}
7474
75+ #ifdef _WIN32
76+ /* 'strerror' has been explicitly marked deprecated */
77+ static const char * win_strerror (int err , char * buf , size_t bufsz ) {
78+ if (strerror_s (buf , bufsz , err ) != 0 ) {
79+ buf [0 ] = '\0' ;
80+ }
81+ return buf ;
82+ }
83+ #endif
84+
7585int main (int argc , const char * argv []) {
86+ #ifdef _WIN32
87+ char errbuf [128 ] = { 0 };
88+ errno_t fe ;
89+ #endif
7690 const char * outname = NULL ;
91+ FILE * fo = NULL ;
92+ FILE * fi = NULL ;
93+
7794 size_t i = 0 ;
7895 size_t num_entries = 0 ;
7996 binentry_t * entries = NULL ;
@@ -98,43 +115,48 @@ int main(int argc, const char* argv[]) {
98115 return EXIT_FAILURE ;
99116 }
100117
101- for (i = 0 ; i < num_entries ; i ++ ) {
118+ for (i = 0 ; i < num_entries ; i ++ ) {
102119 char * endptr = NULL ;
103120 struct stat st ;
104121
105- entries [i ].address = strtol (argv [2 * i + 2 ], & endptr , 0 );
122+ entries [i ].address = strtol (argv [2 * i + 2 ], & endptr , 0 );
106123 if (* endptr ) {
107124 fprintf (stderr ,
108- "Remaining characters in address field %s\n" , endptr );
125+ "Remaining characters in address field %s\n" , endptr );
109126 }
110- entries [i ].fname = argv [2 * i + 3 ];
127+ entries [i ].fname = argv [2 * i + 3 ];
111128
112129 if (stat (entries [i ].fname , & st )) {
130+ #ifdef _WIN32
131+ fprintf (stderr , "unable to stat %s: %s\n" ,
132+ entries [i ].fname , win_strerror (errno , errbuf , sizeof errbuf ));
133+ #else
113134 fprintf (stderr , "unable to stat %s: %s\n" , entries [i ].fname ,
114- strerror (errno ));
135+ strerror (errno ));
136+ #endif
115137 return EXIT_FAILURE ;
116138 }
117139
118140 entries [i ].nbytes = st .st_size ;
119141
120142#if VERBOSE
121143 printf ("%s %zu %zu\n" ,
122- entries [i ].fname ,
123- entries [i ].address ,
124- entries [i ].nbytes );
144+ entries [i ].fname ,
145+ entries [i ].address ,
146+ entries [i ].nbytes );
125147#endif
126148 }
127149
128150 qsort (entries , num_entries , sizeof (binentry_t ), binentry_address_compare );
129151
130152 // check for overlap
131- for (i = 1 ; i < num_entries ; i ++ ) {
132- size_t endaddr = entries [i - 1 ].address + entries [i - 1 ].nbytes ;
153+ for (i = 1 ; i < num_entries ; i ++ ) {
154+ size_t endaddr = entries [i - 1 ].address + entries [i - 1 ].nbytes ;
133155 if (endaddr > entries [i ].address ) {
134156 fprintf (stderr ,
135- "overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n" ,
136- entries [i - 1 ].fname , endaddr ,
137- entries [i ].fname , entries [i ].address );
157+ "overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n" ,
158+ entries [i - 1 ].fname , endaddr ,
159+ entries [i ].fname , entries [i ].address );
138160 err = 1 ;
139161 }
140162 if (err ) {
@@ -144,23 +166,39 @@ int main(int argc, const char* argv[]) {
144166 }
145167
146168 // TODO: consider handling stdout "-"
147-
148- FILE * fo = fopen ( outname , "wb" );
149- if (fo == NULL ) {
169+ #ifdef _WIN32
170+ fe = fopen_s ( & fo , outname , "wb" );
171+ if (fo == NULL || fe != 0 ) {
150172 fprintf (stderr , "opening %s failed %s\n" ,
151- outname , strerror ( errno ));
173+ outname , win_strerror (( int ) fe , errbuf , sizeof errbuf ));
152174 return EXIT_FAILURE ;
153175 }
176+ #else
177+ fo = fopen (outname , "wb" );
178+ if (fo == NULL ) {
179+ fprintf (stderr , "opening %s failed %s\n" , outname , strerror (errno ));
180+ return EXIT_FAILURE ;
181+ }
182+ #endif
154183
155184 cur_add = entries [0 ].address ;
156185 for (i = 0 ; i < num_entries ; i ++ ) {
157186 size_t fillSz = entries [i ].address - cur_add ;
158- FILE * fi = fopen (entries [i ].fname , "rb" );
159- if (fi == NULL ){
187+ #ifdef _WIN32
188+ fe = fopen_s (& fi , entries [i ].fname , "rb" );
189+ if (fi == NULL || fe != 0 ) {
190+ fprintf (stderr , "opening %s failed %s\n" ,
191+ entries [i ].fname , win_strerror ((int )fe , errbuf , sizeof errbuf ));
192+ return EXIT_FAILURE ;
193+ }
194+ #else
195+ fi = fopen (entries [i ].fname , "rb" );
196+ if (fi == NULL ) {
160197 fprintf (stderr , "opening %s failed %s\n" ,
161- entries [i ].fname , strerror (errno ));
198+ entries [i ].fname , strerror (errno ));
162199 return EXIT_FAILURE ;
163200 }
201+ #endif
164202
165203 if (fillSz > 0 && fillSz < INT_MAX ) {
166204 /* fill until address - blocks then bytes */
@@ -194,7 +232,7 @@ int main(int argc, const char* argv[]) {
194232 cur_add += nw ;
195233 if (nr != nw ) {
196234 fprintf (stderr ,
197- "Failed to wrote %zu bytes of the %zu bytes read from %s\n" ,
235+ "Failed to write %zu bytes of the %zu bytes read from %s\n" ,
198236 nw , nr , entries [i ].fname );
199237 return EXIT_FAILURE ;
200238 }
@@ -209,10 +247,9 @@ int main(int argc, const char* argv[]) {
209247 if (ferror (fo )) {
210248 fprintf (stderr ,
211249 "error writing to %s\n" ,
212- entries [ i ]. fname );
250+ outname );
213251 return EXIT_FAILURE ;
214252 }
215-
216253 }
217254
218255 fprintf (stderr , "\tAdded %12zu bytes at 0x%08zx from %s\n" ,
0 commit comments