1: #include <X/mit-copyright.h>
   2: 
   3: /* $Header: XGetDefault.c,v 10.5 86/02/01 15:34:44 tony Rel $ */
   4: /* Copyright (c) 1985, Massachusetts Institute of Technology */
   5: 
   6: /*
   7:  * This routine returns options out of the X user preferences file
   8:  * found in the user's home directory.  It either returns a pointer to
   9:  * the option or returns NULL if option not set.  It is patterned after
  10:  * Andrew's file format (why be different for the sake of being different?).
  11:  * Andrew's code was NOT examined before writing this routine.
  12:  * It parses lines of the format "progname.option:value" and returns a pointer
  13:  * to value.
  14:  */
  15: 
  16: #include <stdio.h>
  17: #include <strings.h>
  18: #include <ctype.h>
  19: 
  20: #define XOPTIONFILE "/.Xdefaults"   /* name in home directory of options
  21: 					   file. */
  22: extern char *malloc();
  23: 
  24: static struct ent {
  25:     struct ent *next;       /* next option entry in chain 	*/
  26:     char *oname;            /* option name 			*/
  27:     char *value;            /* value for that option	*/
  28: } *head;                /* head of list of options	*/
  29: 
  30: char *XGetDefault(prog, name)
  31:     register char *name;        /* name of option program wants */
  32:     char *prog;         /* name of program for option	*/
  33: {                   /* to get, for example, "font"  */
  34:     static nent = -1;       /* have we been here before?	*/
  35:     register struct ent *cur;   /* current entry being examined */
  36:     char namebuf[64];
  37:     register char *pv = namebuf;
  38: 
  39:     strncpy(namebuf,name,sizeof(namebuf));
  40:     while (*pv) {           /* convert upper to lower	*/
  41:         if (isupper(*pv))  *pv += 040;
  42:         pv += 1;
  43:         }
  44:     if (nent == -1) nent = ReadFile(prog);/* if not, parse the file.*/
  45:     if (nent == 0) return(NULL);
  46:     cur = head;
  47:     do {
  48:         if (strcmp(namebuf,cur->oname) == 0) return(cur->value);
  49:         cur = cur->next;
  50:     } while (cur != NULL);
  51:     return(NULL);           /* if no match, let him know	*/
  52: }
  53: 
  54: static ReadFile(prog)
  55:     char *prog;         /* program name to match 	    */
  56: {
  57:     FILE *fptr;         /* preferences file		    */
  58:     char fname[BUFSIZ];     /* longer than any conceivable size */
  59:     char line[BUFSIZ];      /* line buffer for each line of file*/
  60:     register char *point,*colon;    /* where in the line the keys are   */
  61:     register struct ent *buf;   /* new memory for valid option line */
  62:     register char *pv;      /* pointer to value for lowering    */
  63:     int len, nchars;
  64:     int nentries = 0;       /* number of entries found	    */
  65:     char *getenv();
  66:     char *home = getenv("HOME");
  67: 
  68:     if (rindex(prog,'/') != NULL) prog = rindex(prog, '/') + 1;
  69:                     /* if full path, get last component */
  70:     if (home == NULL) return(0);
  71: 
  72:     strcpy(fname, home);        /* form full path name of file	*/
  73:     strcat(fname, XOPTIONFILE);
  74: 
  75:     if ((fptr = fopen(fname, "r")) == NULL) return(0);
  76: 
  77:     while ( fgets(line, sizeof(line), fptr) != NULL ) {
  78:         if (line[0] == '#') continue;       /* comment?	*/
  79:         point = index(line,'.');
  80:         colon = index(line,':');
  81:         if ( (point == NULL) || (colon == NULL) || (colon < point) )
  82:             continue;   /* both . and : required on line*/
  83:         if ( point != line )    /* check all chars up to '.' 	*/
  84:             if (strncmp(line, prog, point - line) != 0) continue;
  85: 
  86:         /*
  87: 		 * ok, we've passed all the tests, so it is a valid option for
  88: 		 * this program, or is global option.
  89: 		 */
  90: 
  91:         len = strlen(line);
  92:         if(line[len-1] == '\n') line[len-1] = '\0';
  93:                     /* braindamaged fgets call	*/
  94:         nchars = len - (point - line);
  95:         /*
  96: 		 * allocate space for data structure and text in one call
  97: 		 */
  98:         if ( (buf = (struct ent *)malloc (sizeof(struct ent) + nchars))
  99:              == NULL) {
 100:                 fprintf(stderr, "ReadFile: Out of memory\n");
 101:                 exit(1);
 102:         }
 103:         len += 1;
 104:         *colon = '\0';      /* seperate string		*/
 105:                     /* chain in the new buffer	*/
 106:         bcopy (point+1, buf+1, nchars);
 107:         buf->oname = (char *) (buf + 1);
 108:         buf->value = buf->oname + (colon - point);
 109:         pv = buf->oname;
 110:         while (*pv) {       /* convert upper to lower	*/
 111:             if (isupper(*pv))  *pv += 040;
 112:             pv += 1;
 113:             }
 114:         while(isspace(*buf->value)) buf->value++; /* skip over spaces*/
 115:         buf->next = head;
 116:         head = buf;
 117:         nentries += 1;
 118:     }
 119:     fclose(fptr);
 120:     return(nentries);
 121: }

Defined functions

ReadFile defined in line 54; used 1 times
  • in line 44

Defined variables

head defined in line 28; used 3 times

Defined struct's

ent defined in line 24; used 10 times

Defined macros

XOPTIONFILE defined in line 20; used 1 times
  • in line 73
Last modified: 1986-02-01
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 772
Valid CSS Valid XHTML 1.0 Strict