home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1710 / ref.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  3KB  |  132 lines

  1. /* ref.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    16820 SW Tallac Way
  6.  *    Beaverton, OR 97006
  7.  *    kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This program looks up the declarations of library functions. */
  12.  
  13. #include <stdio.h>
  14.  
  15. /* This is the list of files that are searched. */
  16. char *refslist[] = {
  17.     "refs",
  18.     "/usr/src/lib/refs",
  19.     "../lib/refs",
  20.     "/usr/local/lib/refs",
  21. };
  22. #define NREFS    (sizeof refslist / sizeof(char *))
  23.  
  24. main(argc, argv)
  25.     int    argc;
  26.     char    **argv;
  27. {
  28.     int    i;    /* used to step through the refslist */
  29.  
  30.     /* make sure our arguments are OK */
  31.     if (argc != 2)
  32.     {
  33.         fprintf(stderr, "usage: %s function_name\n", *argv);
  34.         exit(2);
  35.     }
  36.  
  37.     /* check for the function in each database */
  38.     for (i = 0; i < NREFS; i++)
  39.     {
  40.         if (lookinfor(refslist[i], argv[1]))
  41.         {
  42.             exit(0);
  43.         }
  44.     }
  45.  
  46.     fprintf(stderr, "%s: don't know about %s\n", argv[0], argv[1]);
  47.     exit(2);
  48. }
  49.  
  50.  
  51. /* This function checks a single file for the function.  Returns 1 if found */
  52. int lookinfor(filename, func)
  53.     char    *filename;    /* name of file to look in */
  54.     char    *func;        /* name of function to look for */
  55. {
  56.     FILE    *fp;    /* stream used to access the database */
  57.     char    linebuf[300];
  58.         /* NOTE: in actual use, the first character of linebuf is */
  59.         /* set to ' ' and then we use all EXCEPT the 1st character */
  60.         /* everywhere in this function.  This is because the func */
  61.         /* which examines the linebuf could, in some circumstances */
  62.         /* examine the character before the used part of linebuf; */
  63.         /* we need to control what happens then.           */
  64.  
  65.  
  66.     /* open the database file */
  67.     fp = fopen(filename, "r");
  68.     if (!fp)
  69.     {
  70.         return 0;
  71.     }
  72.  
  73.     /* find the desired entry */
  74.     *linebuf = ' ';
  75.     do
  76.     {
  77.         if (!fgets(linebuf + 1, (sizeof linebuf) - 1, fp))
  78.         {
  79.             fclose(fp);
  80.             return 0;
  81.         }
  82.     } while (!desired(linebuf + 1, func));
  83.  
  84.     /* print it */
  85.     do
  86.     {
  87.         fputs(linebuf + 1, stdout);
  88.     } while (fgets(linebuf + 1, sizeof linebuf, fp) && linebuf[1] == '\t');
  89.  
  90.     /* cleanup & exit */
  91.     fclose(fp);
  92.     return 1;
  93. }
  94.  
  95.  
  96. /* This function checks to see if a given line is the first line of the */
  97. /* entry the user wants to see.  If it is, return non-0 else return 0   */
  98. desired(line, word)
  99.     char    *line;    /* the line buffer */
  100.     char    *word;    /* the string it should contain */
  101. {
  102.     static    wlen = -1;/* length of the "word" variable's value */
  103.     register char *scan;
  104.  
  105.     /* if this line starts with a tab, it couldn't be desired */
  106.     if (*line == '\t')
  107.     {
  108.         return 0;
  109.     }
  110.  
  111.     /* if we haven't found word's length yet, do so */
  112.     if (wlen < 0)
  113.     {
  114.         wlen = strlen(word);
  115.     }
  116.  
  117.     /* search for the word in the line */
  118.     for (scan = line; *scan != '('; scan++)
  119.     {
  120.     }
  121.     while (*--scan == ' ')
  122.     {
  123.     }
  124.     scan -= wlen;
  125.     if (scan < line - 1 || *scan != ' ' && *scan != '\t' && *scan != '*')
  126.     {
  127.         return 0;
  128.     }
  129.     scan++;
  130.     return !strncmp(scan, word, wlen);
  131. }
  132.