Centos获取命令源码

此生再无相见时 提交于 2020-02-16 00:04:45

以mv命令为例

1、查看命令所在位置

[root@VM_0_11_centos ~]# which mv
alias mv='mv -i'
        /usr/bin/mv

2、查看该命令所属rpm包

[root@VM_0_11_centos ~]# rpm -qif /usr/bin/mv
Name        : coreutils
Version     : 8.22
Release     : 24.el7
Architecture: x86_64
Install Date: Sun 08 Dec 2019 08:30:47 AM MST
Group       : System Environment/Base
Size        : 14593469
License     : GPLv3+
Signature   : RSA/SHA256, Thu 22 Aug 2019 03:21:30 PM MDT, Key ID 24c6a8a7f4a80eb5
Source RPM  : coreutils-8.22-24.el7.src.rpm
Build Date  : Tue 20 Aug 2019 12:27:26 AM MDT
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.gnu.org/software/coreutils/
Summary     : A set of basic GNU tools commonly used in shell scripts
Description :
These are the GNU core utilities.  This package is the combination of
the old GNU fileutils, sh-utils, and textutils packages.

3、下载该rpm包

[root@VM_0_11_centos yum.repos.d]# yumdownloader --source coreutils
Loaded plugins: fastestmirror, langpacks
Enabling updates-source repository
Enabling base-source repository
Enabling extras-source repository
Enabling docker-ce-stable-source repository
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * elrepo: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base-source                                                                                                                   | 2.9 kB  00:00:01
base-source/7/primary_db                                                                                                      | 1.1 MB  00:02:44
coreutils-8.22-24.el7.src.rpm                               35% [=================-                                ] 9.2 kB/s | 1.9 MB  00:06:20 ETA

如果下载失败,可以更换yum源,参考链接https://www.jianshu.com/p/df6241d7548c

4、利用rpm2cpio命令提取源代码

[root@VM_0_11_centos coreutils]# rpm2cpio coreutils-8.22-24.el7.src.rpm | cpio -id
11392 blocks
[root@VM_0_11_centos coreutils]# ll
total 11208
-rw-rw-r-- 1 root root     868 Feb 14 05:46 coreutils-4.5.3-langinfo.patch
-rw-rw-r-- 1 root root    5427 Feb 14 05:46 coreutils-6.10-configuration.patch
-rw-rw-r-- 1 root root     576 Feb 14 05:46 coreutils-6.10-manpages.patch
-rw-rw-r-- 1 root root     617 Feb 14 05:46 coreutils-7.4-sttytcsadrain.patch
-rw-r--r-- 1 root root 5531411 Feb 14 05:44 coreutils-8.22-24.el7.src.rpm
-rw-rw-r-- 1 root root    3758 Feb 14 05:46 coreutils-8.22-cp-selinux.patch
-rw-rw-r-- 1 root root   11416 Feb 14 05:46 coreutils-8.22-cp-sparsecorrupt.patch
-rw-rw-r-- 1 root root    1633 Feb 14 05:46 coreutils-8.22-date-emptyTZ.patch
-rw-rw-r-- 1 root root     823 Feb 14 05:46 coreutils-8.22-date-example-typo.patch
-rw-rw-r-- 1 root root   16663 Feb 14 05:46 coreutils-8.22-dd-progress.patch
-rw-rw-r-- 1 root root    1575 Feb 14 05:46 coreutils-8.22-df-autofs.patch
-rw-rw-r-- 1 root root   37683 Feb 14 05:46 coreutils-8.22-df-bind-mount.patch
-rw-rw-r-- 1 root root    1757 Feb 14 05:46 coreutils-8.22-df-dummy-local.patch
-rw-rw-r-- 1 root root   18860 Feb 14 05:46 coreutils-8.22-df-filtermountlistupdate.patch
-rw-rw-r-- 1 root root    3651 Feb 14 05:46 coreutils-8.22-df-getdisk.patch
-rw-rw-r-- 1 root root    1390 Feb 14 05:46 coreutils-8.22-df-stat.patch
-rw-rw-r-- 1 root root    1295 Feb 14 05:46 coreutils-8.22-doc-ls-kibibytes.patch
-rw-rw-r-- 1 root root    5801 Feb 14 05:46 coreutils-8.22-du-bindmountcycles.patch
-rw-rw-r-- 1 root root    1363 Feb 14 05:46 coreutils-8.22-failingtests.patch
-rw-rw-r-- 1 root root    1477 Feb 14 05:46 coreutils-8.22-id-groups.patch
-rw-rw-r-- 1 root root    6854 Feb 14 05:46 coreutils-8.22-ls-interruption.patch
-rw-rw-r-- 1 root root   14467 Feb 14 05:46 coreutils-8.22-mv-hardlinksrace.patch
-rw-rw-r-- 1 root root   37417 Feb 14 05:46 coreutils-8.22-mv-n-noreplace.patch
-rw-rw-r-- 1 root root   13185 Feb 14 05:46 coreutils-8.22-newfilesystems.patch
-rw-rw-r-- 1 root root   10225 Feb 14 05:46 coreutils-8.22-non-defaulttests.patch
-rw-rw-r-- 1 root root    1895 Feb 14 05:46 coreutils-8.22-ppc64le.patch
-rw-rw-r-- 1 root root    4559 Feb 14 05:46 coreutils-8.22-selinux-optionsseparate.patch
-rw-rw-r-- 1 root root    1145 Feb 14 05:46 coreutils-8.22-sort-blanks.patch
-rw-rw-r-- 1 root root 5335124 Feb 14 05:46 coreutils-8.22.tar.xz

5、解压文件

[root@VM_0_11_centos coreutils]# xz -d coreutils-8.22.tar.xz
[root@VM_0_11_centos coreutils]# tar -xf coreutils-8.22.tar
[root@VM_0_11_centos coreutils]# cd coreutils-8.22/
[root@VM_0_11_centos coreutils-8.22]# cd src
[root@VM_0_11_centos src]# ll
total 2808
-rw-rw-r-- 1 root root   8640 Dec  4  2013 base64.c
-rw-rw-r-- 1 root root   4919 Dec  4  2013 basename.c
-rw-rw-r-- 1 root root   5290 Dec  4  2013 c99-to-c89.diff
-rw-rw-r-- 1 root root  22725 Dec  4  2013 cat.c
-rw-rw-r-- 1 root root  16355 Dec  4  2013 chcon.c
-rw-rw-r-- 1 root root   8836 Dec  4  2013 chgrp.c
-rw-rw-r-- 1 root root  15845 Dec  4  2013 chmod.c
-rw-rw-r-- 1 root root  10116 Dec  4  2013 chown.c
-rw-rw-r-- 1 root root  17246 Dec  4  2013 chown-core.c
-rw-rw-r-- 1 root root   2281 Dec  4  2013 chown-core.h
-rw-rw-r-- 1 root root   6821 Dec  4  2013 chroot.c
-rw-rw-r-- 1 root root   9879 Dec  4  2013 cksum.c
-rw-rw-r-- 1 root root  12863 Dec  4  2013 comm.c
-rw-rw-r-- 1 root root 102362 Dec  4  2013 copy.c
-rw-rw-r-- 1 root root  10637 Dec  4  2013 copy.h

6、查看源码

[root@VM_0_11_centos src]# cat mv.c
/* mv -- move or rename files
   Copyright (C) 1986-2013 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.  */

/* Written by Mike Parker, David MacKenzie, and Jim Meyering */

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <assert.h>
#include <selinux/selinux.h>

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!