Linux programming-memoir 4: A memoir of linux Programming

Source: Internet
Author: User
Tags epoch time inotify lstat

Linux programming-memoir 4: A memoir of linux Programming

===== Time ====
There are two types of reference for Program Computing Time:
1. the time on the wall, that is, the time used in real life.
2. The processing time, that is, the amount of time the CPU executes the program.

# Include <sys/time. h>
Int gettimeofday (struct timeval * TV, struct timezone * tz );
Get the standard calendar time, 0 is returned for success, and-1 is returned for error.

# Include <time. h>
Time_t time (time_t * timep );
Returns the number of seconds since the Epoch time.-1 indicates an error.

# Include <time. h>
Char * ctime (const time_t * timep );
Convert the current time to a simple string description. The format is similar
Wed Jun 8 14:22:34 2011, NULL returned indicates an error

# Include <time. h>
Struct tm * gmtime (const time_t * timep );
Struct tm * localtime (const time_t * timep );
The former converts the time_t type value to the UTC standard time structure, and the latter converts to the localization time result.
Return NULL indicates processing error

# Include <time. h>
Time_t mktime (struct tm * timeptr );
Converts the corresponding time structure to the number of seconds since the Epoch time, and-1 indicates an error.

# Include <time. h>
Size_t strftime (char * outstr, size_t maxsize, const char * format,
Const struct tm * timeptr );
Converts a time structure to a string in the format and writes it to outstr.
Returns the actual number of bytes written.-1 indicates an error.

# Define _ XOPEN_SOURCE
# Include <time. h>
Char * strptime (const char * str, const char * format, struct tm * timeptr );
Converts a string that meets the time format into a time structure and returns the next
The character address that is not processed. NULL indicates an error.

==== System resources ====
# Include <unistd. h>
Long sysconf (int name );
The system configuration information is obtained at runtime. The returned value is determined by the name, and-1 indicates an error.

# Include <unistd. h>
Long pathconf (const char * pathname, int name );
Long fpathconf (int fd, int name );
Get the maximum length of the file path.-1 indicates an error.

# Include <sys/utsname. h>
Int uname (struct utsname * utsbuf );
Obtain information about the operating system.-1 indicates an error.

==== File Buffer ====
# Include <stdio. h>
Int setvbuf (FILE * stream, char * buf, int mode, size_t size );
Set the buffer size of the file stream.-1 indicates an error.
The value of mode is as follows <default value: _ IOFBF>
_ IOFBF (full buffer) when the buffer is empty, data is read or written from the stream
_ IOLBF (row buffering) reads a row of data from the stream each time or writes a row of data to the stream
_ IONBF (no buffer) reads/writes data directly without buffering. Ignore the buf and size parameters.

# Include <stdio. h>
Void setbuf (FILE * stream, char * buf );
If buf is NULL, the file stream does not need to be buffered; otherwise, the full buffer mode and buf size are set.
It must be the size of the BUFSIZE.

# Define _ BSD_SOURCE
# Include <stdio. h>
Void setbuffer (FILE * stream, char * buf, size_t size );
The same as above, but you can specify the buffer size as size.

# Include <stdio. h>
Int fflush (FILE * stream );
Refresh the buffer and synchronize the buffered content to the file stream. EOF indicates an error and 0 indicates successful.

# Include <stdio. h>
Int fileno (FILE * stream );
Get the file descriptor of the file stream.-1 indicates an error.
FILE * fdopen (int fd, const char * mode );
Get the file stream of the file descriptor. NULL indicates an error.

# Include <sys/mount. h>
Int mount (const char * source, const char * target,
Const char * fstype, unsigned long mountflags, const void * data );
Mount a file system. Similar to the mount command, source indicates the file system to be mounted, and target indicates
Mount point. fstype indicates the Mount source file system type. mountflags indicates the mount file
System-related option parameters. If the return value is 0, the operation succeeds. If the return value is-1, the operation fails.

# Include <sys/mount. h>
Int umount (const char * target );
After the mount point is detached, 0 is returned, and-1 fails.

# Include <sys/stat. h>
Int stat (const char * pathname, struct stat * statbuf );
Int lstat (const char * pathname, struct stat * statbuf );
Int fstat (int fd, struct stat * statbuf );
Get the object property information. If 0 is returned, it indicates that the object is successful, and-1 is failed. The difference between lstat and stat is that
If the file is a linked file, lstat obtains the information of the linked file instead of the linked object file.

# Include <unistd. h>
Int chown (const char * pathname, uid_t owner, gid_t group );
# Define _ xopen _source 500/* Or: # define _ BSD_SOURCE */
# Include <unistd. h>
Int lchown (const char * pathname, uid_t owner, gid_t group );
Int fchown (int fd, uid_t owner, gid_t group );
Set the permission of the file, which user belongs to, and which group, and return 0 success,-1 failure. Differences between lchown and chow
When a file is a linked file, lchown sets the link File Permission, rather than the linked object file

