Linux programming-memoir 3: A memoir of linux Programming
=== System environment ====
# Include <unistd. h>
Pid_t getpid (void );
Returns the ID of the current process, which is always successful.
# Include <stdlib. h>
Char * getenv (const char * name );
Obtain the value of the given environment variable name. If this variable is not present, NULL is returned.
# Include <stdlib. h>
Int putenv (char * string );
Add a new environment variable in the input string format: "name = value"
The input string is part of the system environment variable. That is to say, string cannot be a local variable.
Or an automatic variable. If an error occurs, the value-1 is returned. If the value is added successfully, 0 is returned.
# Include <stdlib. h>
Int setenv (const char * name, const char * value, int overwrite );
It is more secure than putenv. This function allocates space in the environment variable table and adds
Environment variable. If overwrite is not 0, the environment variable with the same name already exists will be overwritten, and 0 is returned
Success.-1 indicates an error.
# Include <stdlib. h>
Int unsetenv (const char * name );
Delete an environment variable. If 0 is returned, the deletion is successful. If-1 is returned, the deletion fails or the environment variable does not exist.
=== Memory allocation ===
# Include <stdlib. h>
Void * malloc (size_t size );
Allocate the size of memory, and return the address pointer to the memory. return NULL, indicating that the allocation is incorrect.
# Include <stdlib. h>
Void free (void * ptr );
Release memory. The same pointer cannot be released twice; otherwise, the segment is incorrect.
Include <stdlib. h>
Void * calloc (size_t numitems, size_t size );
Allocate and return numitems continuous memory of size, and clear the memory. Set all bytes.
The value is 0. If NULL is returned, the allocation error occurs.
Include <stdlib. h>
Void * realloc (void * ptr, size_t size );
Reset the size of allocated ptr memory to size and return the new memory address.
Pointer. NULL indicates an error.
==== User and group ====
/Etc/passwd User Password File
/Etc/shadow user shadow password file
/Etc/group user group information file
# Include <pwd. h>
Struct passwd * getpwnam (const char * name );
Struct passwd * getpwuid (uid_t uid );
If the user name or user ID is used to obtain the account information of the System user, success is returned.
A pointer. NULL indicates an error. The obtained information is from the/etc/passwd file.
# Include <grp. h>
Struct group * getgrnam (const char * name );
Struct group * getgrgid (gid_t gid );
If the system user group information is obtained by using the group name or group ID,
Pointer. NULL indicates an error.
# Include <pwd. h>
Struct passwd * getpwent (void );
Void setpwent (void );
Void endpwent (void );
Getpwent obtains the user information in order from the/etc/passwd file.
Pointer. If NULL is returned, the function is retrieved and needs to be called cyclically.
After getpwent is obtained, call the endpwent function to disable resource cleanup.
The setpwent function is used to reset the current user information to the first user record of the file.
# Include <shadow. h>
Struct spwd * getspnam (const char * name );
Obtains the shadow password of an account.
Struct spwd * getspent (void );
Void setspent (void );
Void endspent (void );
Functions are the same as those of the getpwent function series. You can obtain the account password information one by one.
# Include <unistd. h>
Uid_t getuid (void );
Uid_t geteuid (void );
Gid_t getgid (void );
Gid_t getegid (void );
Obtains the user ID, group ID, valid user ID, and valid group ID of an account.
The User ID is the actual owner of the program.
The Group ID is the actual user group that runs the program.
A valid user ID is the user ID that executes tasks with certain permissions.
A valid group ID is the ID of the user group that executes tasks with certain permissions.
Include <unistd. h>
Int setuid (uid_t uid );
Int setgid (gid_t gid );
Set the user ID/group ID. 0 is returned for success, and-1 is returned for error.
# Include <unistd. h>
Int seteuid (uid_t euid );
Int setegid (gid_t egid );
Set the user ID/group ID with valid permissions. 0 is returned for success and-1 is returned for error.