1/* $NetBSD: strtoll.c,v 1.3 2007/06/16 23:13:26 agc Exp $ */
2
3/*
4 * Copyright � 2006 Alistair Crooks.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 *    products derived from this software without specific prior written
16 *    permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30#include "config.h"
31
32#include <sys/types.h>
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38#include "compat.h"
39
40#ifndef HAVE_STRTOLL
41
42static char	*digits = "0123456789abcdefghijklmnopqrstuvwxyz";
43
44int64_t
45strtoll(const char *ptr, const char **endptr, int base)
46{
47	const char	*cp;
48	int64_t		 ret;
49	char		*dig;
50	int		 d;
51
52	for (ret = 0, cp = ptr ; *cp && (dig = strchr(digits, *cp)) != NULL  && (d = (int)(dig - digits)) < base ; cp++) {
53		ret = (ret * base) + d;
54	}
55	if (endptr != NULL) {
56		*endptr = cp;
57	}
58	return ret;
59}
60#endif /* HAVE_STRTOLL */
61