APUE course source code
guowenxue
yesterday 7b55c92f8d1401a93c8fd8e342da271dce742000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  access.c
 *    Description:  This file is access() example
 *                 
 *        Version:  1.0.0(10/15/2025)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/15/2025 02:43:08 PM"
 *                 
 ********************************************************************************/
 
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
    const char *file = argv[1];
 
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s <file_path>\n", argv[0]);
        return 1;
    }
 
    if (access(file, F_OK) == -1)
    {
        fprintf(stderr, "cannot access '%s': %s\n", file, strerror(errno));
        return 1;
    }
 
    if (access(file, R_OK) == 0)
    {
        printf("Read  OKAY\n");
    }
    else
    {
        printf("Read  FAIL: (%s)\n", strerror(errno));
    }
 
    if (access(file, W_OK) == 0)
    {
        printf("Write OKAY\n");
    }
    else
    {
        printf("Write FAIL: (%s)\n", strerror(errno));
    }
 
    if (access(file, X_OK) == 0)
    {
        printf("Exec  OKAY\n");
    }
    else
    {
        printf("Exec  FAIL: (%s)\n", strerror(errno));
    }
 
    return 0;
}