Skip to content

Commit

Permalink
mock pwd.h
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Dec 17, 2023
1 parent 8f37aa1 commit 603d564
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup_wasi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ mv libgit2-1.7.1 libgit2
rm libgit2.tar.gz
rm libgit2/src/libgit2/transports/http.c
rm libgit2/src/libgit2/streams/socket.c
rm libgit2/src/libgit2/sysdir.c

cp -r libgit2patchedfiles/examples/* libgit2/examples/
1 change: 1 addition & 0 deletions wasibuild/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if [ "$1" == "Release" ]; then
EXTRA_CMAKE_C_FLAGS="-Oz"
fi

clang --target=wasm32-wasi --sysroot=wasi-sdk-20.0/share/wasi-sysroot -D_WASI_EMULATED_MMAN -Iwasi_mocks -c wasi_mocks/pwd.c -o wasi_mocks/pwd.o
cmake -DCMAKE_TOOLCHAIN_FILE=`pwd`/wasi_toolchain.cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_C_FLAGS="$EXTRA_CMAKE_C_FLAGS" -DREGEX_BACKEND=regcomp -DSONAME=OFF -DUSE_HTTPS=OFF -DBUILD_SHARED_LIBS=OFF -DUSE_THREADS=OFF -DUSE_SSH=OFF -DBUILD_CLAR=OFF -DBUILD_EXAMPLES=ON ../libgit2
make lg2

1 change: 1 addition & 0 deletions wasibuild/wasi_mocks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.o
31 changes: 31 additions & 0 deletions wasibuild/wasi_mocks/pwd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <sys/types.h>
#include <stddef.h>

// Mock structure similar to 'struct passwd' in pwd.h
struct passwd {
char *pw_name; // user's name
char *pw_passwd; // user's password
uid_t pw_uid; // user's user ID
gid_t pw_gid; // user's group ID
char *pw_gecos; // user's real name
char *pw_dir; // user's home directory
char *pw_shell; // user's shell program
};

// Mock function similar to 'getpwnam' in pwd.h
struct passwd *getpwnam(const char *name) {
static struct passwd mock_user;

// Hardcoded user information
mock_user.pw_name = "mockuser";
mock_user.pw_passwd = "mockpassword";
mock_user.pw_uid = 1000;
mock_user.pw_gid = 1000;
mock_user.pw_gecos = "Mock User";
mock_user.pw_dir = "/home/mockuser";
mock_user.pw_shell = "/bin/sh";

return &mock_user;
}

// ... other mock functions as needed ...
20 changes: 20 additions & 0 deletions wasibuild/wasi_mocks/pwd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef MOCK_PWD_H
#define MOCK_PWD_H

#include <sys/types.h>

// Mock structure similar to 'struct passwd' in pwd.h
struct passwd {
char *pw_name; // user's name
char *pw_passwd; // user's password
uid_t pw_uid; // user's user ID
gid_t pw_gid; // user's group ID
char *pw_gecos; // user's real name
char *pw_dir; // user's home directory
char *pw_shell; // user's shell program
};

// Mock function declaration
struct passwd *getpwnam(const char *name);

#endif // MOCK_PWD_H
2 changes: 1 addition & 1 deletion wasibuild/wasi_toolchain.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SET(CMAKE_C_COMPILER clang)
SET(CMAKE_FIND_ROOT_PATH ${CMAKE_CURRENT_LIST_DIR}/wasi-sdk-20.0/share/wasi-sysroot/)
SET(CMAKE_SYSROOT ${CMAKE_CURRENT_LIST_DIR}/wasi-sdk-20.0/share/wasi-sysroot/)
set(CMAKE_C_FLAGS "--target=wasm32-wasi -D_WASI_EMULATED_MMAN -lwasi-emulated-mman ${CMAKE_C_FLAGS}")
set(CMAKE_C_FLAGS "--target=wasm32-wasi -I${CMAKE_CURRENT_LIST_DIR}/wasi_mocks -D_WASI_EMULATED_MMAN -lwasi-emulated-mman ${CMAKE_C_FLAGS}")

set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES "${CMAKE_SYSROOT}/include")

Expand Down

0 comments on commit 603d564

Please sign in to comment.