Linux Programming-Memoirs Three

Source: Internet
Author: User
Tags string format

= = System Environment = = =
#include <unistd.h>
pid_t getpid (void);
Get the process ID number of the current process and always succeed

#include <stdlib.h>
Char *getenv (const char *name);
Gets the value of the given environment variable name, and returns a null value if there is no variable

#include <stdlib.h>
int putenv (char *string);
Add a new environment variable with the passed in string format: "Name=value"
The incoming string becomes part of the system environment variable, which means that the string cannot be a local variable
Or an automatic variable, the error returns a value of 1, adding a successful return of 0

#include <stdlib.h>
int setenv (const char *name, const char *value, int overwrite);
As above, but more secure than putenv, the function allocates space in the environment variable table, adding the
environment variable, if overwrite is not 0, an environment variable with the same name will be overwritten, and 0 indicates
Success,-1 means error

#include <stdlib.h>
int unsetenv (const char *name);
Deleting an environment variable, returning 0 indicates that the deletion succeeded, 1 indicates that the deletion failed, or the environment variable is not

= = memory Allocation = = =
#include <stdlib.h>
void *malloc (size_t size);
Allocates a size-sized memory and returns an address pointer to that memory, returning NULL to indicate an allocation error

#include <stdlib.h>
void free (void *ptr);
Free memory, the same pointer cannot be freed two times, otherwise the segment error

Include <stdlib.h>
void *calloc (size_t numitems, size_t size);
Allocates and returns a NumItems size of contiguous memory, and empties memory, byte full
is a value of 0, returning NULL indicates an allocation error

Include <stdlib.h>
void *realloc (void *ptr, size_t size);
Re-set the size of the allocated PTR memory back to the new memory address
Pointer, returning 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);
Use the user name or user ID number to get the account information of the system user, the success is returned
A pointer that returns null indicates an error and gets information from the/etc/passwd file

#include <grp.h>
struct group *getgrnam (const char *name);
struct group *getgrgid (gid_t GID);
Use the group name or group ID number to get information about the system user group and successfully return a
Pointer, returning NULL indicates an error

#include <pwd.h>
struct passwd *getpwent (void);
void setpwent (void);
void endpwent (void);
Getpwent gets the user's information sequentially from the/etc/passwd file and returns a
pointer, which returns NULL for completion, requires a loop to call the function
After Getpwent gets finished, call the Endpwent function to close the cleanup resource
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);
Get the shadow password information for an account

struct spwd *getspent (void);
void setspent (void);
void endspent (void);
Functions with Getpwent function series, one by one to get account password information

#include <unistd.h>
uid_t getuid (void);
uid_t geteuid (void);
gid_t getgid (void);
gid_t getegid (void);
Get the account User ID, group ID, valid user ID, valid group ID
The user ID refers to the actual owner who runs the program
Group ID refers to the actual group of users running the program
A valid user ID is a user ID that performs a certain permission task
Valid group ID refers to the user group ID that performs a certain permission task

Include <unistd.h>
int setuid (uid_t uid);
int Setgid (gid_t gid);
Set the ID of the user ID/group, return 0 successfully, error 1

#include <unistd.h>
int Seteuid (uid_t euid);
int Setegid (gid_t egid);
Set the ID of the user ID/group with valid permissions, return 0 successfully, error 1

Linux Programming-Memoirs Three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.