Home | History | Annotate | Line # | Download | only in vndcompress
utils.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: utils.c,v 1.1 2014/01/22 06:14:20 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.1  riastrad __RCSID("$NetBSD: utils.c,v 1.1 2014/01/22 06:14:20 riastradh Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/types.h>
     36  1.1  riastrad 
     37  1.1  riastrad #include <assert.h>
     38  1.1  riastrad #include <err.h>
     39  1.1  riastrad #include <errno.h>
     40  1.1  riastrad #include <inttypes.h>
     41  1.1  riastrad #include <limits.h>
     42  1.1  riastrad #include <stdio.h>
     43  1.1  riastrad #include <stdlib.h>
     44  1.1  riastrad #include <string.h>
     45  1.1  riastrad #include <unistd.h>
     46  1.1  riastrad 
     47  1.1  riastrad /* XXX Seems to be missing from <stdio.h>...  */
     48  1.1  riastrad int	snprintf_ss(char *restrict, size_t, const char *restrict, ...)
     49  1.1  riastrad 	    __printflike(3, 4);
     50  1.1  riastrad int	vsnprintf_ss(char *restrict, size_t, const char *restrict, va_list)
     51  1.1  riastrad 	    __printflike(3, 0);
     52  1.1  riastrad 
     53  1.1  riastrad #include "utils.h"
     54  1.1  riastrad 
     55  1.1  riastrad /*
     56  1.1  riastrad  * Read, returning partial data only at end of file.
     57  1.1  riastrad  */
     58  1.1  riastrad ssize_t
     59  1.1  riastrad read_block(int fd, void *buffer, size_t n)
     60  1.1  riastrad {
     61  1.1  riastrad 	char *p = buffer, *const end __unused = (p + n);
     62  1.1  riastrad 	size_t total_read = 0;
     63  1.1  riastrad 
     64  1.1  riastrad 	while (n > 0) {
     65  1.1  riastrad 		const ssize_t n_read = read(fd, p, n);
     66  1.1  riastrad 		if (n_read == -1)
     67  1.1  riastrad 			return -1;
     68  1.1  riastrad 		assert(n_read >= 0);
     69  1.1  riastrad 		if (n_read == 0)
     70  1.1  riastrad 			break;
     71  1.1  riastrad 
     72  1.1  riastrad 		assert((size_t)n_read <= n);
     73  1.1  riastrad 		n -= (size_t)n_read;
     74  1.1  riastrad 
     75  1.1  riastrad 		assert(p <= end);
     76  1.1  riastrad 		assert(n_read <= (end - p));
     77  1.1  riastrad 		p += (size_t)n_read;
     78  1.1  riastrad 
     79  1.1  riastrad 		assert((size_t)n_read <= (SIZE_MAX - total_read));
     80  1.1  riastrad 		total_read += (size_t)n_read;
     81  1.1  riastrad 	}
     82  1.1  riastrad 
     83  1.1  riastrad 	return total_read;
     84  1.1  riastrad }
     85  1.1  riastrad 
     86  1.1  riastrad /*
     87  1.1  riastrad  * Signal-safe err/warn utilities.  The errno varieties are limited to
     88  1.1  riastrad  * having no format arguments for reasons of laziness.
     89  1.1  riastrad  */
     90  1.1  riastrad 
     91  1.1  riastrad void
     92  1.1  riastrad err_ss(int exit_value, const char *msg)
     93  1.1  riastrad {
     94  1.1  riastrad 	warn_ss(msg);
     95  1.1  riastrad 	_Exit(exit_value);
     96  1.1  riastrad }
     97  1.1  riastrad 
     98  1.1  riastrad void
     99  1.1  riastrad errx_ss(int exit_value, const char *format, ...)
    100  1.1  riastrad {
    101  1.1  riastrad 	va_list va;
    102  1.1  riastrad 
    103  1.1  riastrad 	va_start(va, format);
    104  1.1  riastrad 	vwarnx_ss(format, va);
    105  1.1  riastrad 	va_end(va);
    106  1.1  riastrad 	_Exit(exit_value);
    107  1.1  riastrad }
    108  1.1  riastrad 
    109  1.1  riastrad void
    110  1.1  riastrad warn_ss(const char *msg)
    111  1.1  riastrad {
    112  1.1  riastrad 	int error = errno;
    113  1.1  riastrad 
    114  1.1  riastrad 	warnx_ss("%s: %s", msg, strerror(error));
    115  1.1  riastrad 
    116  1.1  riastrad 	errno = error;
    117  1.1  riastrad }
    118  1.1  riastrad 
    119  1.1  riastrad void
    120  1.1  riastrad warnx_ss(const char *format, ...)
    121  1.1  riastrad {
    122  1.1  riastrad 	va_list va;
    123  1.1  riastrad 
    124  1.1  riastrad 	va_start(va, format);
    125  1.1  riastrad 	vwarnx_ss(format, va);
    126  1.1  riastrad 	va_end(va);
    127  1.1  riastrad }
    128  1.1  riastrad 
    129  1.1  riastrad void
    130  1.1  riastrad vwarnx_ss(const char *format, va_list va)
    131  1.1  riastrad {
    132  1.1  riastrad 	char buf[128];
    133  1.1  riastrad 
    134  1.1  riastrad 	(void)strlcpy(buf, getprogname(), sizeof(buf));
    135  1.1  riastrad 	(void)strlcat(buf, ": ", sizeof(buf));
    136  1.1  riastrad 
    137  1.1  riastrad 	const int n = vsnprintf_ss(&buf[strlen(buf)], (sizeof(buf) -
    138  1.1  riastrad 		strlen(buf)), format, va);
    139  1.1  riastrad 	if (n <= 0) {
    140  1.1  riastrad 		const char fallback[] =
    141  1.1  riastrad 		    "vndcompress: Help!  I'm trapped in a signal handler!\n";
    142  1.1  riastrad 		(void)write(STDERR_FILENO, fallback, __arraycount(fallback));
    143  1.1  riastrad 	} else {
    144  1.1  riastrad 		(void)strlcat(buf, "\n", sizeof(buf));
    145  1.1  riastrad 		(void)write(STDERR_FILENO, buf, strlen(buf));
    146  1.1  riastrad 	}
    147  1.1  riastrad }
    148