删除软连接命令 连接已重置的解决办法
总体概述:在上一篇文章中介绍了如何实现软链接命令并创建软链接 , 在这一章我们介绍如何编写程序,实现删除链接文件程序的方法,需要用到lstat,unlink函数和S_ISLNK宏 。函数语法:lstat函数总体概述:在上一篇文章中介绍了如何实现软链接命令并创建软链接,在这一章我们介绍如何编写程序 , 实现删除链接文件程序的方法,需要用到lstat , unlink函数和S_ISLNK宏 。
函数语法:
lstat函数语法:int lstat(const char *pathname, struct stat *statbuf);
unlink函数语法:int unlink(const char *pathname);
【删除软连接命令 连接已重置的解决办法】函数作用:
lstat函数的作用:获取指定文件的信息 。
unlink函数的作用:从文件系统中删除一个名称 。
S_ISLNK宏:判断是否为链接文件
参数介绍:
lstat函数参数介绍:
- 输入参数:pathname => 文件路径名 。输出参数:buf => 文件信息缓存返回值:成功:返回 0;失败:返回 -1 。
- 输入参数:pathname => 文件路径名 。返回值:成功:返回 0;失败:返回 -1 。
#include #include #include #include #include #include int main(int argc, char **argv){if (argc != 2) {printf("usage: %s .\\n", argv[0]);return -1;}struct stat st;const char *pathname = argv[1];if (lstat(pathname, &st) == -1) {printf("get file %s information failed, %s.\\n", \\pathname, strerror(errno));return -1;}if (!S_ISLNK(st.st_mode)) {printf("%s is not a symbolic link file.\\n", pathname);return -1;}return unlink(pathname);}使用方法:$ mkdir ~/clanguage && cd ~/clanguage$ touch unlink.c注:创建并拷贝代码到unlink.c,保存退出 。$ gcc -o unlink unlink.c$ touch hello.txt$ ./symlink hello.txt world.txt$ ./unlink world.txt运行效果:
运行效果图
