/*********************************************************************************
|
* Copyright: (C) 2025 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: myrm.c
|
* Description: This file is unlink() example.
|
*
|
* Version: 1.0.0(10/15/2025)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "10/15/2025 03:01:48 PM"
|
*
|
********************************************************************************/
|
|
#include <stdio.h>
|
#include <unistd.h>
|
#include <sys/stat.h>
|
#include <errno.h>
|
#include <string.h>
|
|
int main(int argc, char *argv[])
|
{
|
struct stat file_stat;
|
const char *file_path;
|
|
if (argc != 2)
|
{
|
fprintf(stderr, "Usage: %s <file_path>\n", argv[0]);
|
return 1;
|
}
|
|
file_path = argv[1];
|
|
if (lstat(file_path, &file_stat) == -1)
|
{
|
fprintf(stderr, "Cannot access file '%s': %s\n", file_path, strerror(errno));
|
return 1;
|
}
|
|
if (!S_ISREG(file_stat.st_mode))
|
{
|
fprintf(stderr, "'%s' is not a regular file\n", file_path);
|
return 1;
|
}
|
|
if (unlink(file_path) == -1)
|
{
|
fprintf(stderr, "Delete file '%s' falied: %s\n", file_path, strerror(errno));
|
return 1;
|
}
|
|
printf("File '%s' deleted successfully\n", file_path);
|
return 0;
|
}
|