home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / index-db / part01 / util.c < prev   
Encoding:
C/C++ Source or Header  |  1989-10-23  |  2.4 KB  |  154 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /u5/davy/progs/index/RCS/util.c,v 1.1 89/08/09 11:07:12 davy Exp $";
  3. #endif
  4. /*
  5.  * util.c - utility routines for index program.
  6.  *
  7.  * David A. Curry
  8.  * Research Institute for Advanced Computer Science
  9.  * Mail Stop 230-5
  10.  * NASA Ames Research Center
  11.  * Moffett Field, CA 94035
  12.  * davy@riacs.edu
  13.  *
  14.  * $Log:    util.c,v $
  15.  * Revision 1.1  89/08/09  11:07:12  davy
  16.  * Initial revision
  17.  * 
  18.  */
  19. #include <curses.h>
  20. #include <stdio.h>
  21. #include "defs.h"
  22.  
  23. /*
  24.  * set_dbase_dir - set the path to the database directory.
  25.  */
  26. set_dbase_dir()
  27. {
  28.     char *s;
  29.     char *getenv();
  30.  
  31.     /*
  32.      * Look for it in the environment.
  33.      */
  34.     if ((s = getenv("INDEXDIR")) != NULL) {
  35.         strcpy(dbasedir, s);
  36.         return;
  37.     }
  38.  
  39.     /*
  40.      * Otherwise, it's in the home directory.
  41.      */
  42.     if ((s = getenv("HOME")) == NULL) {
  43.         fprintf(stderr, "%s: cannot get home directory.\n", pname);
  44.         exit(1);
  45.     }
  46.  
  47.     /*
  48.      * Make the name.
  49.      */
  50.     sprintf(dbasedir, "%s/%s", s, INDEXDIR);
  51. }
  52.  
  53. /*
  54.  * dbsort - comparison routine for qsort of database entries.
  55.  */
  56. dbsort(a, b)
  57. struct dbfile *a, *b;
  58. {
  59.     register int i, n;
  60.  
  61.     /*
  62.      * Sort invalid entries to the end.
  63.      */
  64.     if ((a->db_flag & DB_VALID) == 0) {
  65.         if ((b->db_flag & DB_VALID) == 0)
  66.             return(0);
  67.  
  68.         return(1);
  69.     }
  70.  
  71.     if ((b->db_flag & DB_VALID) == 0)
  72.         return(-1);
  73.  
  74.     /*
  75.      * Sort on first field, then try secondary fields.
  76.      */
  77.     n = 0;
  78.     for (i=0; (i < idx.idx_nlines) && (n == 0); i++)
  79.         n = strcmp(a->db_lines[i], b->db_lines[i]);
  80.     
  81.     return(n);
  82. }
  83.  
  84. /*
  85.  * error - reset tty modes and print an error message.
  86.  */
  87. error(fmt, arg1, arg2, arg3)
  88. char *fmt, *arg1, *arg2, *arg3;
  89. {
  90.     reset_modes();
  91.  
  92.     fprintf(stderr, fmt, arg1, arg2, arg3);
  93. }
  94.  
  95. /*
  96.  * savestr - save a string in dynamically allocated memory.
  97.  */
  98. char *
  99. savestr(str)
  100. char *str;
  101. {
  102.     char *s;
  103.     char *malloc();
  104.  
  105.     if ((s = malloc(strlen(str) + 1)) == NULL) {
  106.         reset_modes();
  107.  
  108.         fprintf(stderr, "%s: out of memory.\n", pname);
  109.         exit(1);
  110.     }
  111.  
  112.     strcpy(s, str);
  113.     return(s);
  114. }
  115.  
  116. /*
  117.  * byebye - exit.
  118.  */
  119. byebye()
  120. {
  121.     register char c;
  122.     register int x, y;
  123.  
  124.     /*
  125.      * If the database is modified, see if they really
  126.      * mean to exit without saving.
  127.      */
  128.     if (dbmodified) {
  129.         getyx(curscr, y, x);
  130.         c = prompt_char(y, 0,
  131.                 "Really exit without saving? ",
  132.                 "YyNn");
  133.  
  134.         if ((c == 'n') || (c == 'N'))
  135.             return;
  136.     }
  137.  
  138.     /*
  139.      * Reset tty modes and exit.
  140.      */
  141.     reset_modes();
  142.     exit(0);
  143. }
  144.  
  145. /*
  146.  * usage - print a usage message.
  147.  */
  148. usage()
  149. {
  150.     fprintf(stderr, "Usage: %s [-f filter] [-i] [database] [pattern]\n",
  151.         pname);
  152.     exit(1);
  153. }
  154.