Chinaunix首页 | 论坛 | 博客
  • 博客访问: 915605
  • 博文数量: 177
  • 博客积分: 8613
  • 博客等级: 中将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-12 04:16
文章分类
文章存档

2012年(12)

2011年(24)

2010年(24)

2009年(75)

2008年(42)

我的朋友

分类: C/C++

2009-10-29 21:37:28

    看完了link.c的源码,自己写一个来实现之:
[root@bjxdurs235 20091029]# cat my_link.c
#include
int main(int argc, char **argv)
{
        if(argc != 3){
                fputs("argument error !\n",stderr);
                exit (1);
        }

        if( link( argv[1],argv[2] ) != 0 )
        {
                fputs("call sys link error !\n",stderr);
                exit (1);
        }
        exit (0);
}

程序里面的错误判断是必须的,因为命令行个数和系统调用都是有可能出现失败的,以下就是两个出错的具体模拟:
1、命令行参数个数不正确
[root@bjxdurs235 20091029]# ./a.out
argument error !
2、目标连接文件名和源文件名相同,则系统调用失败
[root@bjxdurs235 20091029]# ./a.out my_link.c my_link.c
call sys link error !
3、正常使用程序的情况
[root@bjxdurs235 20091029]# ./a.out my_link.c link
[root@bjxdurs235 20091029]# ll
total 16
-rwxr-xr-x  1 root root 5082 Oct 29 21:31 a.out
-rw-r--r--  2 root root  233 Oct 29 21:31 link
-rw-r--r--  2 root root  233 Oct 29 21:31 my_link.c

   ok,把代码分析贴一下,其实写blog,维护blog,目的就是给自己一个事情做,坚持每天分析完以后贴过来,不在乎是否有读者看,只要求对自己的学习有一个交代。

/* link 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:这个注释说的好啊,调用系统调用link^_^

   Simply call the system 'link' function */
//包含进来4个标准头文件
#include
#include
#include
#include
//包含进来4个本地文件夹里的头文件
#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 "link" //定义程序名称宏PROGRAM_NAME,其值为一个字符串

#define AUTHORS "Michael Stone"//定义宏AUTHORS,其值也是一个字符串

/* Name this program was run with.  */
char *program_name;//定义全局变量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 FILE1 FILE2\n\
  or:  %s OPTION\n"), program_name, program_name);
      fputs (_("Call the link function to create a link named FILE2\
 to an existing FILE1.\n\n"), //这句话表明了程序的作用,调用link系统调用,创建一个指向已经存在的文件的连接文件
         stdout);
      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);

  atexit (close_stdout);//登记出口函数

  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
              usage, AUTHORS, (char const *) NULL);//解析命令行选项,用来打印帮助信息
  if (getopt_long (argc, argv, "", NULL, NULL) != -1)//如果调用getopt_long函数解析命令行参数没有正常结束的话,调用usage函数打印帮助信息
    usage (EXIT_FAILURE);//传给usage函数的形参是EXIT_FAILURE,因为此时命令行参数肯定是不正确的

  if (argc < optind + 2)//optind是调用getopt_long后第一个不是选项的参数的index
  /*
如果argc的值小于optind+2的值,
  */
    {
      if (argc < optind + 1)//这个周末好好研究linux上的命令行选项解析命题
      //如果在这个分支里,argc的值比optind+1都小的话,调用error函数
    error (0, 0, _("missing operand"));
      else//否则调用error函数,并调用usage打印帮助信息
    error (0, 0, _("missing operand after %s"), quote (argv[optind]));
      usage (EXIT_FAILURE);
    }

  if (optind + 2 < argc)//如果argc的值是大于optind+2的值的,则说明参数太多了
    {
      error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
      usage (EXIT_FAILURE);
    }

  if (link (argv[optind], argv[optind + 1]) != 0)//这里是关键的、核心的地方,调用是link来实际地进行操作,按照惯例,还是来man一下
  /*
link系统调用的原型为:
  int link(const char *oldpath, const char *newpath);
  改系统调用接受两个形参,一个是现在存在的文件,另一个是要新建的文件名,man里面对它功能的解释为:
  link creates a new link (also known as a hard link) to an existing file.

       If newpath exists it will not be overwritten.
       说明这个系统调用创建了一个硬连接文件


  */
    error (EXIT_FAILURE, errno, _("cannot create link %s to %s"),//如果系统调用失败,则调用error函数
       quote_n (0, argv[optind + 1]), quote_n (1, argv[optind]));

  exit (EXIT_SUCCESS);//程序执行到这里,说明系统调用link正常执行了,于是程序返回状态为EXIT_SUCCESS
}

阅读(1016) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~