preconv.c (86d7f5d3) preconv.c (36342e81)
1/* $Id: preconv.c,v 1.4 2011/05/26 21:13:07 kristaps Exp $ */
1/* $Id: preconv.c,v 1.5 2011/07/24 18:15:14 kristaps Exp $ */
2/*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
2/*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#ifdef HAVE_MMAP
21#include <sys/stat.h>
22#include <sys/mman.h>
22#include <sys/stat.h>
23#include <sys/mman.h>
24#endif
23
24#include <assert.h>
25#include <fcntl.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30

--- 208 unchanged lines hidden (view full) ---

239 exit(EXIT_FAILURE);
240 }
241}
242
243static int
244read_whole_file(const char *f, int fd,
245 struct buf *fb, int *with_mmap)
246{
25
26#include <assert.h>
27#include <fcntl.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32

--- 208 unchanged lines hidden (view full) ---

241 exit(EXIT_FAILURE);
242 }
243}
244
245static int
246read_whole_file(const char *f, int fd,
247 struct buf *fb, int *with_mmap)
248{
247 struct stat st;
248 size_t off;
249 ssize_t ssz;
250
249 size_t off;
250 ssize_t ssz;
251
252#ifdef HAVE_MMAP
253 struct stat st;
251 if (-1 == fstat(fd, &st)) {
252 perror(f);
253 return(0);
254 }
255
256 /*
257 * If we're a regular file, try just reading in the whole entry
258 * via mmap(). This is faster than reading it into blocks, and

--- 9 unchanged lines hidden (view full) ---

268 if (S_ISREG(st.st_mode)) {
269 *with_mmap = 1;
270 fb->sz = (size_t)st.st_size;
271 fb->buf = mmap(NULL, fb->sz, PROT_READ,
272 MAP_FILE|MAP_SHARED, fd, 0);
273 if (fb->buf != MAP_FAILED)
274 return(1);
275 }
254 if (-1 == fstat(fd, &st)) {
255 perror(f);
256 return(0);
257 }
258
259 /*
260 * If we're a regular file, try just reading in the whole entry
261 * via mmap(). This is faster than reading it into blocks, and

--- 9 unchanged lines hidden (view full) ---

271 if (S_ISREG(st.st_mode)) {
272 *with_mmap = 1;
273 fb->sz = (size_t)st.st_size;
274 fb->buf = mmap(NULL, fb->sz, PROT_READ,
275 MAP_FILE|MAP_SHARED, fd, 0);
276 if (fb->buf != MAP_FAILED)
277 return(1);
278 }
279#endif
276
277 /*
278 * If this isn't a regular file (like, say, stdin), then we must
279 * go the old way and just read things in bit by bit.
280 */
281
282 *with_mmap = 0;
283 off = 0;

--- 221 unchanged lines hidden (view full) ---

505
506 if ( ! (*encs[(int)enc].conv)(&b)) {
507 fprintf(stderr, "%s: Bad encoding\n", fn);
508 goto out;
509 }
510
511 rc = EXIT_SUCCESS;
512out:
280
281 /*
282 * If this isn't a regular file (like, say, stdin), then we must
283 * go the old way and just read things in bit by bit.
284 */
285
286 *with_mmap = 0;
287 off = 0;

--- 221 unchanged lines hidden (view full) ---

509
510 if ( ! (*encs[(int)enc].conv)(&b)) {
511 fprintf(stderr, "%s: Bad encoding\n", fn);
512 goto out;
513 }
514
515 rc = EXIT_SUCCESS;
516out:
517#ifdef HAVE_MMAP
513 if (map)
514 munmap(b.buf, b.sz);
515 else
518 if (map)
519 munmap(b.buf, b.sz);
520 else
521#endif
516 free(b.buf);
517
518 if (fd > STDIN_FILENO)
519 close(fd);
520
521 return(rc);
522}
522 free(b.buf);
523
524 if (fd > STDIN_FILENO)
525 close(fd);
526
527 return(rc);
528}