[Database functions] access functions in linux

Source: Internet
Author: User
Access Function header file: unistd. h function: determine the access permissions of files or folders. Check the access method of a file, such as read-only and write-only. If the specified access method is valid, the function returns 0; otherwise, the function returns-1. Usage: intaccess (constchar * filenpath, intmode); or int_acc

Access Function header file: unistd. h function: determine the access permissions of files or folders. Check the access method of a file, such as read-only and write-only. If the specified access method is valid, the function returns 0; otherwise, the function returns-1. Usage: int access (const char * filenpath, int mode); or int _ acc

Access Function

Header file: unistd. h

Function: determine the access permissions of files or folders. Check the access method of a file, such as read-only and write-only. If the specified access method is valid, the function returns 0; otherwise, the function returns-1.

Usage: int access (const char * filenpath, int mode); or int _ access (const char * path, int mode );

Parameter description:

Filenpath

File or folder path. The current directory uses the file or folder name directly.

Note: When this parameter is a file, the access function can use all the values of the mode parameter. When this parameter is a folder, the access function value can determine whether the folder exists. In win nt, all folders have read and write permissions.

Mode

Pattern to be judged

The pre-definition in the header file unistd. h is as follows:

# Define R_ OK 4/* Test for read permission .*/

# Define W_ OK 2/* Test for write permission .*/

# Define X_ OK 1/* Test for execute permission .*/

# Define F_ OK 0/* Test for existence .*/

The specific meaning is as follows:

R_ OK only checks whether the user has the read permission

W_ OK only checks whether the write permission is available

X_ OK

F_ OK: only determines whether the object exists.

Example of the access function program: (refer to the Baidu example to add all four methods)

# Include
# Include

Int file_exists (char * filename)
{
/* Determine whether the file exists */
Return (access (filename, F_ OK) = 0 );
}

Int file_read (char * filename)
{
/* Determine whether the object is readable */
Return (access (filename, R_ OK) = 0 );
}

Int file_write (char * filename)
{
/* Determine whether a file can be written */
Return (access (filename, W_ OK) = 0 );
}

Int file_execute (char * filename)
{
/* Determine whether the file is executable */
Return (access (filename, X_ OK) = 0 );
}

Int main (void)
{
Printf ("Does NOTEXIST. FIL exist: % s \ n ",
File_exists ("NOTEXISTS. FIL ")? "YES": "NO ");

Printf ("Does NOTEXIST. FIL read: % s \ n ",
File_read ("NOTEXISTS. FIL ")? "YES": "NO ");

Printf ("Does NOTEXIST. FIL write: % s \ n ",
File_write ("NOTEXISTS. FIL ")? "YES": "NO ");

Printf ("Does NOTEXIST. FIL execute: % s \ n ",
File_execute ("NOTEXISTS. FIL ")? "YES": "NO ");

Return 0;
}


To perform self-testing, run the chmod command to modify the text attributes and use ls to view the text attributes.

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.