Linux network programming learning notes

Keywords: C Linux

1. bzero() function: zero the first n bytes of the specified string s.

#include <string.h> 
void bzero(void *s, int n);
2. memset() function: replace the memory unit of the first n bytes pointed to by the pointer variable s with an "integer" c, and directly operate the memory space. It is commonly used for memory initialization
# include <string.h>
void *memset(void *s, int c, unsigned long n);
3. atoi() function: converts a string into an integer and returns the converted integer. If str cannot be converted into int or str is an empty string, it returns 0
#include <stdlib.h>
int atoi (const char * str);

The atoi() function scans the str string of the parameter and skips the preceding white space characters (such as spaces, tab indents, etc.) isspace() Function) until it meets a number or positive or negative symbol, and then it ends when it meets the end of a non number or string ('\ 0'), and returns the result.

4. strpbrk() function: retrieves the first character in the string str1 that matches the character in the string str2, and returns a pointer to the position of the character in str1 if any.

#include <string.h>
char *strpbrk(const char *str1, const char *str2);

This function checks the characters in the string str1 in turn. When the checked character is also included in the string str2, it stops the verification and returns the character position. The null character null(' null ('\ 0'') is not included. If it is not found, it returns a null pointer.

5. basename() function: intercept the last file or path name in the path to remove the directory part. When successful, it returns the last file or path name pointer of the de directory part in the intercepted path; Failure returns NULL.

#include <libgen.h>
char *basename(char *path);

6. stat() function: get the file information through the file name filename and save it in the structure stat referred to by buf. Success returns 0, failure returns - 1, and the error code is stored in errno

#include <sys/stat.h>
#include <unistd.h>
int stat(const char *file_name, struct stat *buf);

errno error code:

    ENOENT         parameter file_name The specified file does not exist
    ENOTDIR        The directory in the path exists but is not a real directory
    ELOOP          The file you want to open has too many symbolic connections. The upper limit is 16 symbolic connections
    EFAULT         parameter buf Is an invalid pointer to memory space that cannot exist
    EACCESS        Access to the file was denied
    ENOMEM         Insufficient core memory
    ENAMETOOLONG   parameter file_name The path name for is too long

stat structure:

struct stat
 {
  dev_t st_dev;            //Equipment number of the document
  ino_t st_ino;            //node
  mode_t st_mode;          //File type and access rights
  nlink_t st_nlink;        //The number of hard connections to the file. The value of the newly established file is 1
  uid_t st_uid;            //User ID
  gid_t st_gid;            //Group ID
  dev_t st_rdev;           //(equipment type) if this document is an equipment document, its equipment number
  off_t st_size;           //File bytes (file size)
  unsigned long st_blksize;//Block size (I/O buffer size of the file system)
  unsigned long st_blocks; //Number of blocks
  time_t st_atime;         //Last visit time
  time_t st_mtime;         //Last modification time
  time_t st_ctime;         //Last change time (referring to attribute)
};

st_mode: file type and access rights

    S_IFMT   0170000         Bit mask for file type
    S_IFSOCK 0140000         scoket
    S_IFLNK 0120000          Symbolic connection
    S_IFREG 0100000          General documents
    S_IFBLK 0060000          block device 
    S_IFDIR 0040000          catalogue
    S_IFCHR 0020000          Character device
    S_IFIFO 0010000          fifo
    S_ISUID 04000            Document(set user-id on execution)position
    S_ISGID 02000            Document(set group-id on execution)position
    S_ISVTX 01000            Document sticky position
    S_IRUSR(S_IREAD) 00400   The file owner has read permission
    S_IWUSR(S_IWRITE)00200   The file owner has write permission
    S_IXUSR(S_IEXEC) 00100   The file owner has executable rights
    S_IRGRP 00040            The user group has read permission
    S_IWGRP 00020            The user group has writable permissions
    S_IXGRP 00010            The user group has executable rights
    S_IROTH 00004            Other users have read permission
    S_IWOTH 00002            Other users have write permission
    S_IXOTH 00001            Other users have executable rights

7. fstat() function: obtain the file state from the file descriptor, and copy the file state pointed to by the parameter filedes to the structure (struct stat) pointed to by the parameter buf

#include <sys/stat.h>
#include <unistd.h>
int stat(int fd, struct stat *buf);

fstat() has exactly the same function as stat(), except that the parameter passed in is an open file descriptor.

8. open() function: create a file descriptor. Other functions can read and write the specified file through the file descriptor.

#include <sys/types.h>
#incldue <sys/stat.h>
int open(const char*pathname, int flags);
int open(const char*pathname, int flags, mode_t mode);

mode:

  O_RDONLY:   Read only open
  O_WRONLY:   Write only open
  O_RDWR:     Read, write on
  O_CREAT:    If the file does not exist, create it and use mode Options. To indicate access to the new file
  O_APPEND:   Append write. If the file already has content, the data written in the file will be appended to the end of the file without overwriting the original content

9. close() function: closes an open file

#include <unistd.h>
int close(int fd);

10. umask() function: change file permissions, often used in combination with the open() function

#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t mask);

The umask() function will set the system umask value to the value after the parameter mask&0777, and then return the previous umask value. When using open() to create a new file, the parameter mode is not the permission to create the file, but the permission value of (mode & ~ umask).

11. exit() function: terminates the process

#include <stdlib.h>
void exit(int status);
exit(1);              /*Abnormal exit. If it is not 0, it means abnormal exit*/
exit(0);              /*Normal exit*/

Posted by Anthony1312002 on Sun, 17 Oct 2021 10:05:18 -0700