putspent()
QNX SDP8.0C Library ReferenceAPIDeveloper
Put an entry into the shadow password database
Synopsis:
#include <sys/types.h>
#include <shadow.h>
int putspent( const struct spwd* p,
              FILE* fp );
Arguments:
- p
 - A pointer to an spwd structure that contains the entry that you want to write.
 - fp
 - The stream that you want to write the entry into.
 
Library:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
Description:
The putspent() function writes a shadow password entry into the specified file stream. This function is the inverse of getspent().
Given a pointer to an spwd structure created by the
getspent() or the
getspnam() routine,
putspent() writes a line that matches the format of
</etc/shadow> on the stream fp.
The spwd structure contains the following members:
char    *sp_namp;     /* name */
char    *sp_pwdp;     /* encrypted password */
long    sp_lstchg;    /* last changed */
long    sp_max;       /* #days (min) to change */
long    sp_min;       /* #days (max) to change */
long    sp_warn;      /* #days to warn */
long    sp_inact;     /* #days of inactivity */
long    sp_expire;    /* date to auto-expire */
long    sp_flag;      /* reserved */
If the sp_min, sp_max, sp_lstchg, sp_warn, sp_inact, or sp_expire field of the structure is -1, or if sp_flag is 0, the corresponding </etc/shadow> field is cleared.
Returns:
0 on success, -1 on failure.
Errors:
- EINVAL
 - The username field in the provided spwd structure is NULL.
 
In addition to the errno settings listed above, a call to putspent() can result in errno being set by any of the following functions:
Examples:
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>
/*
 * This program adds a user and password to a temporary file
 * which can then be used with fgetspent() (of course the password
 * string should be encrypted already)
 */
int main( int argc, char** argv )
{
    FILE* fp;
    struct spwd sp;
    char pwbuf[80], nambuf[80];
    memset(&sp, 0, sizeof(sp));
    if (argc < 2) {
        printf("%s filename \n", argv[0]);
        return(EXIT_FAILURE);
    }
    if (!(fp = fopen(argv[1], "w"))) {
        fprintf(stderr, "Can't open file %s \n", argv[1]);
        perror("Problem ");
        return(1);
    }
    printf("Enter a userid: ");
    if (!gets(nambuf)) {
        fprintf(stderr, "Can't get username string\n");
    }
    sp.sp_namp = nambuf;
    printf("Enter a password: ");
    if (!gets(pwbuf)) {
        fprintf(stderr, "Can't get password string\n");
    }
    sp.sp_pwdp = pwbuf;
    putspent(&sp, fp);
    fclose(fp);
    return( EXIT_SUCCESS );
}
Classification:
| Safety: | |
|---|---|
| Cancellation point | Yes | 
| Signal handler | No | 
| Thread | No | 
Page updated: 
