<commit_before>/*
 * POSIX library for Lua 5.1/5.2.
 * (c) <NAME> <<EMAIL>>, 2013-2014
 * (c) <NAME> <<EMAIL>> 2010-2013
 * (c) <NAME> <<EMAIL>> 2008-2010
 * Clean up and bug fixes by <NAME> <<EMAIL>> 2006-10-11
 * <NAME> <<EMAIL>> 07 Apr 2006 23:17:49
 * Based on original by Claudio Terra for Lua 3.x.
 * With contributions by <NAME>.
 * With documentation from <NAME> 2012
 */
/***
 Group Database Operations.

 Query the system group database.

@see posix.pwd
@module posix.grp
*/

#include <config.h>

#include <grp.h>

#include "_helpers.c"


/***
Group record.
@table group
@string gr_name name of group
@int gr_gid unique group id
@tfield list gr_mem a list of group members
*/

static int
pushgroup(lua_State *L, struct group *g)
{
	if (!g)
	{
		lua_pushnil(L);
		return 1;
	}

	lua_createtable(L, 0, 3);
	lua_pushnumber(L, (int) g->gr_gid);
	lua_setfield(L, -2, "gr_gid");
	if (g->gr_name)
	{
		lua_pushstring(L, g->gr_name);
		lua_setfield(L, -2, "gr_name");
	}
	if (g->gr_mem)
	{
		int i;
		lua_newtable(L);
		for (i = 0; g->gr_mem[i] != NULL; i++)
		{
			lua_pushstring(L, g->gr_mem[i]);
			lua_rawseti(L, -2, i + 1);
		}
		lua_setfield(L, -2, "gr_mem");
	}
	return 1;
}


/***
Release group database resources.
@function endgrent
@see getgrent
*/
static int
Pendgrent(lua_State *L)
{
	checknargs(L, 0);
	endgrent();
	return 0;
}


/***
Fetch next group.
@function getgrent
@treturn group next group record
@see endgrent
@usage
  t = P.getgrent ()
  while t ~= nil do
    process (t)
    t = P.getgrent ()
  end
  P.endgrent ()
*/
static int
Pgetgrent(lua_State *L)
{
	struct group *g;
	checknargs(L, 0);
	g = getgrent();
	if (!g && errno == 0)
		endgrent ();
	return pushgroup(L, g);
}


/***
Fetch group with given groud id.
@function getgrgid
@int gid group id
@treturn group group record for *gid*
@usage
  t = P.getgrgid (0)
*/
static int
Pgetgrgid(lua_State *L)
{
	gid_t gid = (gid_t) checkint(L, 1);
	struct group *g;
	checknargs(L, 1);

	errno = 0;	/* so we can recognise a successful empty result */
	g = getgrgid (gid);
	if (!g && errno != 0)
		return pusherror(L, "getgrgid");
	return pushgroup(L, g);
}


/***
Fetch named group.
@function getgrnam
@string name group name
@treturn group group record for *name*
@usage
  t = P.getgrnam "wheel"<commit_msg>refactor: tidy up formatting in grp.c.<commit_after>/*
 * POSIX library for Lua 5.1/5.2.
 * (c) <NAME> <<EMAIL>>, 2013-2014
 * (c) <NAME> <<EMAIL>> 2010-2013
 * (c) <NAME> <<EMAIL>> 2008-2010
 * Clean up and bug fixes by Le<NAME> <<EMAIL>> 2006-10-11
 * <NAME> <<EMAIL>> 07 Apr 2006 23:17:49
 * Based on original by <NAME> for Lua 3.x.
 * With contributions by <NAME>.
 * With documentation from <NAME> 2012
 */
/***
 Group Database Operations.

 Query the system group database.

@see posix.pwd
@module posix.grp
*/

#include <config.h>

#include <grp.h>

#include "_helpers.c"


/***
Group record.
@table group
@string gr_name name of group
@int gr_gid unique group id
@tfield list gr_mem a list of group members
*/

static int
pushgroup(lua_State *L, struct group *g)
{
	if (!g)
	{
		lua_pushnil(L);
		return 1;
	}

	lua_createtable(L, 0, 3);
	lua_pushnumber(L, (int) g->gr_gid);
	lua_setfield(L, -2, "gr_gid");
	if (g->gr_name)
	{
		lua_pushstring(L, g->gr_name);
		lua_setfield(L, -2, "gr_name");
	}
	if (g->gr_mem)
	{
		int i;
		lua_newtable(L);
		for (i = 0; g->gr_mem[i] != NULL; i++)
		{
			lua_pushstring(L, g->gr_mem[i]);
			lua_rawseti(L, -2, i + 1);
		}
		lua_setfield(L, -2, "gr_mem");
	}
	return 1;
}


/***
Release group database resources.
@function endgrent
@see getgrent
*/
static int
Pendgrent(lua_State *L)
{
	checknargs(L, 0);
	endgrent();
	return 0;
}


/***
Fetch next group.
@function getgrent
@treturn group next group record
@see endgrent
@usage
  t = P.getgrent ()
  while t ~= nil do
    process (t)
    t = P.getgrent ()
  end
  P.endgrent ()
*/
static int
Pgetgrent(lua_State *L)
{
	struct group *g;
	checknargs(L, 0);
	g = getgrent();
	if (!g && errno == 0)
		endgrent();
	return pushgroup(L, g);
}


/***
Fetch group with given group id.
@function getgrgid
@int gid group id
@treturn group group record for *gid*
@usage
  t = P.getgrgid (0)
*/
static int
Pgetgrgid(lua_State *L)
{
	gid_t gid = (gid_t) checkint(L, 1);
	struct group *g;
	checknargs(L, 1);

	errno = 0;	/* so we can recognise a successful empty result */
	g = getgrgid(gid);
	if (!g && errno != 0)
		return pusherror(L, "getgrgid");
	return pushgroup(L, g);
}


/***
Fetch named group.
@function getgrnam
@string name group name
@treturn group group record for *name*
@usage
  t = P.getgrnam "wheel"
{'fraction_non_alphanumeric': 0.16105769230769232, 'fraction_numerical': 0.03146853146853147, 'mean_word_length': 3.554228855721393, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3254691', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1891, 'n_words': 562}