home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume04 / which2 < prev   
Encoding:
Text File  |  1991-08-27  |  5.2 KB  |  250 lines

  1. Path: uunet!husc6!m2c!necntc!ncoast!allbery
  2. From: maart@uunet.uu.net@cs.vu.nl.UUCP (Maarten Litmaath)
  3. Newsgroups: comp.sources.misc
  4. Subject: v04i010: New version of fast which(1) + manual
  5. Message-ID: <1328@ark.cs.vu.nl>
  6. Date: 22 Jul 88 18:26:59 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: maart@uunet.uu.net@cs.vu.nl.UUCP (Maarten Litmaath)
  9. Organization: VU Informatica, Amsterdam
  10. Lines: 237
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. Posting-number: Volume 4, Issue 10
  14. Submitted-by: "Maarten Litmaath" <maart@uunet.uu.net@cs.vu.nl.UUCP>
  15. Archive-name: which2
  16.  
  17. Here's yet another which(1). Its pluses compared to /usr/ucb/which and other
  18. FAST versions, are described in the manual.  Enjoy!
  19.  
  20. : This is a shar archive.  Extract with sh, not csh.
  21. : This archive ends with exit, so do not worry about trailing junk.
  22. : --------------------------- cut here --------------------------
  23. PATH=/bin:/usr/bin
  24. echo Extracting \w\h\i\c\h\.\1
  25. sed 's/^X//' > \w\h\i\c\h\.\1 << '+ END-OF-FILE '\w\h\i\c\h\.\1
  26. X.TH WHICH 1 LOCAL
  27. X.SH NAME
  28. Xwhich \- give alias or path expansion of command
  29. X.SH SYNOPSIS
  30. X.B which
  31. X[
  32. X.B \-i
  33. X] [
  34. X.B \-a
  35. X] 
  36. X.I command
  37. X.SH DESCRIPTION
  38. X.B Which
  39. Xprovides the user with the full expansion of the
  40. X.I command
  41. Xargument, be it either an
  42. X.I alias
  43. Xor an executable file (default). To enable search for
  44. X.I aliases
  45. Xthe user should supply the
  46. X.B \-i
  47. X(= interactive) flag. In that case
  48. X.B which
  49. Xexpects as standard input the output of the
  50. X.I csh
  51. X.I alias
  52. Xcommand. This demand is easily met by setting an alias like the following:
  53. X.IP
  54. X.B
  55. Xalias which alias \\| /usr/local/bin/which \-i
  56. X.LP
  57. X.sp
  58. XIf the
  59. X.B \-i
  60. Xflag is not supplied, only the user's
  61. X.I PATH
  62. Xis searched for the
  63. X.I command.
  64. XIf the
  65. X.B \-a
  66. X(= all) flag is given,
  67. X.B which
  68. Xwill not stop after the first 'match', but search for all occurrences of
  69. X.I command
  70. Xin the user's
  71. X.I PATH.
  72. X.PP
  73. XThis new version of the
  74. X.I which
  75. Xcommand is not a
  76. X.I csh
  77. Xscript.
  78. XBeing an executable it is much faster, and not sourcing 
  79. X.I .cshrc
  80. Xit gives a true picture of one's
  81. X.I aliases
  82. Xand can be safely used between backquotes, like:
  83. X.sp
  84. X.RS
  85. X.B
  86. X$ file `which which`
  87. X.br
  88. X.B /usr/local/bin/which: pure executable
  89. X.br
  90. X.B $
  91. X.RE
  92. X.SH EXAMPLE
  93. X.B % alias
  94. X.br
  95. X.B which \t alias | /usr/local/bin/which \-i
  96. X.br
  97. X.B % which which
  98. X.br
  99. X.B which \t alias | /usr/local/bin/which \-i
  100. X.br
  101. X.B % which \-a which
  102. X.br
  103. X.B which \t alias | /usr/local/bin/which \-i
  104. X.br
  105. X.B /usr/local/bin/which
  106. X.br
  107. X.B /usr/ucb/which
  108. X.br
  109. X.B %
  110. X.SH AUTHOR
  111. XMaarten Litmaath
  112. X.SH BUGS
  113. XStrange behaviour occurs at power failures.
  114. + END-OF-FILE which.1
  115. chmod 'u=rw,g=r,o=r' \w\h\i\c\h\.\1
  116. set `sum \w\h\i\c\h\.\1`
  117. sum=$1
  118. case $sum in
  119. 52618)    :;;
  120. *)    echo 'Bad sum in '\w\h\i\c\h\.\1 >&2
  121. esac
  122. echo Extracting \w\h\i\c\h\.\c
  123. sed 's/^X//' > \w\h\i\c\h\.\c << '+ END-OF-FILE '\w\h\i\c\h\.\c
  124. X/*
  125. X * which [-a] [-i] <command>
  126. X * alias which alias \| /usr/local/bin/which -i
  127. X *
  128. X * author: Maarten Litmaath @ Free U Amsterdam
  129. X */
  130. X
  131. X#include    <sys/types.h>
  132. X#include    <sys/stat.h>
  133. X#include    <stdio.h>
  134. X
  135. X#define        BUF_SIZE    512
  136. X
  137. Xchar    E_usage[] = "Usage: [alias |] %s [-a] [-i] <command>\n",
  138. X    E_read[] = "%s - read error in ",
  139. X    E_path[] = "no PATH in environment!\n",
  140. X    E_dir[] = "%s found in unreadable directory %s!\n",
  141. X    E_notfound[] = "%s not found\n",
  142. X    *prog;
  143. X
  144. X
  145. Xmain(argc, argv) 
  146. Xint    argc;
  147. Xregister char    **argv;
  148. X{
  149. X    register char    *path, *s;
  150. X    char    *strcpy(), *getenv(), *gets(), buf[BUF_SIZE];
  151. X    int    n, all = 0, inter = 0, found = 0;
  152. X    struct    stat    st;
  153. X    void    usage();
  154. X
  155. X
  156. X    prog = *argv++;
  157. X
  158. X    if (argc < 2 || argc > 4)
  159. X        usage();
  160. X
  161. X    if (--argc > 1) {
  162. X        while (--argc) {
  163. X            s = *argv++;
  164. X            if (*s++ != '-')
  165. X                usage();
  166. X            while (*s)
  167. X                switch (*s++) {
  168. X                    case 'a':
  169. X                        all = 1;
  170. X                        break;
  171. X                    case 'i':
  172. X                        inter = 1;
  173. X                        break;
  174. X                    default:
  175. X                        usage();
  176. X                }
  177. X        }
  178. X        if (inter) {
  179. X            n = strlen(*argv);
  180. X            while (gets(buf))
  181. X                if (buf[n] == '\t' &&
  182. X                    !strncmp(buf, *argv, n)) {
  183. X                    puts(buf);
  184. X                    if (!all)
  185. X                        exit(0);
  186. X                    found = 1;
  187. X                    break;
  188. X                }
  189. X            if (ferror(stdin)) {
  190. X                fprintf(stderr, E_read, prog);
  191. X                perror("stdin");
  192. X                exit(1);
  193. X            }
  194. X        }
  195. X    }
  196. X
  197. X    if (!(path = getenv("PATH"))) {
  198. X        fprintf(stderr, E_path);
  199. X        exit(1);
  200. X    }
  201. X
  202. X    for ( ; *path; ) {
  203. X        s = buf;
  204. X        while ((*s++ = *path) && *path++ != ':')
  205. X            ;
  206. X        (void) strcpy(s, *argv);
  207. X        *--s = '/';
  208. X        if (access(buf, 1) == 0) {    /* file is executable */
  209. X            if (stat(buf, &st) == 0 && (st.st_mode & S_IFMT)
  210. X                == S_IFREG) {    /* file is regular,   */
  211. X                *s = 0;        /* is not a directory */
  212. X                if (access(buf, 4) == 0) {  /* directory is */
  213. X                    *s = '/';        /* readable     */
  214. X                    puts(buf);
  215. X                    if (!all)
  216. X                        exit(0);
  217. X                    found = 1;
  218. X                }
  219. X                else
  220. X                    fprintf(stderr, E_dir, *argv, buf);
  221. X            }
  222. X        }
  223. X    }
  224. X
  225. X    if (found)
  226. X        exit(0);
  227. X
  228. X    fprintf(stderr, E_notfound, *argv);
  229. X    exit(1);
  230. X}
  231. X
  232. X
  233. Xvoid    usage()
  234. X{
  235. X    fprintf(stderr, E_usage, prog);
  236. X    exit(1);
  237. X}
  238. + END-OF-FILE which.c
  239. chmod 'u=rw,g=r,o=r' \w\h\i\c\h\.\c
  240. set `sum \w\h\i\c\h\.\c`
  241. sum=$1
  242. case $sum in
  243. 21648)    :;;
  244. *)    echo 'Bad sum in '\w\h\i\c\h\.\c >&2
  245. esac
  246. exit 0
  247. -- 
  248. I'd rather live in Russia             |Maarten Litmaath @ Free U Amsterdam:
  249.               than in South-Africa... |maart@cs.vu.nl, mcvax!hp4nl!botter!maart
  250.