# Include <unistd. h>
Int access (const char * pathname, int mode );
Test whether the file can be accessed with the mode permission. If the return value is 0, the file can be accessed. If the return value is-1, the file cannot be accessed.
The permission information of mode is as follows:
Does the F_ OK file exist?
Is the R_ OK file readable?
Is the W_ OK file writable?
Can the X_ OK file be executed?

# Include <sys/stat. h>
Mode_t umask (mode_t mask );
Sets the File Permission mask and returns the last set mask value. Generally, you can save the previous
Mask value, which is restored when necessary

Include <unistd. h>
Int link (const char * oldpath, const char * newpath );
Create a hard link to the file, and return 0 success,-1 failed

# Include <unistd. h>
Int symlink (const char * filepath, const char * linkpath );
If the soft link of a file is created, 0 is returned, and-1 fails.

# Include <unistd. h>
Int unlink (const char * pathname );
Delete A File Link. If it is the last hard link of the file, delete the file.
0 is returned, and-1 is returned.

NOTE: If an opened file is deleted, it can only be disabled by all file descriptors that reference it.
Will be deleted

# Include <stdio. h>
Int rename (const char * oldpath, const char * newpath );
If a file is renamed, 0 is returned, and-1 fails.

Nclude <sys/stat. h>
Int mkdir (const char * pathname, mode_t mode );
Create a folder with the given permissions, and return 0 success,-1 failed

# Include <unistd. h>
Int rmdir (const char * pathname );
Delete an empty folder and return 0 to success.-1 to fail. Please note that this function
You cannot delete subfolders or directories containing files.

# Include <stdio. h>
Int remove (const char * pathname );
If a file (unlink) or an empty folder (rmdir) is deleted, 0 is returned and-1 fails.
Mainly to implement C-standard library functions

# Include <dirent. h>
DIR * opendir (const char * dirpath );
# Include <dirent. h>
DIR * fdopendir (int fd );
Open a directory, return the directory structure, and return NULL to indicate failure

# Include <dirent. h>
Struct dirent * readdir (DIR * dirp );
Read the contents in the directory one by one and return an internal directory
Entry information. If NULL is returned, it indicates that the operation failed or ended.

# Include <dirent. h>
Void rewinddir (DIR * dirp );
Resets the read offset of the directory structure, pointing to the first directory entry information

# Include <dirent. h>
Int closedir (DIR * dirp );
Close the directory, return 0 success,-1 failed

# Include <dirent. h>
Int dirfd (DIR * dirp );
Returns the file descriptor of the Directory stream. The value 0 indicates success, and the value-1 indicates failure.

# Include <unistd. h>
Char * getcwd (char * cwdbuf, size_t size );
Obtains information about the current directory. If cwdbuf is returned successfully, NULL is returned.

# Include <unistd. h>
Int chdir (const char * pathname );
# Define _ xopen _source 500/* Or: # define _ BSD_SOURCE */
# Include <unistd. h>
Int fchdir (int fd );
If the current directory is changed, 0 or-1 is returned.

# Define _ BSD_SOURCE
# Include <unistd. h>
Int chroot (const char * pathname );
If the root directory is changed, 0 is returned, and-1 fails.

# Include <stdlib. h>
Char * realpath (const char * pathname, char * resolved_path );
Obtain the actual file path of the soft link. The resolved_path must be at least the size of PATH_MAX.

# Include <libgen. h>
Char * dirname (char * pathname );
Char * basename (char * pathname );
Obtain the directory information or file name information of a path.

=== Monitoring File System ====
Currently, inotify APIs are used to monitor File System Operation events.
1. Use the inotify_init function to create an inotify instance
2. Use the inotify_add_watch function to add files or directories to be monitored. This function
Returns an inotify file descriptor.
3. Read time from inotify file descriptor for processing
4. After processing, you can remove all monitoring objects.

Note:
A) inotify is not recursive. The monitored directory does not monitor the seeds under the directory.
Directory or file
B) inotify can be obtained using functions such as select (), poll (), and epoll () For IO.
Monitored event information

# Include <sys/inotify. h>
Int inotify_init (void );
Initialize inotify and return the inotify file descriptor.-1 indicates an error.

# Include <sys/inotify. h>
Int inotify_add_watch (int fd, const char * pathname, uint32_t mask );
Add a monitored file or directory object and return the monitored file descriptor.-1 indicates an error.

# Include <sys/inotify. h>
Int inotify_rm_watch (int fd, uint32_t wd );
If the Monitored object is deleted, 0 is returned and-1 fails.

 

Related Article

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.