国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Linux > 正文

linux系統(tǒng)中whereis的用法詳解

2024-08-27 23:58:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

linux/Unix可以使用 whereis 這個(gè)命令來(lái)查找某個(gè)特定的程序和命令的二進(jìn)制文件()、源代碼和man手冊(cè)的位置,貌似現(xiàn)在還能看到軟件的配置文件的位置(路徑).

命令作用:

查找二進(jìn)制文件位置,查找軟件的配置文件的位置,查找源代碼文件位置,查看man手冊(cè)位置.

語(yǔ)法,基本語(yǔ)法如下:

  1. whereis command 
  2. OR 
  3. whereis program 
  4. OR 
  5. whereis [options] program 
  6. OR 
  7. whereis -BMS directory -f command 

whereis 使用舉例:

查找 date 命令的 可執(zhí)行文件、源代碼和man手冊(cè)的位置,輸入:

$ whereis date

如何只搜索二進(jìn)制文件的位置?

使用 -b 參數(shù):$ whereis -b date

如何只搜索man手冊(cè)的位置?

使用 -m 參數(shù):$ whereis -m date

如何只搜索源代碼文件的位置?

使用 -s 參數(shù):$ whereis -s date

問(wèn)題:whereis -u參數(shù)是有問(wèn)題的,按照man whereis 的說(shuō)明,-u的就是搜索那些沒(méi)有二進(jìn)制文件或者源代碼文件或者man手冊(cè)的文件的,但是實(shí)際測(cè)試發(fā)現(xiàn),和這毛關(guān)系都沒(méi)有啊.

man手冊(cè)上的一個(gè)例子:

A file is said to be unusual if it does not have one entry of each requested type. Thus the following example, asks for those files in the current directory which have no documentation(意思是搜索當(dāng)前目錄下,沒(méi)有man文檔的文件):

$ whereis -m -u *

我們先cd /bin,然后執(zhí)行上面的命令,會(huì)發(fā)現(xiàn)  whereis -m -u *  和 where -m *   結(jié)果是一模一樣的,-u的功能完全沒(méi)體現(xiàn)出來(lái),而且與man文檔描述的完全不符,因?yàn)?bin目錄下的文件都是有man文檔的,按man文檔的意思,結(jié)果應(yīng)該是空的,但是結(jié)果卻不是空的.

如何限制搜索的路徑?

使用下面的參數(shù)限制對(duì)應(yīng)的搜索路徑:

-B /path/to/dir:限制在指定的目錄搜索二進(jìn)制文件.

-M /path/to/dir:限制在指定的目錄搜索man手冊(cè)文件.

-S /path/to/dir:限制在指定的目錄搜索二進(jìn)制文件.

在使用了-B , -M , -S  任意一個(gè)參數(shù)時(shí),必須加上 -f  參數(shù),然后指定要搜索的文件名.

實(shí)例如下:只在 /bin 目錄下搜索 ls 和gcc的:$ whereis -B /bin -f ls gcc

結(jié)果如下:

  1. ls: /bin/ls /usr/share/man/man1/ls.1.gz 
  2. gcc: 

可以看到,gcc在/bin目錄下搜索二進(jìn)制文件是沒(méi)有結(jié)果的,說(shuō)明gcc的二進(jìn)制文件不在 /bin目錄下.

問(wèn)題:但是,我發(fā)現(xiàn),雖然/bin目錄下沒(méi)有g(shù)cc二進(jìn)制文件,但是使用上面的命令照樣有輸出,而不是像我翻譯的這篇文章那樣.

實(shí)例2:這也是man手冊(cè)上的例子,經(jīng)測(cè)試,這里 -u參數(shù)還是和man手冊(cè)上描述的不符合,查找所有/usr/bin目錄下的,其man文檔不在 /usr/man/man1/,且其源代碼文件不在/usr/src/ 的 文件,輸入:

  1. # cd /usr/bin 
  2. # whereis -u -ms -M /usr/man/man1 -S /usr/src -f * 
  3. //測(cè)試: 
  4. #cd  /bin 
  5. #whereis -u  -m -M /root  -f * 

