Add copyright notices and new function String.chomp
[ocaml.git] / otherlibs / unix / getserv.c
blobe882e8435745ac2f65564759cb8a582011df6b4c
1 /***********************************************************************/
2 /* */
3 /* Objective Caml */
4 /* */
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
6 /* */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the GNU Library General Public License, with */
10 /* the special exception on linking described in file ../../LICENSE. */
11 /* */
12 /***********************************************************************/
14 /* $Id$ */
16 #include <mlvalues.h>
17 #include <alloc.h>
18 #include <fail.h>
19 #include <memory.h>
20 #include "unixsupport.h"
22 #ifdef HAS_SOCKETS
24 #include <sys/types.h>
26 #ifndef _WIN32
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netdb.h>
30 #else
31 #include <winsock.h>
32 #endif
34 static value alloc_service_entry(struct servent *entry)
36 value res;
37 value name = Val_unit, aliases = Val_unit, proto = Val_unit;
39 Begin_roots3 (name, aliases, proto);
40 name = copy_string(entry->s_name);
41 aliases = copy_string_array((const char**)entry->s_aliases);
42 proto = copy_string(entry->s_proto);
43 res = alloc_small(4, 0);
44 Field(res,0) = name;
45 Field(res,1) = aliases;
46 Field(res,2) = Val_int(ntohs(entry->s_port));
47 Field(res,3) = proto;
48 End_roots();
49 return res;
52 CAMLprim value unix_getservbyname(value name, value proto)
54 struct servent * entry;
55 entry = getservbyname(String_val(name), String_val(proto));
56 if (entry == (struct servent *) NULL) raise_not_found();
57 return alloc_service_entry(entry);
60 CAMLprim value unix_getservbyport(value port, value proto)
62 struct servent * entry;
63 entry = getservbyport(htons(Int_val(port)), String_val(proto));
64 if (entry == (struct servent *) NULL) raise_not_found();
65 return alloc_service_entry(entry);
68 #else
70 CAMLprim value unix_getservbyport(value port, value proto)
71 { invalid_argument("getservbyport not implemented"); }
73 CAMLprim value unix_getservbyname(value name, value proto)
74 { invalid_argument("getservbyname not implemented"); }
76 #endif