| @@ -1,17 +1,17 @@ | | | @@ -1,17 +1,17 @@ |
1 | /* | | 1 | /* |
2 | * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $ | | 2 | * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $ |
3 | * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $ | | 3 | * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $ |
4 | * $NetBSD: inp.c,v 1.5 2009/06/05 20:00:26 joerg Exp $ | | 4 | * $NetBSD: inp.c,v 1.6 2009/10/21 17:17:04 joerg Exp $ |
5 | */ | | 5 | */ |
6 | | | 6 | |
7 | /* | | 7 | /* |
8 | * patch - a program to apply diffs to original files | | 8 | * patch - a program to apply diffs to original files |
9 | * | | 9 | * |
10 | * Copyright 1986, Larry Wall | | 10 | * Copyright 1986, Larry Wall |
11 | * | | 11 | * |
12 | * Redistribution and use in source and binary forms, with or without | | 12 | * Redistribution and use in source and binary forms, with or without |
13 | * modification, are permitted provided that the following condition is met: | | 13 | * modification, are permitted provided that the following condition is met: |
14 | * 1. Redistributions of source code must retain the above copyright notice, | | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
15 | * this condition and the following disclaimer. | | 15 | * this condition and the following disclaimer. |
16 | * | | 16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY | | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
| @@ -259,32 +259,36 @@ plan_a(const char *filename) | | | @@ -259,32 +259,36 @@ plan_a(const char *filename) |
259 | i_size = filestat.st_size; | | 259 | i_size = filestat.st_size; |
260 | if (out_of_mem) { | | 260 | if (out_of_mem) { |
261 | set_hunkmax(); /* make sure dynamic arrays are allocated */ | | 261 | set_hunkmax(); /* make sure dynamic arrays are allocated */ |
262 | out_of_mem = false; | | 262 | out_of_mem = false; |
263 | return false; /* force plan b because plan a bombed */ | | 263 | return false; /* force plan b because plan a bombed */ |
264 | } | | 264 | } |
265 | if (i_size > SIZE_MAX) { | | 265 | if (i_size > SIZE_MAX) { |
266 | say("block too large to mmap\n"); | | 266 | say("block too large to mmap\n"); |
267 | return false; | | 267 | return false; |
268 | } | | 268 | } |
269 | if ((ifd = open(filename, O_RDONLY)) < 0) | | 269 | if ((ifd = open(filename, O_RDONLY)) < 0) |
270 | pfatal("can't open file %s", filename); | | 270 | pfatal("can't open file %s", filename); |
271 | | | 271 | |
272 | i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); | | 272 | if (i_size) { |
273 | if (i_womp == MAP_FAILED) { | | 273 | i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); |
274 | perror("mmap failed"); | | 274 | if (i_womp == MAP_FAILED) { |
| | | 275 | perror("mmap failed"); |
| | | 276 | i_womp = NULL; |
| | | 277 | close(ifd); |
| | | 278 | return false; |
| | | 279 | } |
| | | 280 | } else { |
275 | i_womp = NULL; | | 281 | i_womp = NULL; |
276 | close(ifd); | | | |
277 | return false; | | | |
278 | } | | 282 | } |
279 | | | 283 | |
280 | close(ifd); | | 284 | close(ifd); |
281 | #ifdef MADV_SEQUENTIAL | | 285 | #ifdef MADV_SEQUENTIAL |
282 | if (i_size) | | 286 | if (i_size) |
283 | madvise(i_womp, i_size, MADV_SEQUENTIAL); | | 287 | madvise(i_womp, i_size, MADV_SEQUENTIAL); |
284 | #elif defined(POSIX_MADV_SEQUENTIAL) | | 288 | #elif defined(POSIX_MADV_SEQUENTIAL) |
285 | if (i_size) | | 289 | if (i_size) |
286 | posix_madvise(i_womp, i_size, POSIX_MADV_SEQUENTIAL); | | 290 | posix_madvise(i_womp, i_size, POSIX_MADV_SEQUENTIAL); |
287 | #endif | | 291 | #endif |
288 | | | 292 | |
289 | /* estimate the number of lines */ | | 293 | /* estimate the number of lines */ |
290 | lines_allocated = i_size / 25; | | 294 | lines_allocated = i_size / 25; |