按man手冊(cè)的意思,這行命令的功能是:查找 所有/bin下,其man文檔不在/root的文件,所以應(yīng)該是有結(jié)果輸出的,因?yàn)?root目錄下根本沒(méi)有任何文件的man手冊(cè),可以,驚奇的發(fā)現(xiàn),結(jié)果居然是空的.

  1. whereis command options 
  2. From the whereis(1) command man page: 
  3. Option Meaning 
  4. -f Define search scope. 
  5. -b Search only binaries. 
  6. -B Define binaries lookup path. 
  7. -m Search only manual paths. 
  8. -M Define man lookup path. 
  9. -s Search only sources path. 
  10. -S Define sources lookup path. 
  11. -u Search from unusual enties. 
  12. -V Output version information and exit.無(wú)效的,man文檔無(wú)此參數(shù) 
  13. -h Display this help and exit.    無(wú)效的,man文檔無(wú)此參數(shù) 
  14. SEE ALSO 
  15. whereis(1) Linux/Unix command man page 
  16. Category List of Unix and Linux commands 
  17. File Management cat 
  18. Network Utilities dig • host • ip 
  19. Processes Management bg • chroot • disown • fg • jobs • kill • killall • pwdx • time • pidof • pstree 
  20. Searching whereis • which 
  21. User Information groups • id • last • lastcomm • logname • users • w • who • whoami • lid • members 

關(guān)于whereis 的 -u參數(shù)的功能,因?yàn)椴恢纖hereis的版本,不好查找對(duì)應(yīng)版本的whereis的源代碼,我從網(wǎng)上找了個(gè)新版本的whereis的c源代碼,明顯的發(fā)現(xiàn),whereis使用hard-coded paths.

whereis在git上的代碼地址:https://github.com/karelzak/util-linux/blob/master/misc-utils/whereis.c#L96

