forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add getpwent() family of functions to LibC.
Also add a little /etc/passwd database. There's just me in there.
- Loading branch information
1 parent
819ce91
commit 9886b27
Showing
17 changed files
with
175 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ class String { | |
{ | ||
} | ||
|
||
unsigned toUInt(bool& ok) const; | ||
|
||
String toLowercase() const | ||
{ | ||
if (!m_impl) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
root:x:0:0:root:/:/bin/sh | ||
andreas:x:100:100:Andreas Kling,,,:/users/andreas:/bin/sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ LIBC_OBJS = \ | |
signal.o \ | ||
getopt.o \ | ||
scanf.o \ | ||
pwd.o \ | ||
entry.o | ||
|
||
OBJS = $(AK_OBJS) $(LIBC_OBJS) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include <pwd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/mman.h> | ||
#include <AK/String.h> | ||
|
||
struct passwd_with_strings : public passwd { | ||
char name_buffer[256]; | ||
char passwd_buffer[256]; | ||
char gecos_buffer[256]; | ||
char dir_buffer[256]; | ||
char shell_buffer[256]; | ||
}; | ||
|
||
static FILE* __pwdb_stream = nullptr; | ||
static unsigned __pwdb_line_number = 0; | ||
static struct passwd_with_strings* __pwdb_entry = nullptr; | ||
|
||
void setpwent() | ||
{ | ||
__pwdb_line_number = 0; | ||
if (__pwdb_stream) { | ||
rewind(__pwdb_stream); | ||
} else { | ||
__pwdb_stream = fopen("/etc/passwd", "r"); | ||
__pwdb_entry = (struct passwd_with_strings*)mmap(nullptr, getpagesize()); | ||
} | ||
} | ||
|
||
void endpwent() | ||
{ | ||
__pwdb_line_number = 0; | ||
if (__pwdb_stream) { | ||
fclose(__pwdb_stream); | ||
__pwdb_stream = nullptr; | ||
} | ||
if (__pwdb_entry) { | ||
munmap(__pwdb_entry, getpagesize()); | ||
__pwdb_entry = nullptr; | ||
} | ||
} | ||
|
||
struct passwd* getpwuid(uid_t uid) | ||
{ | ||
setpwent(); | ||
while (auto* pw = getpwent()) { | ||
if (pw->pw_uid == uid) | ||
return pw; | ||
} | ||
return nullptr; | ||
} | ||
|
||
struct passwd* getpwnam(const char* name) | ||
{ | ||
setpwent(); | ||
while (auto* pw = getpwent()) { | ||
if (!strcmp(pw->pw_name, name)) | ||
return pw; | ||
} | ||
return nullptr; | ||
} | ||
|
||
struct passwd* getpwent() | ||
{ | ||
if (!__pwdb_stream) | ||
setpwent(); | ||
|
||
if (feof(__pwdb_stream)) | ||
return nullptr; | ||
|
||
next_entry: | ||
char buffer[1024]; | ||
++__pwdb_line_number; | ||
char* s = fgets(buffer, sizeof(buffer), __pwdb_stream); | ||
if (!s) | ||
return nullptr; | ||
if (feof(__pwdb_stream)) | ||
return nullptr; | ||
String line(s); | ||
auto parts = line.split(':'); | ||
if (parts.size() != 7) { | ||
fprintf(stderr, "getpwent(): Malformed entry on line %u\n", __pwdb_line_number); | ||
goto next_entry; | ||
} | ||
auto& e_name = parts[0]; | ||
auto& e_passwd = parts[1]; | ||
auto& e_uid_string = parts[2]; | ||
auto& e_gid_string = parts[3]; | ||
auto& e_gecos = parts[4]; | ||
auto& e_dir = parts[5]; | ||
auto& e_shell = parts[6]; | ||
bool ok; | ||
uid_t e_uid = e_uid_string.toUInt(ok); | ||
if (!ok) { | ||
fprintf(stderr, "getpwent(): Malformed UID on line %u\n", __pwdb_line_number); | ||
goto next_entry; | ||
} | ||
gid_t e_gid = e_gid_string.toUInt(ok); | ||
if (!ok) { | ||
fprintf(stderr, "getpwent(): Malformed GID on line %u\n", __pwdb_line_number); | ||
goto next_entry; | ||
} | ||
__pwdb_entry->pw_uid = e_uid; | ||
__pwdb_entry->pw_gid = e_gid; | ||
__pwdb_entry->pw_name = __pwdb_entry->name_buffer; | ||
__pwdb_entry->pw_passwd = __pwdb_entry->passwd_buffer; | ||
__pwdb_entry->pw_gecos = __pwdb_entry->gecos_buffer; | ||
__pwdb_entry->pw_dir = __pwdb_entry->dir_buffer; | ||
__pwdb_entry->pw_shell = __pwdb_entry->shell_buffer; | ||
strncpy(__pwdb_entry->name_buffer, e_name.characters(), e_name.length()); | ||
strncpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), e_passwd.length()); | ||
strncpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), e_gecos.length()); | ||
strncpy(__pwdb_entry->dir_buffer, e_dir.characters(), e_dir.length()); | ||
strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), e_shell.length()); | ||
return __pwdb_entry; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <sys/cdefs.h> | ||
#include <sys/types.h> | ||
|
||
__BEGIN_DECLS | ||
|
||
struct passwd { | ||
char* pw_name; | ||
char* pw_passwd; | ||
uid_t pw_uid; | ||
gid_t pw_gid; | ||
char* pw_gecos; | ||
char* pw_dir; | ||
char* pw_shell; | ||
}; | ||
|
||
struct passwd* getpwent(); | ||
void setpwent(); | ||
void endpwent(); | ||
struct passwd* getpwnam(const char* name); | ||
struct passwd* getpwuid(uid_t); | ||
|
||
__END_DECLS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,3 @@ | |
#define __BEGIN_DECLS | ||
#define __END_DECLS | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include <mman.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
#include <LibC/unistd.h> | ||
#include <LibC/stdio.h> | ||
#include <LibC/pwd.h> | ||
|
||
int main(int c, char** v) | ||
{ | ||
uid_t uid = getuid(); | ||
gid_t gid = getgid(); | ||
pid_t pid = getpid(); | ||
printf("uid=%u, gid=%u, pid=%u\n", uid, gid, pid); | ||
|
||
struct passwd* pw = getpwuid(uid); | ||
|
||
printf("uid=%u(%s), gid=%u\n", uid, pw ? pw->pw_name : "n/a", gid); | ||
return 0; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.