请教,哪儿可以找到linux命令的源代码呀,比如ls,pwd,cp等的源代码

发布于 2022-07-21 23:01:18 字数 26 浏览 12 评论 9

如题。。。。

谢谢。。。。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

只有影子陪我不离不弃 2022-07-23 02:19:02

原帖由 wooin 于 2006-4-25 14:09 发表

不对。。。不对。。。都有单独的源代码的,看看我找到的ls.c的文件

[code]/* `dir', `vdir' and `ls' directory listing programs for GNU.
   Copyright (C) 85, 88, 90, 91, 1995-2003 Free Softwa ...

你没看明白斑竹的意思……
他没说ls是shell内建命令。

辞慾 2022-07-23 02:15:59

系统的学习一下基础知识吧

另外, 别人的帖子也看一下再回复嘛

简美 2022-07-23 02:15:30

原帖由 albcamus 于 2006-4-24 15:35 发表
Q:如何知道cd、ls等命令来自哪个软件包?
A:cd是shell内建命令,除非看shell源代码,否则没有源码; ls则#rpm -ql `which ls`。 其它类似。

不对。。。不对。。。都有单独的源代码的,看看我找到的ls.c的文件

  1. /* `dir', `vdir' and `ls' directory listing programs for GNU.
  2.    Copyright (C) 85, 88, 90, 91, 1995-2003 Free Software Foundation, Inc.
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2, or (at your option)
  6.    any later version.
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.
  11.    You should have received a copy of the GNU General Public License
  12.    along with this program; if not, write to the Free Software Foundation,
  13.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  14. /* If ls_mode is LS_MULTI_COL,
  15.    the multi-column format is the default regardless
  16.    of the type of output device.
  17.    This is for the `dir' program.
  18.    If ls_mode is LS_LONG_FORMAT,
  19.    the long format is the default regardless of the
  20.    type of output device.
  21.    This is for the `vdir' program.
  22.    If ls_mode is LS_LS,
  23.    the output format depends on whether the output
  24.    device is a terminal.
  25.    This is for the `ls' program. */
  26. /* Written by Richard Stallman and David MacKenzie.  */
  27. /* Color support by Peter Anvin <Peter.Anvin@linux.org> and Dennis
  28.    Flaherty <dennisf@denix.elk.miles.com> based on original patches by
  29.    Greg Lee <lee@uhunix.uhcc.hawaii.edu>.  */
  30. #ifdef _AIX
  31. #pragma alloca
  32. #endif
  33. #include <config.h>
  34. #include <sys/types.h>
  35. #if HAVE_TERMIOS_H
  36. # include <termios.h>
  37. #endif
  38. #ifdef GWINSZ_IN_SYS_IOCTL
  39. # include <sys/ioctl.h>
  40. #endif
  41. #ifdef WINSIZE_IN_PTEM
  42. # include <sys/stream.h>
  43. # include <sys/ptem.h>
  44. #endif
  45. #include <stdio.h>
  46. #include <assert.h>
  47. #include <setjmp.h>
  48. #include <grp.h>
  49. #include <pwd.h>
  50. #include <getopt.h>
  51. #include <signal.h>
  52. /* Get MB_CUR_MAX.  */
  53. #if HAVE_STDLIB_H
  54. # include <stdlib.h>
  55. #endif
  56. /* Get mbstate_t, mbrtowc(), mbsinit(), wcwidth().  */
  57. #if HAVE_WCHAR_H
  58. # include <wchar.h>
  59. #endif
  60. /* Get iswprint().  */
  61. #if HAVE_WCTYPE_H
  62. # include <wctype.h>
  63. #endif
  64. #if !defined iswprint && !HAVE_ISWPRINT
  65. # define iswprint(wc) 1
  66. #endif
  67. #ifndef HAVE_DECL_WCWIDTH
  68. "this configure-time declaration test was not run"
  69. #endif
  70. #if !HAVE_DECL_WCWIDTH
  71. int wcwidth ();
  72. #endif
  73. /* If wcwidth() doesn't exist, assume all printable characters have
  74.    width 1.  */
  75. #ifndef wcwidth
  76. # if !HAVE_WCWIDTH
  77. #  define wcwidth(wc) ((wc) == 0 ? 0 : iswprint (wc) ? 1 : -1)
  78. # endif
  79. #endif
  80. #include "system.h"
  81. #include <fnmatch.h>
  82. #include "acl.h"
  83. #include "argmatch.h"
  84. #include "dev-ino.h"
  85. #include "dirname.h"
  86. #include "dirfd.h"
  87. #include "error.h"
  88. #include "full-write.h"
  89. #include "hard-locale.h"
  90. #include "hash.h"
  91. #include "human.h"
  92. #include "filemode.h"
  93. #include "inttostr.h"
  94. #include "ls.h"
  95. #include "mbswidth.h"
  96. #include "obstack.h"
  97. #include "path-concat.h"
  98. #include "quote.h"
  99. #include "quotearg.h"
  100. #include "same.h"
  101. #include "strftime.h"
  102. #include "strverscmp.h"
  103. #include "xstrtol.h"
  104. #include "xreadlink.h"
  105. #define PROGRAM_NAME (ls_mode == LS_LS ? "ls"
  106.                       : (ls_mode == LS_MULTI_COL
  107.                          ? "dir" : "vdir"))
  108. #define AUTHORS N_ ("Richard Stallman and David MacKenzie")
  109. #define obstack_chunk_alloc malloc
  110. #define obstack_chunk_free free
  111. /* Return an int indicating the result of comparing two integers.
  112.    Subtracting doesn't always work, due to overflow.  */
  113. #define longdiff(a, b) ((a) < (b) ? -1 : (a) > (b))
  114. /* The field width for inode numbers.  On some hosts inode numbers are
  115.    64 bits, so columns won't line up exactly when a huge inode number
  116.    is encountered, but in practice 7 digits is usually enough.  */
  117. #ifndef INODE_DIGITS
  118. # define INODE_DIGITS 7
  119. #endif
  120. /* Arrange to make lstat calls go through the wrapper function
  121.    on systems with an lstat function that does not dereference symlinks
  122.    that are specified with a trailing slash.  */
  123. #if ! LSTAT_FOLLOWS_SLASHED_SYMLINK
  124. int rpl_lstat (const char *, struct stat *);
  125. # undef lstat
  126. # define lstat(Name, Stat_buf) rpl_lstat(Name, Stat_buf)
  127. #endif
  128. #if HAVE_STRUCT_DIRENT_D_TYPE && defined DTTOIF
  129. # define DT_INIT(Val) = Val
  130. #else
  131. # define DT_INIT(Val) /* empty */
  132. #endif
  133. #ifdef ST_MTIM_NSEC
  134. # define TIMESPEC_NS(timespec) ((timespec).ST_MTIM_NSEC)
  135. #else
  136. # define TIMESPEC_NS(timespec) 0
  137. #endif
  138. #if ! HAVE_STRUCT_STAT_ST_AUTHOR
  139. # define st_author st_uid
  140. #endif
  141. /* Cray/Unicos DMF: use the file's migrated, not real, status */
  142. #if HAVE_ST_DM_MODE
  143. # define ST_DM_MODE(Stat_buf) ((Stat_buf).st_dm_mode)
  144. #else
  145. # define ST_DM_MODE(Stat_buf) ((Stat_buf).st_mode)
  146. #endif
  147. #ifndef LOGIN_NAME_MAX
  148. # if _POSIX_LOGIN_NAME_MAX
  149. #  define LOGIN_NAME_MAX _POSIX_LOGIN_NAME_MAX
  150. # else
  151. #  define LOGIN_NAME_MAX 17
  152. # endif
  153. #endif
  154. /* The maximum length of a string representation of a user or group ID,
  155.    not counting any terminating NUL byte.  */
  156. #define ID_LENGTH_MAX
  157.   MAX (LOGIN_NAME_MAX - 1, LONGEST_HUMAN_READABLE)
  158. enum filetype
  159.   {
  160.     unknown DT_INIT (DT_UNKNOWN),
  161.     fifo DT_INIT (DT_FIFO),
  162.     chardev DT_INIT (DT_CHR),
  163.     directory DT_INIT (DT_DIR),
  164.     blockdev DT_INIT (DT_BLK),
  165.     normal DT_INIT (DT_REG),
  166.     symbolic_link DT_INIT (DT_LNK),
  167.     sock DT_INIT (DT_SOCK),
  168.     arg_directory DT_INIT (2 * (DT_UNKNOWN | DT_FIFO | DT_CHR | DT_DIR | DT_BLK
  169.                                 | DT_REG | DT_LNK | DT_SOCK))
  170.   };
  171. struct fileinfo
  172.   {
  173.     /* The file name. */
  174.     char *name;
  175.     struct stat stat;
  176.     /* For symbolic link, name of the file linked to, otherwise zero. */
  177.     char *linkname;
  178.     /* For symbolic link and long listing, st_mode of file linked to, otherwise
  179.        zero. */
  180.     mode_t linkmode;
  181.     /* For symbolic link and color printing, 1 if linked-to file
  182.        exists, otherwise 0.  */
  183.     int linkok;
  184.     enum filetype filetype;
  185. #if HAVE_ACL
  186.     /* For long listings, true if the file has an access control list.  */
  187.     bool have_acl;
  188. #endif
  189.   };
  190. #if HAVE_ACL
  191. # define FILE_HAS_ACL(F) ((F)->have_acl)
  192. #else
  193. # define FILE_HAS_ACL(F) 0
  194. #endif
  195. #define LEN_STR_PAIR(s) sizeof (s) - 1, s
  196. /* Null is a valid character in a color indicator (think about Epson
  197.    printers, for example) so we have to use a length/buffer string
  198.    type.  */
  199. struct bin_str
  200.   {
  201.     int len;                        /* Number of bytes */
  202.     const char *string;                /* Pointer to the same */
  203.   };
  204. #ifndef STDC_HEADERS
  205. time_t time ();
  206. #endif
  207. char *getgroup ();
  208. char *getuser ();
  209. static size_t quote_name (FILE *out, const char *name,
  210.                           struct quoting_options const *options,
  211.                           size_t *width);
  212. static char *make_link_path (const char *path, const char *linkname);
  213. static int decode_switches (int argc, char **argv);
  214. static int file_interesting (const struct dirent *next);
  215. static uintmax_t gobble_file (const char *name, enum filetype type,
  216.                               int explicit_arg, const char *dirname);
  217. static void print_color_indicator (const char *name, mode_t mode, int linkok);
  218. static void put_indicator (const struct bin_str *ind);
  219. static int put_indicator_direct (const struct bin_str *ind);
  220. static int length_of_file_name_and_frills (const struct fileinfo *f);
  221. static void add_ignore_pattern (const char *pattern);
  222. static void attach (char *dest, const char *dirname, const char *name);
  223. static void clear_files (void);
  224. static void extract_dirs_from_files (const char *dirname,
  225.                                      int ignore_dot_and_dot_dot);
  226. static void get_link_name (const char *filename, struct fileinfo *f);
  227. static void indent (int from, int to);
  228. static void init_column_info (void);
  229. static void print_current_files (void);
  230. static void print_dir (const char *name, const char *realname);
  231. static void print_file_name_and_frills (const struct fileinfo *f);
  232. static void print_horizontal (void);
  233. static void print_long_format (const struct fileinfo *f);
  234. static void print_many_per_line (void);
  235. static void print_name_with_quoting (const char *p, mode_t mode,
  236.                                      int linkok,
  237.                                      struct obstack *stack);
  238. static void prep_non_filename_text (void);
  239. static void print_type_indicator (mode_t mode);
  240. static void print_with_commas (void);
  241. static void queue_directory (const char *name, const char *realname);
  242. static void sort_files (void);
  243. static void parse_ls_color (void);
  244. void usage (int status);
  245. /* The name the program was run with, stripped of any leading path. */
  246. char *program_name;
  247. /* Initial size of hash table.
  248.    Most hierarchies are likely to be shallower than this.  */
  249. #define INITIAL_TABLE_SIZE 30
  250. /* The set of `active' directories, from the current command-line argument
  251.    to the level in the hierarchy at which files are being listed.
  252.    A directory is represented by its device and inode numbers (struct dev_ino).
  253.    A directory is added to this set when ls begins listing it or its
  254.    entries, and it is removed from the set just after ls has finished
  255.    processing it.  This set is used solely to detect loops, e.g., with
  256.    mkdir loop; cd loop; ln -s ../loop sub; ls -RL  */
  257. static Hash_table *active_dir_set;
  258. #define LOOP_DETECT (!!active_dir_set)
  259. /* The table of files in the current directory:
  260.    `files' points to a vector of `struct fileinfo', one per file.
  261.    `nfiles' is the number of elements space has been allocated for.
  262.    `files_index' is the number actually in use.  */
  263. /* Address of block containing the files that are described.  */
  264. static struct fileinfo *files;  /* FIXME: rename this to e.g. cwd_file */
  265. /* Length of block that `files' points to, measured in files.  */
  266. static int nfiles;  /* FIXME: rename this to e.g. cwd_n_alloc */
  267. /* Index of first unused in `files'.  */
  268. static int files_index;  /* FIXME: rename this to e.g. cwd_n_used */
  269. /* When nonzero, in a color listing, color each symlink name according to the
  270.    type of file it points to.  Otherwise, color them according to the `ln'
  271.    directive in LS_COLORS.  Dangling (orphan) symlinks are treated specially,
  272.    regardless.  This is set when `ln=target' appears in LS_COLORS.  */
  273. static int color_symlink_as_referent;
  274. /* mode of appropriate file for colorization */
  275. #define FILE_OR_LINK_MODE(File)
  276.     ((color_symlink_as_referent && (File)->linkok)
  277.      ? (File)->linkmode : (File)->stat.st_mode)
  278. /* Record of one pending directory waiting to be listed.  */
  279. struct pending
  280.   {
  281.     char *name;
  282.     /* If the directory is actually the file pointed to by a symbolic link we
  283.        were told to list, `realname' will contain the name of the symbolic
  284.        link, otherwise zero. */
  285.     char *realname;
  286.     struct pending *next;
  287.   };
  288. static struct pending *pending_dirs;
  289. /* Current time in seconds and nanoseconds since 1970, updated as
  290.    needed when deciding whether a file is recent.  */
  291. static time_t current_time = TYPE_MINIMUM (time_t);
  292. static int current_time_ns = -1;
  293. /* The number of digits to use for block sizes.
  294.    4, or more if needed for bigger numbers.  */
  295. static int block_size_size;
  296. 。。。。。。。。太长了。。。。。。。。
  297. int
  298. main (int argc, char **argv)
  299. {
  300.   register int i;
  301.   register struct pending *thispend;
  302.   unsigned int n_files;
  303.   program_name = argv[0];
  304.   setlocale (LC_ALL, "");
  305.   bindtextdomain (PACKAGE, LOCALEDIR);
  306.   textdomain (PACKAGE);
  307.   atexit (close_stdout);
  308. #define N_ENTRIES(Array) (sizeof Array / sizeof *(Array))
  309.   assert (N_ENTRIES (color_indicator) + 1 == N_ENTRIES (indicator_name));
  310.   exit_status = 0;
  311.   dir_defaulted = 1;
  312.   print_dir_name = 1;
  313.   pending_dirs = 0;
  314.   i = decode_switches (argc, argv);
  315.   if (print_with_color)
  316.     parse_ls_color ();
  317.   /* Test print_with_color again, because the call to parse_ls_color
  318.      may have just reset it -- e.g., if LS_COLORS is invalid.  */
  319.   if (print_with_color)
  320.     {
  321.       prep_non_filename_text ();
  322.       /* Avoid following symbolic links when possible.  */
  323.       if (color_indicator[C_ORPHAN].string != NULL
  324.           || (color_indicator[C_MISSING].string != NULL
  325.               && format == long_format))
  326.         check_symlink_color = 1;
  327.       {
  328.         unsigned j;
  329.         static int const sigs[] = { SIGHUP, SIGINT, SIGPIPE,
  330.                                     SIGQUIT, SIGTERM, SIGTSTP };
  331.         unsigned nsigs = sizeof sigs / sizeof *sigs;
  332. #ifdef SA_NOCLDSTOP
  333.         struct sigaction oldact, newact;
  334.         sigset_t caught_signals;
  335.         sigemptyset (&caught_signals);
  336.         for (j = 0; j < nsigs; j++)
  337.           sigaddset (&caught_signals, sigs[j]);
  338.         newact.sa_handler = sighandler;
  339.         newact.sa_mask = caught_signals;
  340.         newact.sa_flags = 0;
  341. #endif
  342.         for (j = 0; j < nsigs; j++)
  343.           {
  344.             int sig = sigs[j];
  345. #ifdef SA_NOCLDSTOP
  346.             sigaction (sig, NULL, &oldact);
  347.             if (oldact.sa_handler != SIG_IGN)
  348.               sigaction (sig, &newact, NULL);
  349. #else
  350.             if (signal (sig, SIG_IGN) != SIG_IGN)
  351.               signal (sig, sighandler);
  352. #endif
  353.           }
  354.       }
  355.     }
  356.   if (dereference == DEREF_UNDEFINED)
  357.     dereference = ((immediate_dirs
  358.                     || indicator_style == classify
  359.                     || format == long_format)
  360.                    ? DEREF_NEVER
  361.                    : DEREF_COMMAND_LINE_SYMLINK_TO_DIR);
  362.   /* When using -R, initialize a data structure we'll use to
  363.      detect any directory cycles.  */
  364.   if (recursive)
  365.     {
  366.       active_dir_set = hash_initialize (INITIAL_TABLE_SIZE, NULL,
  367.                                         dev_ino_hash,
  368.                                         dev_ino_compare,
  369.                                         dev_ino_free);
  370.       if (active_dir_set == NULL)
  371.         xalloc_die ();
  372.       obstack_init (&dev_ino_obstack);
  373.     }
  374.   format_needs_stat = sort_type == sort_time || sort_type == sort_size
  375.     || format == long_format
  376.     || dereference == DEREF_ALWAYS
  377.     || print_block_size || print_inode;
  378.   format_needs_type = (format_needs_stat == 0
  379.                        && (recursive || print_with_color
  380.                            || indicator_style != none));
  381.   if (dired)
  382.     {
  383.       obstack_init (&dired_obstack);
  384.       obstack_init (&subdired_obstack);
  385.     }
  386.   nfiles = 100;
  387.   files = XMALLOC (struct fileinfo, nfiles);
  388.   files_index = 0;
  389.   clear_files ();
  390.   n_files = argc - i;
  391.   if (0 < n_files)
  392.     dir_defaulted = 0;
  393.   for (; i < argc; i++)
  394.     {
  395.       gobble_file (argv[i], unknown, 1, "");
  396.     }
  397.   if (dir_defaulted)
  398.     {
  399.       if (immediate_dirs)
  400.         gobble_file (".", directory, 1, "");
  401.       else
  402.         queue_directory (".", 0);
  403.     }
  404.   if (files_index)
  405.     {
  406.       sort_files ();
  407.       if (!immediate_dirs)
  408.         extract_dirs_from_files ("", 0);
  409.       /* `files_index' might be zero now.  */
  410.     }
  411.   /* In the following if/else blocks, it is sufficient to test `pending_dirs'
  412.      (and not pending_dirs->name) because there may be no markers in the queue
  413.      at this point.  A marker may be enqueued when extract_dirs_from_files is
  414.      called with a non-empty string or via print_dir.  */
  415.   if (files_index)
  416.     {
  417.       print_current_files ();
  418.       if (pending_dirs)
  419.         DIRED_PUTCHAR ('n');
  420.     }
  421.   else if (n_files <= 1 && pending_dirs && pending_dirs->next == 0)
  422.     print_dir_name = 0;
  423.   while (pending_dirs)
  424.     {
  425.       thispend = pending_dirs;
  426.       pending_dirs = pending_dirs->next;
  427.       if (LOOP_DETECT)
  428.         {
  429.           if (thispend->name == NULL)
  430.             {
  431.               /* thispend->name == NULL means this is a marker entry
  432.                  indicating we've finished processing the directory.
  433.                  Use its dev/ino numbers to remove the corresponding
  434.                  entry from the active_dir_set hash table.  */
  435.               struct dev_ino di = dev_ino_pop ();
  436.               struct dev_ino *found = hash_delete (active_dir_set, &di);
  437.               /* ASSERT_MATCHING_DEV_INO (thispend->realname, di); */
  438.               assert (found);
  439.               dev_ino_free (found);
  440.               free_pending_ent (thispend);
  441.               continue;
  442.             }
  443.         }
  444.       print_dir (thispend->name, thispend->realname);
  445.       free_pending_ent (thispend);
  446.       print_dir_name = 1;
  447.     }
  448.   if (dired)
  449.     {
  450.       /* No need to free these since we're about to exit.  */
  451.       dired_dump_obstack ("//DIRED//", &dired_obstack);
  452.       dired_dump_obstack ("//SUBDIRED//", &subdired_obstack);
  453.       printf ("//DIRED-OPTIONS// --quoting-style=%sn",
  454.               quoting_style_args[get_quoting_style (filename_quoting_options)]);
  455.     }
  456.   /* Restore default color before exiting */
  457.   if (print_with_color)
  458.     {
  459.       put_indicator (&color_indicator[C_LEFT]);
  460.       put_indicator (&color_indicator[C_RIGHT]);
  461.     }
  462.   if (LOOP_DETECT)
  463.     {
  464.       assert (hash_get_n_entries (active_dir_set) == 0);
  465.       hash_free (active_dir_set);
  466.     }
  467.   exit (exit_status);
  468. }
  469. 。。。。。。。。太长了。。。。。。。。

复制代码

最初的梦 2022-07-23 02:02:18

coreutils
findutils
diffutils

哽咽笑 2022-07-23 01:59:36

在fedora.redhat.com可找到

执笏见 2022-07-22 22:45:58

Q:如何知道cd、ls等命令来自哪个软件包?
A:cd是shell内建命令,除非看shell源代码,否则没有源码; ls则#rpm -ql `which ls`。 其它类似。

一抹苦笑 2022-07-22 22:41:27

原帖由 dbcat 于 2006-4-24 13:31 发表
gnu.org

哈哈。。。当然是在gnu.org了,真是一时糊涂。。。谢谢美女了。。。

这些命令在gnu.org中叫coreutils(The GNU Core Utilities)
http://www.gnu.org/software/coreutils/coreutils.html

下载地址在
ftp://sunsite.ust.hk/pub/gnu/coreutils/

希望对大家有点用。。。呵呵。。。

你的呼吸 2022-07-22 19:09:40

gnu.org

独自唱情﹋歌 2022-07-22 18:41:11

自己顶下。。。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文