代碼如下:

  1. /*- 
  2.  * Copyright (c) 1980 The Regents of the University of California. 
  3.  * All rights reserved. 
  4.  * 
  5.  * Redistribution and use in source and binary forms, with or without 
  6.  * modification, are permitted provided that the following conditions 
  7.  * are met: 
  8.  * 1. Redistributions of source code must retain the above copyright 
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  * 2. Redistributions in binary form must reproduce the above copyright 
  11.  *    notice, this list of conditions and the following disclaimer in the 
  12.  *    documentation and/or other materials provided with the distribution. 
  13.  * 3. All advertising materials mentioning features or use of this software 
  14.  *    must display the following acknowledgement: 
  15.  * This product includes software developed by the University of 
  16.  * California, Berkeley and its contributors. 
  17.  * 4. Neither the name of the University nor the names of its contributors 
  18.  *    may be used to endorse or promote products derived from this software 
  19.  *    without specific prior written permission. 
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  31.  * SUCH DAMAGE. 
  32.  * 
  33.  * 1999-02-22 Arkadiusz Mi?kiewicz <misiek@pld.ORG.PL> 
  34.  * - added Native Language Support 
  35.  * 2011-08-12 Davidlohr Bueso <dave@gnu.org> 
  36.  * - added $PATH lookup 
  37.  * 
  38.  * Copyright (C) 2013 Karel Zak <kzak@redhat.com> 
  39.  *               2013 Sami Kerola <kerolasa@iki.fi> 
  40.  */ 
  41.  
  42. #include <sys/param.h> 
  43. #include <sys/types.h> 
  44. #include <sys/stat.h> 
  45. #include <dirent.h> 
  46. #include <stdio.h> 
  47. #include <stdlib.h> 
  48. #include <string.h> 
  49. #include <ctype.h> 
  50. #include <assert.h> 
  51.  
  52. #include "xalloc.h" 
  53. #include "nls.h" 
  54. #include "c.h" 
  55. #include "closestream.h" 
  56. #include "canonicalize.h" 
  57.  
  58. #include "debug.h" 
  59.  
  60. UL_DEBUG_DEFINE_MASK(whereis); 
  61. UL_DEBUG_DEFINE_MASKNAMES(whereis) = UL_DEBUG_EMPTY_MASKNAMES; 
  62.  
  63. #define WHEREIS_DEBUG_INIT (1 << 1) 
  64. #define WHEREIS_DEBUG_PATH (1 << 2) 
  65. #define WHEREIS_DEBUG_ENV (1 << 3) 
  66. #define WHEREIS_DEBUG_ARGV (1 << 4) 
  67. #define WHEREIS_DEBUG_SEARCH (1 << 5) 
  68. #define WHEREIS_DEBUG_STATIC (1 << 6) 
  69. #define WHEREIS_DEBUG_LIST (1 << 7) 
  70. #define WHEREIS_DEBUG_ALL 0xFFFF 
  71.  
  72. #define DBG(m, x)       __UL_DBG(whereis, WHEREIS_DEBUG_, m, x) 
  73. #define ON_DBG(m, x)    __UL_DBG_CALL(whereis, WHEREIS_DEBUG_, m, x) 
  74.  
  75. static char uflag = 0; 
  76.  
  77. /* supported types */ 
  78. enum { 
  79.  BIN_DIR = (1 << 1), 
  80.  MAN_DIR = (1 << 2), 
  81.  SRC_DIR = (1 << 3), 
  82.  
  83.  ALL_DIRS = BIN_DIR | MAN_DIR | SRC_DIR 
  84. }; 
  85.  
  86. /* directories */ 
  87. struct wh_dirlist { 
  88.  int type; 
  89.  dev_t st_dev; 
  90.  ino_t st_ino; 
  91.  char *path; 
  92.  
  93.  struct wh_dirlist *next; 
  94. }; 
  95.  
  96. static const char *bindirs[] = { 
  97.  "/usr/bin"
  98.  "/usr/sbin"
  99.  "/usr/lib"
  100.  "/usr/lib64"
  101.  "/bin"
  102.  "/sbin"
  103.  "/etc"
  104.  "/usr/etc"
  105.  "/lib"
  106.  "/lib64"
  107.  "/usr/games"
  108.  "/usr/games/bin"
  109.  "/usr/games/lib"
  110.  "/usr/emacs/etc"
  111.  "/usr/lib/emacs/*/etc"
  112.  "/usr/TeX/bin"
  113.  "/usr/tex/bin"
  114.  "/usr/interviews/bin/LINUX"
  115.  
  116.  "/usr/X11R6/bin"
  117.  "/usr/X386/bin"
  118.  "/usr/bin/X11"
  119.  "/usr/X11/bin"
  120.  "/usr/X11R5/bin"
  121.  
  122.  "/usr/local/bin"
  123.  "/usr/local/sbin"
  124.  "/usr/local/etc"
  125.  "/usr/local/lib"
  126.  "/usr/local/games"
  127.  "/usr/local/games/bin"
  128.  "/usr/local/emacs/etc"
  129.  "/usr/local/TeX/bin"
  130.  "/usr/local/tex/bin"
  131.  "/usr/local/bin/X11"
  132.  
  133.  "/usr/contrib"
  134.  "/usr/hosts"
  135.  "/usr/include"
  136.  
  137.  "/usr/g++-include"
  138.  
  139.  "/usr/ucb"
  140.  "/usr/old"
  141.  "/usr/new"
  142.  "/usr/local"
  143.  "/usr/libexec"
  144.  "/usr/share"
  145.  
  146.  "/opt/*/bin"
  147.  NULL 
  148. }; 
  149.  
  150. static const char *mandirs[] = { 
  151.  "/usr/man/*"
  152.  "/usr/share/man/*"
  153.  "/usr/X386/man/*"
  154.  "/usr/X11/man/*"
  155.  "/usr/TeX/man/*"
  156.  "/usr/interviews/man/mann"
  157.  "/usr/share/info"
  158.  NULL 
  159. }; 
  160.  
  161. static const char *srcdirs[] = { 
  162.  "/usr/src/*"
  163.  "/usr/src/lib/libc/*"
  164.  "/usr/src/lib/libc/net/*"
  165.  "/usr/src/ucb/pascal"
  166.  "/usr/src/ucb/pascal/utilities"
  167.  "/usr/src/undoc"
  168.  NULL 
  169. }; 
  170.  
  171. static void whereis_init_debug(void) 
  172.  __UL_INIT_DEBUG(whereis, WHEREIS_DEBUG_, 0, WHEREIS_DEBUG); 
  173.  
  174. static const char *whereis_type_to_name(int type) 
  175.  switch (type) { 
  176.  case BIN_DIR: return "bin"
  177.  case MAN_DIR: return "man"
  178.  case SRC_DIR: return "src"
  179.  default:      return "???"
  180.  } 
  181.  
  182. static void __attribute__((__noreturn__)) usage(FILE *out) 
  183.  fputs(USAGE_HEADER, out); 
  184.  fprintf(out, _(" %s [options] <file>\n"), program_invocation_short_name); 
  185.  
  186.  fputs(USAGE_SEPARATOR, out); 
  187.  fputs(_("Locate the binary, source, and manual-page files for a command.\n"), out); 
  188.  
  189.  fputs(USAGE_OPTIONS, out); 
  190.  fputs(_(" -b         search only for binaries\n"), out); 
  191.  fputs(_(" -B <dirs>  define binaries lookup path\n"), out); 
  192.  fputs(_(" -m         search only for manuals and infos\n"), out); 
  193.  fputs(_(" -M <dirs>  define man and info lookup path\n"), out); 
  194.  fputs(_(" -s         search only for sources\n"), out); 
  195.  fputs(_(" -S <dirs>  define sources lookup path\n"), out); 
  196.  fputs(_(" -f         terminate <dirs> argument list\n"), out); 
  197.  fputs(_(" -u         search for unusual entries\n"), out); 
  198.  fputs(_(" -l         output effective lookup paths\n"), out); 
  199.  fprintf(out, USAGE_MAN_TAIL("whereis(1)")); 
  200.  
  201.  exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); 
  202.  
  203. static void dirlist_add_dir(struct wh_dirlist **ls0, int type, const char *dir) 
  204.  struct stat st; 
  205.  struct wh_dirlist *prev = NULL, *ls = *ls0; 
  206.  
  207.  if (access(dir, R_OK) != 0) 
  208.   return
  209.  if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode)) 
  210.   return
  211.  
  212.  while (ls) { 
  213.   if (ls->st_ino == st.st_ino && 
  214.       ls->st_dev == st.st_dev && 
  215.       ls->type == type) { 
  216.    DBG(LIST, ul_debugobj(*ls0, "  ignore (already in list): %s", dir)); 
  217.    return
  218.   } 
  219.   prev = ls; 
  220.   ls = ls->next; 
  221.  } 
  222.  
  223.  
  224.  ls = xcalloc(1, sizeof(*ls)); 
  225.  ls->st_ino = st.st_ino; 
  226.  ls->st_dev = st.st_dev; 
  227.  ls->type = type; 
  228.  ls->path = canonicalize_path(dir); 
  229.  
  230.  if (!*ls0) 
  231.   *ls0 = ls;  /* first in the list */ 
  232.  else { 
  233.   assert(prev); 
  234.   prev->next = ls; /* add to the end of the list */ 
  235.  } 
  236.  
  237.  DBG(LIST, ul_debugobj(*ls0, "  add dir: %s", ls->path)); 
  238.  return
  239.  
  240. /* special case for '*' in the paths */ 
  241. static void dirlist_add_subdir(struct wh_dirlist **ls, int type, const char *dir) 
  242.  char buf[PATH_MAX], *d; 
  243.  DIR *dirp; 
  244.  struct dirent *dp; 
  245.  
  246.  strncpy(buf, dir, PATH_MAX); 
  247.  buf[PATH_MAX - 1] = '\0'
  248.  
  249.  d = strchr(buf, '*'); 
  250.  if (!d) 
  251.   return
  252.  *d = 0; 
  253.  
  254.  dirp = opendir(buf); 
  255.  if (!dirp) 
  256.   return
  257.  
  258.  DBG(LIST, ul_debugobj(*ls, " scanning subdir: %s", dir)); 
  259.  
  260.  while ((dp = readdir(dirp)) != NULL) { 
  261.   if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 
  262.    continue
  263.   snprintf(d, PATH_MAX - (d - buf), "%s", dp->d_name); 
  264.   /* a dir definition can have a star in middle of path */ 
  265.   strcat(buf, strchr(dir, '*') + 1); 
  266.   dirlist_add_dir(ls, type, buf); 
  267.  } 
  268.  closedir(dirp); 
  269.  return
  270.  
  271. static void construct_dirlist_from_env(const char *env, 
  272.            struct wh_dirlist **ls, 
  273.            int type) 
  274.  char *key = NULL, *tok = NULL, *pathcp, *path = getenv(env); 
  275.  
  276.  if (!path) 
  277.   return
  278.  pathcp = xstrdup(path); 
  279.  
  280.  DBG(ENV, ul_debugobj(*ls, "construct %s dirlist from: %s"
  281.     whereis_type_to_name(type), path)); 
  282.  
  283.  for (tok = strtok_r(pathcp, ":", &key); tok; 
  284.       tok = strtok_r(NULL, ":", &key)) 
  285.   dirlist_add_dir(ls, type, tok); 
  286.  
  287.  free(pathcp); 
  288.  return
  289.  
  290. static void construct_dirlist_from_argv(struct wh_dirlist **ls, 
  291.      int *idx, 
  292.      int argc, 
  293.      char *argv[], 
  294.      int type) 
  295.  int i; 
  296.  
  297.  DBG(ARGV, ul_debugobj(*ls, "construct %s dirlist from argv[%d..]"
  298.     whereis_type_to_name(type), *idx)); 
  299.  
  300.  for (i = *idx; i < argc; i++) { 
  301.   if (*argv[i] == '-')   /* end of the list */ 
  302.    break
  303.  
  304.   DBG(ARGV, ul_debugobj(*ls, "  using argv[%d]: %s", *idx, argv[*idx])); 
  305.   dirlist_add_dir(ls, type, argv[i]); 
  306.   *idx = i; 
  307.  } 
  308.  
  309.  return
  310.  
  311. static void construct_dirlist(struct wh_dirlist **ls, 
  312.          int type, 
  313.          const char **paths) 
  314.  size_t i; 
  315.  
  316.  DBG(STATIC, ul_debugobj(*ls, "construct %s dirlist from static array"
  317.     whereis_type_to_name(type))); 
  318.  
  319.  for (i = 0; paths[i]; i++) { 
  320.   if (!strchr(paths[i], '*')) 
  321.    dirlist_add_dir(ls, type, paths[i]); 
  322.   else 
  323.    dirlist_add_subdir(ls, type, paths[i]); 
  324.  } 
  325.  return
  326.  
  327. static void free_dirlist(struct wh_dirlist **ls0, int type) 
  328.  struct wh_dirlist *prev = NULL, *next, *ls = *ls0; 
  329.  
  330.  *ls0 = NULL; 
  331.  
  332.  DBG(LIST, ul_debugobj(*ls0, "free dirlist")); 
  333.  
  334.  while (ls) { 
  335.   if (ls->type & type) { 
  336.    next = ls->next; 
  337.    DBG(LIST, ul_debugobj(*ls0, " free: %s", ls->path)); 
  338.    free(ls->path); 
  339.    free(ls); 
  340.    ls = next; 
  341.    if (prev) 
  342.     prev->next = ls; 
  343.   } else { 
  344.    if (!prev) 
  345.     *ls0 = ls; /* first unremoved */ 
  346.    prev = ls; 
  347.    ls = ls->next; 
  348.   } 
  349.  } 
  350.  
  351.  return
  352.  
  353.  
  354. static int filename_equal(const char *cp, const char *dp) 
  355.  int i = strlen(dp); 
  356.  
  357.  DBG(SEARCH, ul_debug("compare '%s' and '%s'", cp, dp)); 
  358.  
  359.  if (dp[0] == 's' && dp[1] == '.' && filename_equal(cp, dp + 2)) 
  360.   return 1; 
  361.  if (!strcmp(dp + i - 2, ".Z")) 
  362.   i -= 2; 
  363.  else if (!strcmp(dp + i - 3, ".gz")) 
  364.   i -= 3; 
  365.  else if (!strcmp(dp + i - 3, ".xz")) 
  366.   i -= 3; 
  367.  else if (!strcmp(dp + i - 4, ".bz2")) 
  368.   i -= 4; 
  369.  while (*cp && *dp && *cp == *dp) 
  370.   cp++, dp++, i--; 
  371.  if (*cp == 0 && *dp == 0) 
  372.   return 1; 
  373.  while (isdigit(*dp)) 
  374.   dp++; 
  375.  if (*cp == 0 && *dp++ == '.') { 
  376.   --i; 
  377.   while (i > 0 && *dp) 
  378.    if (--i, *dp++ == '.'
  379.     return (*dp++ == 'C' && *dp++ == 0); 
  380.   return 1; 
  381.  } 
  382.  return 0; 
  383.  
  384. static void findin(const char *dir, const char *pattern, int *count, char **wait) 
  385.  DIR *dirp; 
  386.  struct dirent *dp; 
  387.  
  388.  dirp = opendir(dir); 
  389.  if (dirp == NULL) 
  390.   return
  391.  
  392.  DBG(SEARCH, ul_debug("find '%s' in '%s'", pattern, dir)); 
  393.  
  394.  while ((dp = readdir(dirp)) != NULL) { 
  395.   if (!filename_equal(pattern, dp->d_name)) 
  396.    continue
  397.  
  398.   if (uflag && *count == 0) 
  399.    xasprintf(wait, "%s/%s", dir, dp->d_name); 
  400.  
  401.   else if (uflag && *count == 1 && *wait) { 
  402.    printf("%s: %s %s/%s", pattern, *wait, dir,  dp->d_name); 
  403.    free(*wait); 
  404.    *wait = NULL; 
  405.   } else 
  406.    printf(" %s/%s", dir, dp->d_name); 
  407.   ++(*count); 
  408.  } 
  409.  closedir(dirp); 
  410.  return
  411.  
  412. static void lookup(const char *pattern, struct wh_dirlist *ls, int want) 
  413.  char patbuf[PATH_MAX]; 
  414.  int count = 0; 
  415.  char *wait = NULL, *p; 
  416.  
  417.  /* canonicalize pattern -- remove path suffix etc. */ 
  418.  p = strrchr(pattern, '/'); 
  419.  p = p ? p + 1 : (char *) pattern; 
  420.  strncpy(patbuf, p, PATH_MAX); 
  421.  patbuf[PATH_MAX - 1] = '\0'
  422.  
  423.  DBG(SEARCH, ul_debug("lookup dirs for '%s' (%s), want: %s %s %s"
  424.     patbuf, pattern, 
  425.     want & BIN_DIR ? "bin" : ""
  426.     want & MAN_DIR ? "min" : ""
  427.     want & SRC_DIR ? "src" : "")); 
  428.  p = strrchr(patbuf, '.'); 
  429.  if (p) 
  430.   *p = '\0'
  431.  
  432.  if (!uflag) 
  433.   /* if -u not specified then we always print the pattern */ 
  434.   printf("%s:", patbuf); 
  435.  
  436.  for (; ls; ls = ls->next) { 
  437.   if ((ls->type & want) && ls->path) 
  438.    findin(ls->path, patbuf, &count, &wait); 
  439.  } 
  440.  
  441.  free(wait); 
  442.  
  443.  if (!uflag || (uflag && count > 1)) 
  444.   putchar('\n'); 
  445.  return
  446.  
  447. static void list_dirlist(struct wh_dirlist *ls) 
  448.  while (ls) { 
  449.   if (ls->path) { 
  450.    switch (ls->type) { 
  451.    case BIN_DIR: 
  452.     printf("bin: "); 
  453.     break
  454.    case MAN_DIR: 
  455.     printf("man: "); 
  456.     break
  457.    case SRC_DIR: 
  458.     printf("src: "); 
  459.     break
  460.    default
  461.     abort(); 
  462.    } 
  463.    printf("%s\n", ls->path); 
  464.   } 
  465.   ls = ls->next; 
  466.  } 
  467.  
  468. int main(int argc, char **argv) 
  469.  struct wh_dirlist *ls = NULL; 
  470.  int want = ALL_DIRS; 
  471.  int i, want_resetable = 0; 
  472.  
  473.  setlocale(LC_ALL, ""); 
  474.  bindtextdomain(PACKAGE, LOCALEDIR); 
  475.  textdomain(PACKAGE); 
  476.  atexit(close_stdout); 
  477.  
  478.  if (argc == 1) 
  479.   usage(stderr); 
  480.  
  481.  whereis_init_debug(); 
  482.  
  483.  construct_dirlist(&ls, BIN_DIR, bindirs); 
  484.  construct_dirlist_from_env("PATH", &ls, BIN_DIR); 
  485.  
  486.  construct_dirlist(&ls, MAN_DIR, mandirs); 
  487.  construct_dirlist_from_env("MANPATH", &ls, MAN_DIR); 
  488.  
  489.  construct_dirlist(&ls, SRC_DIR, srcdirs); 
  490.  
  491.  for (i = 1; i < argc; i++) { 
  492.   const char *arg = argv[i]; 
  493.   int arg_i = i; 
  494.  
  495.   DBG(ARGV, ul_debug("argv[%d]: %s", i, arg)); 
  496.  
  497.   if (*arg != '-') { 
  498.    lookup(arg, ls, want); 
  499.    /* 
  500.     * The lookup mask ("want") is cumulative and it's 
  501.     * resetable only when it has been already used. 
  502.     * 
  503.     *  whereis -b -m foo     :'foo' mask=BIN|MAN 
  504.     *  whereis -b foo bar    :'foo' and 'bar' mask=BIN|MAN 
  505.     *  whereis -b foo -m bar :'foo' mask=BIN; 'bar' mask=MAN 
  506.     */ 
  507.    want_resetable = 1; 
  508.    continue
  509.   } 
  510.  
  511.   for (++arg; arg && *arg; arg++) { 
  512.    DBG(ARGV, ul_debug("  arg: %s", arg)); 
  513.  
  514.    switch (*arg) { 
  515.    case 'f'
  516.     break
  517.    case 'u'
  518.     uflag = 1; 
  519.     break
  520.    case 'B'
  521.     if (*(arg + 1)) 
  522.      usage(stderr); 
  523.     i++; 
  524.     free_dirlist(&ls, BIN_DIR); 
  525.     construct_dirlist_from_argv( 
  526.      &ls, &i, argc, argv, BIN_DIR); 
  527.     break
  528.    case 'M'
  529.     if (*(arg + 1)) 
  530.      usage(stderr); 
  531.     i++; 
  532.     free_dirlist(&ls, MAN_DIR); 
  533.     construct_dirlist_from_argv( 
  534.      &ls, &i, argc, argv, MAN_DIR); 
  535.     break
  536.    case 'S'
  537.     if (*(arg + 1)) 
  538.      usage(stderr); 
  539.     i++; 
  540.     free_dirlist(&ls, SRC_DIR); 
  541.     construct_dirlist_from_argv( 
  542.      &ls, &i, argc, argv, SRC_DIR); 
  543.     break
  544.    case 'b'
  545.     if (want_resetable) { 
  546.      want = ALL_DIRS; 
  547.      want_resetable = 0; 
  548.     } 
  549.     want = want == ALL_DIRS ? BIN_DIR : want | BIN_DIR; 
  550.     break
  551.    case 'm'
  552.     if (want_resetable) { 
  553.      want = ALL_DIRS; 
  554.      want_resetable = 0; 
  555.     } 
  556.     want = want == ALL_DIRS ? MAN_DIR : want | MAN_DIR; 
  557.     break
  558.    case 's'
  559.     if (want_resetable) { 
  560.      want = ALL_DIRS; 
  561.      want_resetable = 0; 
  562.     } 
  563.     want = want == ALL_DIRS ? SRC_DIR : want | SRC_DIR; 
  564.     break
  565.    case 'l'
  566.     list_dirlist(ls); 
  567.     break
  568.    case 'V'
  569.     printf(UTIL_LINUX_VERSION); 
  570.     return EXIT_SUCCESS; 
  571.    case 'h'
  572.     usage(stdout); 
  573.    default
  574.     usage(stderr); 
  575.    } 
  576.  
  577.    if (arg_i < i)  /* moved to the next argv[] item */ 
  578.     break;  //Vevb.com 
  579.   } 
  580.  } 
  581.  
  582.  free_dirlist(&ls, ALL_DIRS); 
  583.  return EXIT_SUCCESS; 

靠,就連代碼注釋都有錯(cuò)誤,懶得折騰了,哥還有事要做,反正 -u 參數(shù)實(shí)際很少使用的,有興趣的可以看下這代碼.

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 准格尔旗| 晋中市| 曲周县| 名山县| 苍梧县| 永州市| 松溪县| 阿鲁科尔沁旗| 津市市| 天长市| 华安县| 雷波县| 澜沧| 广州市| 肥西县| 六枝特区| 内丘县| 安义县| 祁连县| 西贡区| 循化| 衡南县| 金门县| 黄骅市| 斗六市| 治县。| 江华| 新兴县| 新乐市| 孙吴县| 吴江市| 琼海市| 寿宁县| 罗平县| 奉贤区| 周宁县| 丹东市| 井冈山市| 云龙县| 鄱阳县| 共和县|