Explain the use of symlink of function and readlink of function in C language

  • 2020-04-02 03:21:38
  • OfStack

C language symlink() function: establish a file symbolic connection
The header file:


 #include <unistd.h>

Definition function:


int symlink(const char * oldpath, const char * newpath);

Symlink () creates a new connection (symbolic connection) with the name specified by newpath to an existing file specified by oldpath.

Return value: 0 on success, -1 on failure, error in errno.

Error code:
1. The file system referred to by EPERM parameter oldpath and newpath does not support symbolic connection.
2. The file EROFS wants to test the write permission exists in the read-only file system.
3. The EFAULT parameter oldpath or newpath pointer exceeds the accessible memory space.
4. The ENAMETOOLONG parameter oldpath or newpath is too long.
5. ENOMEM is out of core memory.
6. The file name referred to by the EEXIST parameter newpath already exists.
7. The file to which the EMLINK parameter oldpath refers has reached the maximum number of connections.
8. ELOOP parameter pathname has too many symbolic connection problems.
9. Insufficient space for ENOSPC file system.
EIO I/O access error.

sample


#include <unistd.h>
main()
{
  symlink("/etc/passwd", "pass");
}

C readlink() function: gets the file that the symbolic link refers to
The header file:


 #include <unistd.h>

Definition function:


int readlink(const char * path, char * buf, size_t bufsiz);

Readlink () stores the symbolic connection contents of the parameter path into the memory space indicated by the parameter buf. The returned contents do not end in NULL, but return the number of characters in the string. If the parameter bufsiz is less than the length of the symbolic connection, the excessively long contents will be truncated.

Return value: the file path string referred to by the symbolic connection is passed on success, -1 is returned on failure, and the error code is stored in errno.

Error code:
1. When EACCESS takes the file, it is refused, and the permission is not enough.
2. EINVAL parameter bufsiz is negative.
EIO I/O access error.
4. ELOOP has too many symbolic links to open.
5. The path name of the ENAMETOOLONG parameter path is too long.
6. The file specified by the ENOENT parameter path does not exist.
7. ENOMEM is out of core memory.
8. The directory in the ENOTDIR path exists but is not a real directory.


Related articles: