nuttx/libs/libc/pwd/lib_getpwbufr.c
dongjiuzhu1 5826e8eeb1 libc/passwd: add pw_gecos field(userinfo)
The pw_gecos field is not specified in POSIX, but is
present on most implementations.
passwd file format:
name:uid:gid:gecos:dir:shell
ex: gdm127:133:Gnome Display Manager:/var/lib/gdm3:/bin/false

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2022-12-30 00:21:15 +08:00

99 lines
3.5 KiB
C

/****************************************************************************
* libs/libc/pwd/lib_getpwbufr.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pwd.h>
#include "pwd/lib_pwd.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: getpwbuf_r
*
* Description:
* libc/grp internal helper function for getpwuid_r and getpwnam_r to setup
* the caller supplied 'pwd' and 'buf' buffers once a matching entry has
* been found.
*
* Input Parameters:
* uid - Value to set the passwd structure's pw_uid field to.
* gid - Value to set the passwd structure's pw_gid field to.
* name - Value to set the passwd structure's pw_name field to.
* gecos - Value to set the passwd structure's pw_gecos field to.
* dir - Value to set the passwd structure's pw_dir field to.
* shell - Value to set the passwd structure's pw_shell field to.
* pwd - Pointer to the space to store the retrieved passwd structure
* in.
* buf - String fields pointed to by the passwd struct are stored here.
* buflen - The length of buf in bytes.
* result - Pointer to the resulting passwd struct, or NULL in case of
* fail.
*
* Returned Value:
* On success getpwgid_r returns 0 and sets *result to pwd. In case of
* failure an error number is returned.
*
****************************************************************************/
int getpwbuf_r(uid_t uid, gid_t gid, FAR const char *name,
FAR const char *gecos, FAR const char *dir,
FAR const char *shell, FAR struct passwd *pwd,
FAR char *buf, size_t buflen, FAR struct passwd **result)
{
size_t reqdlen;
reqdlen = strlen(name) + 1 + strlen(gecos) + 1 + strlen(dir) + 1 +
strlen(shell) + 1;
if (buflen < reqdlen)
{
/* Insufficient buffer space supplied. */
*result = NULL;
return ERANGE;
}
pwd->pw_name = buf;
pwd->pw_gecos = &buf[strlen(name) + 1];
pwd->pw_dir = &buf[strlen(name) + strlen(gecos) + 2];
pwd->pw_shell = &buf[strlen(name) + strlen(gecos) + strlen(dir) + 3];
pwd->pw_uid = uid;
pwd->pw_gid = gid;
strcpy(pwd->pw_name, name);
strcpy(pwd->pw_gecos, gecos);
strcpy(pwd->pw_dir, dir);
strcpy(pwd->pw_shell, shell);
*result = pwd;
return 0;
}