Fix all the missing comments.

This commit is contained in:
Enrico Fraccaroli
2022-01-03 22:25:21 +01:00
parent 181d1d69cb
commit 2461d82431
59 changed files with 826 additions and 601 deletions
+32 -35
View File
@@ -3,7 +3,7 @@
/// @copyright (c) 2014-2021 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include "grp.h"
#include "grp.h"
#include "sys/unistd.h"
#include "sys/errno.h"
#include "assert.h"
@@ -12,8 +12,9 @@
#include "debug.h"
#include "fcntl.h"
static int __fd = -1;
static inline void __parse_line(struct group* grp, char *buf)
static inline void __parse_line(group_t *grp, char *buf)
{
assert(grp && "Received null grp!");
char *token;
@@ -28,7 +29,7 @@ static inline void __parse_line(struct group* grp, char *buf)
grp->gr_gid = atoi(token);
size_t found_users = 0;
while((token = strtok(NULL, ",\n\0")) != NULL && found_users < MAX_MEMBERS_PER_GROUP) {
while ((token = strtok(NULL, ",\n\0")) != NULL && found_users < MAX_MEMBERS_PER_GROUP) {
grp->gr_mem[found_users] = token;
found_users += 1;
}
@@ -89,45 +90,44 @@ static inline char *__search_entry(int fd, char *buf, int buflen, const char *na
return NULL;
}
struct group* getgrgid(gid_t gid) {
static group grp;
group_t *getgrgid(gid_t gid)
{
static group_t grp;
static char buffer[BUFSIZ];
group *result;
group_t *result;
if (!getgrgid_r(gid, &grp, buffer, BUFSIZ, &result))
return NULL;
return &grp;
}
struct group *getgrnam(const char* name) {
group_t *getgrnam(const char *name)
{
if (name == NULL)
return NULL;
static group grp;
static group_t grp;
static char buffer[BUFSIZ];
group *result;
group_t *result;
if (!getgrnam_r(name, &grp, buffer, BUFSIZ, &result))
return NULL;
return &grp;
}
int getgrgid_r(gid_t gid, struct group* group, char* buf, size_t buflen, struct group ** result) {
int getgrgid_r(gid_t gid, group_t *group, char *buf, size_t buflen, group_t **result)
{
int fd = open("/etc/group", O_RDONLY, 0);
if (fd == -1) {
errno = ENOENT;
*result = NULL;
return 0;
}
char *entry = __search_entry(fd, buf, buflen, NULL, gid);
if (entry != NULL) {
// Close the file.
close(fd);
// Parse the line.
@@ -142,18 +142,17 @@ int getgrgid_r(gid_t gid, struct group* group, char* buf, size_t buflen, struct
return 0;
}
int getgrnam_r(const char* name, struct group* group, char* buf, size_t buflen, struct group** result) {
int getgrnam_r(const char *name, group_t *group, char *buf, size_t buflen, group_t **result)
{
int fd = open("/etc/group", O_RDONLY, 0);
if (fd == -1) {
errno = ENOENT;
*result = NULL;
return 0;
}
char *entry = __search_entry(fd, buf, buflen, name, 0);
if (entry != NULL) {
// Close the file.
close(fd);
// Parse the line.
@@ -168,15 +167,14 @@ int getgrnam_r(const char* name, struct group* group, char* buf, size_t buflen,
return 0;
}
static int fd = -1;
struct group* getgrent(void) {
group_t *getgrent(void)
{
static group_t result;
static group result;
if (fd == -1) {
if (__fd == -1) {
//pr_debug("Opening group file\n");
fd = open("/etc/group", O_RDONLY, 0);
if (fd == -1) {
__fd = open("/etc/group", O_RDONLY, 0);
if (__fd == -1) {
errno = ENOENT;
return 0;
}
@@ -187,8 +185,7 @@ struct group* getgrent(void) {
int pos = 0;
static char buffer[BUFSIZ];
while ((ret = read(fd, &c, 1U))) {
while ((ret = read(__fd, &c, 1U))) {
// Skip carriage return.
if (c == '\r')
continue;
@@ -223,15 +220,15 @@ struct group* getgrent(void) {
return NULL;
}
void endgrent(void) {
void endgrent(void)
{
//pr_debug("Closing group file\n");
close(fd);
fd = -1;
close(__fd);
__fd = -1;
}
void setgrent(void) {
void setgrent(void)
{
//pr_debug("Resetting pointer to beginning of group file\n");
lseek(fd, 0, SEEK_SET);
lseek(__fd, 0, SEEK_SET);
}