home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mintman4.zoo / mintman / apropos.c < prev    next >
C/C++ Source or Header  |  1992-12-15  |  5KB  |  173 lines

  1. /********************************************************************/
  2. /* apropos.c - a simple-minded apropos program for MiNT and TOS     */
  3. /* Copyright (c) 1992 by HPP Biersma (schuller@dutiag.tudelft.nl)   */
  4. /* This program comes under the GNU public license -                */
  5. /*                            see the file "copying" for details.   */
  6. /********************************************************************/
  7. /* bugs: - not UN*X compatible, either in source-code or behavior   */
  8. /*       - string operations are not safe, ie not limits checked    */
  9. /********************************************************************/
  10. /* version: 0.2 (second released version), December 15, 1992        */
  11. /* written for: - GCC version 2.3.1, patchlevel 1                   */
  12. /*              - mintlib patchlevel 25                             */
  13. /* compile with: gcc -o apropos.ttp apropos.c -mbaserel -mpcrel -O2 */
  14. /********************************************************************/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stat.h>
  20. #include <mintbind.h>
  21.  
  22. #define NAME_LENGTH 256
  23.  
  24. /* prototypes for local (static) functions */
  25. static int check_directory_name(const char *dirname);
  26. static int print_apropos(const char *mandir, const char *command);
  27.  
  28. static char man_path[NAME_LENGTH] = "/usr/man";
  29.  
  30. void main(int argc, char *argv[])
  31. {
  32.   char *ptr;
  33.   int  done_anything, did_print;
  34.   
  35.   /* Set the search path for the man pages */
  36.   if ((ptr = getenv("MANPATH")) != NULL)
  37.     strncpy(man_path, ptr, NAME_LENGTH - 1);
  38.  
  39.   done_anything = 0;
  40.   argc -= 1;
  41.   argv += 1;
  42.   while (argc >= 1)
  43.   {
  44.     ptr = argv[0];
  45.     if (*ptr == '-')
  46.     {
  47.       if (*(ptr + 1) == 'M')
  48.       {
  49.         if (*(ptr + 2) != 0x00)
  50.           strcpy(man_path, (ptr + 2));
  51.         else
  52.         {
  53.           if (argc == 1)
  54.           {
  55.             fprintf(stderr, "apropos: -M option needs an argument\n");
  56.             exit(1);
  57.           }
  58.           else
  59.           {
  60.             strcpy(man_path, argv[1]);
  61.             argv += 1;
  62.             argc -= 1;
  63.           }
  64.         } /* End of if (-M (space) name) */
  65.       } /* End of if (-M option) */
  66.       else
  67.       {
  68.         fprintf(stderr, "apropos: no option %s supported\n", ptr);
  69.         fprintf(stderr, "usage: apropos [-M path] command ...\n");
  70.         exit(1);
  71.       }
  72.     } /* end of if (option) */
  73.     else
  74.     {
  75.       char *path_ptr;
  76.       
  77.       /* walk along manual path and check whatis database */
  78.       path_ptr = man_path;
  79.       did_print = 0;
  80.       while (*path_ptr != 0x00)
  81.       {
  82.         char mandir[NAME_LENGTH], *manptr;
  83.             
  84.         /* Copy one directory from the manual search path into mandir */
  85.         manptr = mandir;
  86.         while ((*path_ptr != 0x00) && (*path_ptr != ';') && 
  87.                (*path_ptr != ','))
  88.           *manptr++ = *path_ptr++;
  89.         *manptr = 0x00;
  90.         if (*path_ptr != 0x00)
  91.           path_ptr += 1;
  92.  
  93.         if (check_directory_name(mandir) == 0)
  94.         {
  95.           fprintf(stderr, "apropos: directory %s on search path not found\n",
  96.                   mandir);
  97.           continue; /* go on to next part of manual search path */
  98.         }
  99.         
  100.         did_print |= print_apropos(mandir, ptr);
  101.         done_anything = 1;
  102.       } /* end of while (walk along manual search path) */
  103.       if (did_print == 0)
  104.         fprintf(stderr, "apropos: %s not found in whatis database\n", ptr);
  105.     }
  106.     argv += 1;
  107.     argc -= 1;
  108.   } /* End of while (arguments left) */
  109.  
  110.   if (done_anything == 0)
  111.   {
  112.     fprintf(stderr, "usage: apropos [-M path] command ...\n");
  113.     exit(1);
  114.   }
  115.   
  116.   exit(0);
  117. } /* End of main() */
  118.  
  119.  
  120. /* Check if a dirname is a valid directory */
  121. static int check_directory_name(const char *dirname)
  122. {
  123.   struct stat dir_stat;
  124.  
  125.   if (stat(dirname, &dir_stat) == -1)
  126.     return(0);
  127.   else if (dir_stat.st_mode & S_IFDIR == 0)
  128.   {
  129.     fprintf(stderr, "apropos: %s is not a directory\n", dirname);
  130.     return(0);
  131.   }
  132.   else
  133.     return(1);
  134. } /* End of check_directory_name() */
  135.  
  136.  
  137. /* Handle the `man -k' (apropos) search within a specific directory. */
  138. /* This is similar to the `man -f' (whatis) search, which also makes */
  139. /* use of this routine. The whatis argument must be stripped first.  */
  140. static int print_apropos(const char *mandir, const char *name)
  141. {
  142.   FILE *whatis;
  143.   char line_read[NAME_LENGTH], line_buffer[NAME_LENGTH], 
  144.        name_buffer[NAME_LENGTH];
  145.   int  did_print;
  146.   
  147.   strcpy(name_buffer, mandir);
  148.   strcat(name_buffer, "/whatis");
  149.   if ((whatis = fopen(name_buffer, "r")) == NULL)
  150.   {
  151.     fprintf(stderr, "%s: no such file\n", name_buffer);
  152.     return(0);
  153.   }
  154.   
  155.   /* No case-insensitive strstr exists. Improvise with all-lowercase. */
  156.   strcpy(name_buffer, name);
  157.   strlwr(name_buffer);
  158.   did_print = 0;
  159.   while (!feof(whatis))
  160.   {
  161.     fgets(line_read, NAME_LENGTH - 1, whatis);
  162.     strcpy(line_buffer, line_read);
  163.     strlwr(line_buffer);
  164.     if (strstr(line_buffer, name_buffer) != NULL)
  165.     {
  166.       fputs(line_read, stdout);
  167.       did_print = 1;
  168.     }
  169.   }
  170.   fclose(whatis);
  171.   return(did_print);
  172. } /* End of print_apropos() */
  173.