home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / FREEZE-2.ZIP / default.c < prev    next >
C/C++ Source or Header  |  1992-07-18  |  1KB  |  61 lines

  1. #include "freeze.h"
  2.  
  3.         /*-------------------------------*/
  4.         /*     DEFAULTS FILE HANDLING    */
  5.         /*-------------------------------*/
  6.  
  7. #define OK      0
  8. #define FAIL    NULL
  9. #define NOFILE  ((FILE *) 0)
  10. #define MAXLINE 128
  11.  
  12. extern int      errno;
  13. char            *strchr();
  14. static FILE     *defd = NOFILE;  /* defaults file stream */
  15.  
  16. int     defopen(fname)          /* open | reopen | close defaults file */
  17.     char    *fname;
  18. {
  19.     register FILE   *fd;
  20.  
  21.     if (!fname) {
  22.         if (defd)
  23.             (void) fclose(defd);
  24.         defd = NOFILE;
  25.         return OK;
  26.     }
  27.  
  28.     if (!(fd = fopen(fname, "r")))
  29.         return errno;                   /* problems opening file */
  30.  
  31.     defd = fd;
  32.     return OK;
  33. }
  34.  
  35. static char     defline[MAXLINE + 1];
  36.  
  37. char    *defread(pattern)
  38.     register char   *pattern;
  39. {
  40.     register        sz_patt;
  41.     register char   *cp;
  42.  
  43.     if (!defd)
  44.         return FAIL;            /* defaults file not opened */
  45.  
  46.     rewind(defd);
  47.     sz_patt = strlen(pattern);
  48.  
  49.     while (fgets(defline, MAXLINE, defd)) {
  50.         if (!(cp = strchr(defline, '\n')))
  51.             return FAIL;     /* line too long  */
  52.         if (cp - defline < sz_patt)
  53.             continue;       /* line too short */
  54.         *cp = '\0';
  55.         if (!strncmp(pattern, defline, sz_patt))
  56.             return defline + sz_patt;       /* match found */
  57.     }
  58.  
  59.     return FAIL;                    /* no matching lines */
  60. }
  61.