1 /*
2 * Copyright (C) 1998,1999 Uwe Ohse
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * As a special exception this source may be used as part of the
19 * SRS project by CORE/Computer Service Langenbach
20 * regardless of the copyright they choose.
21 *
22 * Contact: uwe@ohse.de
23 */
24
25 /* written mainly to get around the need to include a strtol.c in the distributions */
26 #include "config.h"
27 #include "str2num.h"
28
29 int
str2long(const char * s,long * lo,int base)30 str2long(const char *s, long *lo, int base)
31 {
32 int sign=0;
33 unsigned long ul;
34 int len;
35 int loff=0;
36 long l;
37 if (*s=='-') {s++; sign=1; loff++;}
38 else if (*s=='+') {s++; loff++;}
39 len=str2ulong(s,&ul,base);
40 if (len<0) return len;
41 if (len==0) {*lo=0;return len+loff;} /* consistence with str2ulong */
42 if (!sign) {
43 l=(long)ul;
44 if (l<0 || ul != (unsigned long)l) return -1;
45 *lo=l;
46 return len+loff;
47 }
48 l=(long)ul; if (ul != (unsigned long)l) return -1;
49 *lo=0-l;
50 return len+loff;
51 }
52