2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2011 Jo-Philipp Wich <jow@openwrt.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 * protoent conversion helper
28 static int nixio__pushprotoent(lua_State *L, struct protoent *e) {
33 lua_pushstring(L, e->p_name);
34 lua_setfield(L, -2, "name");
36 lua_pushnumber(L, e->p_proto);
37 lua_setfield(L, -2, "proto");
40 for (i = 0; e->p_aliases[i]; i++) {
41 lua_pushstring(L, e->p_aliases[i]);
42 lua_rawseti(L, -2, i+1);
44 lua_setfield(L, -2, "aliases");
52 * getprotobyname(name)
54 static int nixio_getprotobyname(lua_State *L) {
55 const char *name = luaL_checkstring(L, 1);
56 struct protoent *res = getprotobyname(name);
57 return nixio__pushprotoent(L, res);
61 * getprotobynumber(proto)
63 static int nixio_getprotobynumber(lua_State *L) {
64 int proto = luaL_checkinteger(L, 1);
65 struct protoent *res = getprotobynumber(proto);
66 return nixio__pushprotoent(L, res);
70 * getproto(name_or_proto)
72 static int nixio_getproto(lua_State *L) {
75 if (lua_isnumber(L, 1)) {
76 return nixio_getprotobynumber(L);
77 } else if (lua_isstring(L, 1)) {
78 return nixio_getprotobyname(L);
79 } else if (lua_isnoneornil(L, 1)) {
82 while ((res = getprotoent()) != NULL) {
83 nixio__pushprotoent(L, res);
84 lua_rawseti(L, -2, i++);
89 return luaL_argerror(L, 1, "supported values: <protoname>, <protonumber>");
94 static const luaL_reg R[] = {
95 {"getprotobyname", nixio_getprotobyname},
96 {"getprotobynumber", nixio_getprotobynumber},
97 {"getproto", nixio_getproto},
101 void nixio_open_protoent(lua_State *L) {
102 luaL_register(L, NULL, R);