summaryrefslogtreecommitdiff
path: root/src/tet3/xtilib/inetoul.c
blob: 1e0b0b7fa76f6065814ba2a7d79963b03fa361bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 *      SCCS:  @(#)inetoul.c	1.6 (96/11/04) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1993 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieoctet system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)inetoul.c	1.6 96/11/04 TETware release 3.3
NAME:		inetoul.c
PRODUCT:	TETware
AUTHOR:		Denis McConalogue, UniSoft Ltd.
DATE CREATED:	August 1993

DESCRIPTION:
	Convert an inet address of the form aa.bb.cc.dd to unsigned long.
	Returns 0 if the char format address is invalid. Network byte
	ordering is assumed.

MODIFICATIONS:
	
	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <errno.h>
#ifdef TCPTPI
#  include <netinet/in.h>
#endif
#include "dtmac.h"
#include "error.h"
#include "dtetlib.h"

#define	N_PARTS	4

unsigned long tet_inetoul(cp)
register char *cp;
{
#ifdef TCPTPI
	unsigned long ip=0, base, part;
	int i;
	char c, *sp=cp;
	
	for (i=0; i < N_PARTS; i++, cp++) {
	
	        /* Accumulate part up to `.' with 0x=hex, 0=octal,
		   anything else assumed decimal */
	
		base  = 10;
		if (*cp == '0') {
	                base = 8, cp++;
			if (*cp == 'X' || *cp == 'x')
				base = 16, cp ++;
		}
	
		part = 0;
	        while ((c = *cp) && (c != '.')) {
	                if (isdigit(c)) {
	                        part = (part*base) + (c-'0');
	                        cp++;
	                }
			else if (base == 16 && isxdigit(c)) {
	                        part = (part*base) + (c+10-(islower(c)?'a':'A'));
	                        cp++;
			}
			else if (*cp != '.') {
				error(0, "bad format IP address", sp);
				return (0);
			}
	        }
		ip = (ip << 8) + part;
	}
	return (htonl(ip));
#else
	return ((unsigned long) 0);
#endif /* TCPTPI */
}