Question Details

No question body available.

Tags

c pointers

Answers (3)

Accepted Answer Available
Accepted Answer
April 29, 2026 Score: 3 Rep: 71 Quality: High Completeness: 60%

modify the contents of the memory that pwd points to, rather than trying to change the pointer itself.

In a standard getpwnamr implementation, the function populates the pwd structure provided by the caller and sets *result to point to that same structure upon success.

Here is the corrected logic for your win
pwd.h:

int getpwnamr(const char * name, struct passwd * pwd,
               char * buffer, sizet size, struct passwd  result) {

// 1. Populate the struct provided by the caller directly pwd->pwname = getenv("USERNAME"); pwd->pwdir = getenv("PWD"); pwd->pwshell = "powershell.exe"; pwd->pwuid = 1; pwd->pwgid = 1;

// 2. Set the result pointer to point to the now-populated struct if (pwd->pw
name != NULL) { result = pwd; return 0; // Success }

// 3. If something failed, set result to NULL
result = NULL; return -1; }
April 29, 2026 Score: 0 Rep: 125,874 Quality: Medium Completeness: 80%

You've already gotten an answer answering where you went wrong when implementing your current idea for how the function should work. This is mainly focused on changing that idea.

POSIX 2024:

int getpwnamr(const char *name, struct passwd *pwd, char *buffer,
       sizet bufsize, struct passwd result);

The getpwnamr() function shall update the passwd structure pointed to by pwd and store a pointer to that structure at the location pointed to by result. The structure shall contain an entry from the user database with a matching name. Storage referenced by the structure is allocated from the memory provided with the buffer parameter, which is bufsize bytes in size. A null pointer shall be returned at the location pointed to by result on error or if the requested entry is not found.

So, if you want to mimic the real thing, getpwnamr should not allocate memory. It should use the memory provided to it.

It could look like this:

// a helper function to append to buffer
static char add_to_buf(char buffer, sizet size, sizet offset,
                        const char str) {
    char wr = buffer + offset;
    offset += snprintf(wr, size - offset, "%s", str) + 1;

// return pointer to beginning of the new string if it fit in the // remaining buffer return offset < size ? wr : NULL; }

int getpwnam_r(const char name, struct passwd pwd, char buffer, sizet size, struct passwd result) { *result = NULL;

// lookup name, simulated: const char* USER = getenv("USERNAME"); const char* PWD = getenv("PWD");

// check that the user is found. note: not found is not an error // so return 0: if (!USER || !PWD) return 0;

pwd->pwuid = 1; pwd->pwgid = 1;

// fill buffer with the found user information: sizet offset = 0;

// return ERANGE if the information does not fit in buffer: if (!(pwd->pwname = addtobuf(buffer, size, &offset, USER))) return ERANGE; if (!(pwd->pwdir = addtobuf(buffer, size, &offset, PWD))) return ERANGE; if (!(pwd->pwshell = addto_buf(buffer, size, &offset, "powershell.exe"))) return ERANGE;

// success *result = pwd; return 0; }

Demo

April 29, 2026 Score: 0 Rep: 106 Quality: Low Completeness: 30%

To fill in pwd you must de-reference it:

pwd = * result;