程序看完了,实际的使用了一下unlink程序,的确是把指定的文件给删除了,和rm一样,强啊,另外,自己实现了一个简陋版的unlink.c呵呵:
[root@bjxdurs235 20091026]# cat unlink.c
#include
int main(int argc , char **argv)
{
if(argc != 2){
fputs("argument error!\n",stderr);
exit (1);
}
unlink(argv[1]);
exit (0);
}
[root@bjxdurs235 20091026]# ll
total 12
-rwxr-xr-x 1 root root 5015 Oct 26 20:42 a.out
-rw-r--r-- 1 root root 155 Oct 26 20:42 unlink.c
[root@bjxdurs235 20091026]# echo ok > test
[root@bjxdurs235 20091026]# ./a.out
argument error!
[root@bjxdurs235 20091026]# ./a.out test
[root@bjxdurs235 20091026]# echo $?
0
[root@bjxdurs235 20091026]# ll
total 12
-rwxr-xr-x 1 root root 5015 Oct 26 20:42 a.out
-rw-r--r-- 1 root root 155 Oct 26 20:42 unlink.c
可以看到,这程序的确是调用了unlink系统调用,把文件给删除了。我的程序也实现了功能,但是和下面人家的标准程序比较一下,差距的确很大啊,开源的程序绝对值得好好学习。
下面是标准包里的代码,和我的注释。
/* unlink utility for GNU.
Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* Written by Michael Stone */
/* Implementation overview:
Simply call the system 'unlink' function */
//包含四个必要的库里的头文件
#include
#include
#include
#include
//包含本地的四个头文件
#include "system.h"
#include "error.h"
#include "long-options.h"
#include "quote.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "unlink" //定义宏PROGRAM_NAME,其值为一个字符串
#define AUTHORS "Michael Stone"//定义宏AUTHORS,其值也是一个字符串
/* Name this program was run with. */
char *program_name;//声明一个全局字符指针变量
void
usage (int status)//和前面几个程序一样,上来先定义了一个帮助函数
{
if (status != EXIT_SUCCESS)//如果函数接受到的形参status不是EXIT_SUCCESS,则:
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);//向标准错误打印一个字符串,提示用户输入--help
else//反之,如果status等于EXIT_SUCCESS的话,则打印下面的这一段字符串
{
printf (_("\
Usage: %s FILE\n\
or: %s OPTION\n"), program_name, program_name);
fputs (_("Call the unlink function to remove the specified FILE.\n\n"),
stdout);//这个输出说明了这个程序的作用,调用unlink系统调用,来删除特定的文件
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status);//函数的退出值为传入的形参status的值
}
int
main (int argc, char **argv)//标准的main函数声明
{
initialize_main (&argc, &argv);//初始化命令行参数
program_name = argv[0];//把命令行参数的第一个赋给全局变量program_name
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);//这3句是和环境有关的,可以理解为空语句,在我的as4上面,预编译后就是空语句
atexit (close_stdout);//yodao词典有工作了:登记出口函数
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
usage, AUTHORS, (char const *) NULL);//解析命令行参数,这里多说一点吧
/*
parse_long_options()函数是在lib/parse_long_options.h文件里声明、在lib/parse_long_options.c下面实现的,具体的实现代码我在另一个段落里面贴出来
*/
if (getopt_long (argc, argv, "", NULL, NULL) != -1)
//getopt_long()函数返回-1的情况是:没有命令行参数
usage (EXIT_FAILURE);//这时候调用usage函数,传递的形参值为EXIT_FAILURE
if (argc < optind + 1)//如果命令行参数的个数比optind + 1的值还小,则调用error函数,并调用usage函数
{
error (0, 0, _("missing operand"));
usage (EXIT_FAILURE);
}
if (optind + 1 < argc)//反之,如果argc的值大于optind + 1 ,则说明参数多了,也调用error函数和usage函数
{
error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
usage (EXIT_FAILURE);
}
if (unlink (argv[optind]) != 0)//这一句是核心的做事的语句,调用unlink系统调用了,我待会写个程序直接调用他
error (EXIT_FAILURE, errno, _("cannot unlink %s"), quote (argv[optind]));//如果系统调用失败的话,调用error函数
exit (EXIT_SUCCESS);//如果上面的都顺利执行了的话,程序最终返回值为EXIT_SUCCESS
}
阅读(1024) | 评论(0) | 转发(0) |