/*********************************************************************************
|
* 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;
|
}
|