import cyrus-sasl-2.1.23
[cyrus-sasl.git] / mac / mac_lib / xxx_mac_lib.c
1 /*
2  * internaly sasl or its test programs use some functions which are not availible
3  * on the macintosh.  these have common names like strdrup gethostname etc.  defining
4  * them as routines could make conflicts with clients of the library.  in config.h
5  * we macro define such names to start with xxx_.  The implementation for them is
6  * here.  The xxx_ is in hopes of not conflicting with a name in client program.
7  */
8 /* $Id: xxx_mac_lib.c,v 1.3 2003/02/13 19:55:59 rjs3 Exp $
9  * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer. 
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  *
23  * 3. The name "Carnegie Mellon University" must not be used to
24  *    endorse or promote products derived from this software without
25  *    prior written permission. For permission or any other legal
26  *    details, please contact  
27  *      Office of Technology Transfer
28  *      Carnegie Mellon University
29  *      5000 Forbes Avenue
30  *      Pittsburgh, PA  15213-3890
31  *      (412) 268-4387, fax: (412) 268-7395
32  *      tech-transfer@andrew.cmu.edu
33  *
34  * 4. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by Computing Services
37  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
38  *
39  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
40  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
41  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
42  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
44  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
45  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46  */
47
48  #include <string.h>
49  #include <stdlib.h>
50  #include <ctype.h>
51  
52  #include <config.h>
53 // #include <netinet/in.h>
54
55 /*
56  * return the smaller of two integers
57  */
58 static int xxy_min(int a,int b)
59 {
60         if(a<b)
61                 return a;
62         return b;
63 }
64
65 static int limit_strcpy(char *dest,const char *src,int len)
66 {
67         int slen=strlen(src);
68         if(len<1)
69                 return 0;
70         slen=xxy_min(slen,len-1);
71         if(slen>0)
72                 memcpy(dest,src,slen);
73         dest[slen]=0;
74         return slen;    
75 }
76
77 int strcpy_truncate(char *dest,char *src,int len)
78 {
79         return limit_strcpy(dest,src,len);
80 }
81
82 int gethostname(char *dest,int destlen)
83 {
84         limit_strcpy(dest,"localhost",destlen);
85         return 0;
86 }
87
88 char *strdup(const char *str)
89 {
90         if(str==0)
91                 return 0;
92         {
93                 const int len=strlen(str);
94                 char *result=malloc(len+1);
95                 strcpy(result,str);
96                 return result;
97         }
98         
99 }
100  
101 int strncasecmp(const char *s1,const char *s2,int len)
102 {
103         while(len-- >0) {
104                 char c1= *s1++;
105                 char c2= *s2++;
106                 if((c1==0)&&(c2==0))
107                         return 0;
108                 if(c1==0)
109                         return -1;
110                 if(c2==0)
111                         return 1;
112                 /* last ansi spec i read tolower was undefined for non uppercase chars
113                  * but it works in most implementations
114                  */
115                 if(isupper(c1))
116                         c1=tolower(c1);
117                 if(isupper(c2))
118                         c2=tolower(c2);
119                 if(c1<c2)
120                         return -1;
121                 if(c1>c2)
122                         return 1;
123         }
124         return 1;
125 }
126
127 int strcasecmp(const char *s1,const char *s2)
128 {
129         while(1) {
130                 char c1= *s1++;
131                 char c2= *s2++;
132                 if((c1==0)&&(c2==0))
133                         return 0;
134                 if(c1==0)
135                         return -1;
136                 if(c2==0)
137                         return 1;
138                 /* last ansi spec i read tolower was undefined for non uppercase chars
139                  * but it works in most implementations
140                  */
141                 if(isupper(c1))
142                         c1=tolower(c1);
143                 if(isupper(c2))
144                         c2=tolower(c2);
145                 if(c1<c2)
146                         return -1;
147                 if(c1>c2)
148                         return 1;
149         }
150 }
151
152 int inet_aton(const char *cp, struct in_addr *inp)
153 {
154         char *cptr1, *cptr2, *cptr3;
155         long u;
156         char cptr0[256];
157         strcpy(cptr0, cp);
158
159         if (!(cptr1 = strchr(cptr0, '.'))) return 0;
160         *cptr1++ = 0;
161         if (!(cptr2 = strchr(cptr1, '.'))) return 0;
162         *cptr2++ = 0;
163         if (!(cptr3 = strchr(cptr2, '.'))) return 0;
164         *cptr3++ = 0;
165         if (!*cptr3) return 0;
166
167         u = ((atoi(cptr0) << 8 + atoi(cptr1)) << 8 + atoi(cptr2)) << 8 + atoi(cptr3);
168         inp->s_addr = htonl(u);
169         return 1;
170 }