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

  1. /******************************************************************/
  2. /* whatis.c - a simple-minded whatis 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 whatis.ttp whatis.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 void strip_filename(const char *name, char *stripped_name);
  27. static int print_apropos(const char *mandir, const char *command);
  28.  
  29. static char man_path[NAME_LENGTH] = "/usr/man";
  30.  
  31. void main(int argc, char *argv[])
  32. {
  33.   char *ptr, stripped_name[NAME_LENGTH];
  34.   int  done_anything, did_print;
  35.   
  36.   /* Set the search path for the man pages */
  37.   if ((ptr = getenv("MANPATH")) != NULL)
  38.     strncpy(man_path, ptr, NAME_LENGTH - 1);
  39.  
  40.   done_anything = 0;
  41.   argc -= 1;
  42.   argv += 1;
  43.   while (argc >= 1)
  44.   {
  45.     ptr = argv[0];
  46.     if (*ptr == '-')
  47.     {
  48.       if (*(ptr + 1) == 'M')
  49.       {
  50.         if (*(ptr + 2) != 0x00)
  51.           strcpy(man_path, (ptr + 2));
  52.         else
  53.         {
  54.           if (argc == 1)
  55.           {
  56.             fprintf(stderr, "whatis: -M needs an argument\n");
  57.             exit(1);
  58.           }
  59.           else
  60.           {
  61.             strcpy(man_path, argv[1]);
  62.             argv += 1;
  63.             argc -= 1;
  64.           }
  65.         } /* End of if (-M (space) name) */
  66.       } /* End of if (-M option) */
  67.       else
  68.       {
  69.         fprintf(stderr, "whatis: no option %s supported\n", ptr);
  70.         fprintf(stderr, "usage: whatis [-M path] command ...\n");
  71.         exit(1);
  72.       }
  73.     } /* end of if (option) */
  74.     else
  75.     {
  76.       char *path_ptr;
  77.       
  78.       /* walk along manual path and check whatis database */
  79.       path_ptr = man_path;
  80.       did_print = 0;
  81.       while (*path_ptr != 0x00)
  82.       {
  83.         char mandir[NAME_LENGTH], *manptr;
  84.             
  85.         /* Copy one directory from the manual search path into mandir */
  86.         manptr = mandir;
  87.         while ((*path_ptr != 0x00) && (*path_ptr != ';') && 
  88.                (*path_ptr != ','))
  89.           *manptr++ = *path_ptr++;
  90.         *manptr = 0x00;
  91.         if (*path_ptr != 0x00)
  92.           path_ptr += 1;
  93.  
  94.         if (check_directory_name(mandir) == 0)
  95.         {
  96.           fprintf(stderr, "whatis: directory %s on search path not found\n",
  97.                   mandir);
  98.           continue; /* go on to next part of manual search path */
  99.         }
  100.         
  101.         strip_filename(ptr, stripped_name);
  102.         did_print |= print_apropos(mandir, stripped_name);
  103.         done_anything = 1;
  104.       } /* end of while (walk along manual search path) */
  105.       if (did_print == 0)
  106.         fprintf(stderr, "whatis: %s not found in whatis database\n", ptr);
  107.     }
  108.     argv += 1;
  109.     argc -= 1;
  110.   } /* End of while (arguments left) */
  111.  
  112.   if (done_anything == 0)
  113.   {
  114.     fprintf(stderr, "usage: whatis [-M path] command ...\n");
  115.     exit(1);
  116.   }
  117.   
  118.   exit(0);
  119. } /* End of main() */
  120.  
  121.  
  122. /* Check if a dirname is a valid directory */
  123. static int check_directory_name(const char *dirname)
  124. {
  125.   struct stat dir_stat;
  126.  
  127.   if (stat(dirname, &dir_stat) == -1)
  128.     return(0);
  129.   else if (dir_stat.st_mode & S_IFDIR == 0)
  130.   {
  131.     fprintf(stderr, "whatis: %s is not a directory\n", dirname);
  132.     return(0);
  133.   }
  134.   else
  135.     return(1);
  136. } /* End of check_directory_name() */
  137.  
  138.  
  139. /* Strip the leading path-name and extension components from a filename. */
  140. /* This is needed for the `man -f' (whatis) option, which is the same as */
  141. /* `man -k' (apropos), except the whatis version needs a stripped name.  */
  142. static void strip_filename(const char *name, char *stripped_name)
  143. {
  144.   char   buffer[NAME_LENGTH], *ptr1, *ptr2;
  145.   
  146.   strcpy(buffer, name);
  147.   ptr1 = ptr2 = buffer;
  148.   while (*ptr2 != 0x00)
  149.   {
  150.     if ((*ptr2 == '/') || (*ptr2 == '\\'))
  151.       ptr1 = ptr2 + 1;
  152.     ptr2 += 1;
  153.   }
  154.   ptr2 = ptr1;
  155.   while ((*ptr2 != 0x00) && (*ptr2 != '.'))
  156.     ptr2 += 1;
  157.   *ptr2 = 0x00;
  158.   strcpy(stripped_name, ptr1);
  159. } /* End of strip_filename() */
  160.  
  161.  
  162. /* Handle the `man -k' (apropos) search within a specific directory. */
  163. /* This is similar to the `man -f' (whatis) search, which also makes */
  164. /* use of this routine. The whatis argument must be stripped first.  */
  165. static int print_apropos(const char *mandir, const char *name)
  166. {
  167.   FILE *whatis;
  168.   char line_read[NAME_LENGTH], line_buffer[NAME_LENGTH], 
  169.        name_buffer[NAME_LENGTH];
  170.   int  did_print;
  171.   
  172.   strcpy(name_buffer, mandir);
  173.   strcat(name_buffer, "/whatis");
  174.   if ((whatis = fopen(name_buffer, "r")) == NULL)
  175.   {
  176.     fprintf(stderr, "%s: no such file\n", name_buffer);
  177.     return(0);
  178.   }
  179.   
  180.   /* No case-insensitive strstr exists. Improvise with all-lowercase. */
  181.   strcpy(name_buffer, name);
  182.   strlwr(name_buffer);
  183.   did_print = 0;
  184.   while (!feof(whatis))
  185.   {
  186.     fgets(line_read, NAME_LENGTH - 1, whatis);
  187.     strcpy(line_buffer, line_read);
  188.     strlwr(line_buffer);
  189.     if (strstr(line_buffer, name_buffer) != NULL)
  190.     {
  191.       fputs(line_read, stdout);
  192.       did_print = 1;
  193.     }
  194.   }
  195.   fclose(whatis);
  196.   return(did_print);
  197. } /* End of print_apropos() */
  198.