unsigned int getdef_unum (const char *item, unsigned int dflt) { struct itemdef *d; long val; if (!def_loaded) { def_load (); } d = def_find (item); if ((NULL == d) || (NULL == d->value)) { return dflt; } if ( (getlong (d->value, &val) == 0) || (val < 0) || (val > INT_MAX)) { fprintf (stderr, _("configuration error - cannot parse %s value: '%s'"), item, d->value); return dflt; } return (unsigned int) val; }
int main (int argc, char **argv) { int i; char *cp; struct itemdef *d; def_load (); for (i = 0; i < NUMDEFS; ++i) { d = def_find (def_table[i].name); if (NULL == d) { printf ("error - lookup '%s' failed\n", def_table[i].name); } else { printf ("%4d %-24s %s\n", i + 1, d->name, d->value); } } for (i = 1; i < argc; i++) { cp = getdef_str (argv[1]); if (NULL != cp) { printf ("%s `%s'\n", argv[1], cp); } else { printf ("%s not found\n", argv[1]); } } exit (EXIT_SUCCESS); }
char * def_install(default_t *dp, char *key, char *val) { int hval = def_hash(key,dp->nsymtab); char *xp; if (xp=def_find(dp, key)) { return 0; } if (dp->symtab[hval].key) { struct syment *sp = malloc(sizeof *sp); if (!sp) { return 0; } /* printf("1 STRDUP key %s length %d \n", (key) ? key : "!NULL!", (key) ? strlen(key) : -1); fflush(stdout); */ if (!(sp->key = strdup(key))) { free(sp); return 0; } /* printf("1 STRDUP val %s length %d \n", (val) ? val : "!NULL!", (val) ? strlen(val) : -1); fflush(stdout); */ if (!(sp->val = strdup(val))) { free(sp->key); free(sp); return 0; } sp->next = dp->symtab[hval].next; dp->symtab[hval].next = sp; return sp->val; } /* printf("2 STRDUP key %s length %d \n", (key) ? key : "!NULL!", (key) ? strlen(key) : -1); fflush(stdout); */ if (!(dp->symtab[hval].key = strdup(key))) { return 0; } /* printf("2 STRDUP val %s length %d \n", (val) ? val : "!NULL!", (val) ? strlen(val) : -1); fflush(stdout); */ if (!(dp->symtab[hval].val = strdup(val))) { free(dp->symtab[hval].key); dp->symtab[hval].key = 0; return 0; } return dp->symtab[hval].val; }
/* * getdef_str - get string value from table of definitions. * * Return point to static data for specified item, or NULL if item is not * defined. First time invoked, will load definitions from the file. */ char *getdef_str(const char *item) { struct itemdef *d; if (!def_loaded) def_load(); return ((d = def_find(item)) == NULL ? (char *)NULL : d->value); }
/*@observer@*/ /*@null@*/const char *getdef_str (const char *item) { struct itemdef *d; if (!def_loaded) { def_load (); } d = def_find (item); return ((NULL == d)? (const char *) NULL : d->value); }
/* * getdef_long - get long integer value from table of definitions * * Returns numeric value of specified item, else the "dflt" value if * the item is not defined. Octal (leading "0") and hex (leading "0x") * values are handled. */ long getdef_long(const char *item, long dflt) { struct itemdef *d; if (!def_loaded) def_load(); if ((d = def_find(item)) == NULL || d->value == NULL) return dflt; return strtol(d->value, (char **)NULL, 0); }
/* * getdef_bool - get boolean value from table of definitions. * * Return TRUE if specified item is defined as "yes", else FALSE. */ int getdef_bool(const char *item) { struct itemdef *d; if (!def_loaded) def_load(); if ((d = def_find(item)) == NULL || d->value == NULL) return 0; return (strcmp(d->value, "yes") == 0); }
bool getdef_bool (const char *item) { struct itemdef *d; if (!def_loaded) { def_load (); } d = def_find (item); if ((NULL == d) || (NULL == d->value)) { return false; } return (strcasecmp (d->value, "yes") == 0); }
int main(int argc, char **argv) { int i; char *cp; struct itemdef *d; def_load (); for (i = 0 ; i < NUMDEFS ; ++i) { if ((d = def_find(def_table[i].name)) == NULL) printf("error - lookup '%s' failed\n", def_table[i].name); else printf("%4d %-24s %s\n", i+1, d->name, d->value); } for (i = 1;i < argc;i++) { if (cp = getdef_str (argv[1])) printf ("%s `%s'\n", argv[1], cp); else printf ("%s not found\n", argv[1]); } exit(0); }
/* Handle ifdef and ifndef directives. */ static int ifdef_directive( struct fileinfo_s *pfi ) { int ifndef, defined; const char *pname; int deflen; ifndef = ( pfi->srcleft > 6 && strncmp( pfi->sp, "ifndef", 6 ) == 0 ); while ( pfi->sp[0] && pfi->srcleft > 0 && ! isspace( pfi->sp[0] ) ) { pfi->sp++; pfi->srcleft--; } if ( pfi->srcleft > 0 && isspace( pfi->sp[0] ) ) { pfi->sp++; pfi->srcleft--; } pname = pfi->sp; deflen = getdeflen( pfi->sp, pfi->srcleft ); if ( deflen < 1 || (pfi->sp[deflen] != '\n' && ! isspace( pfi->sp[deflen])) ) { fprintf( stderr, "bad `ifdef directive.\n" ); return -1; } pfi->sp += deflen; pfi->srcleft -= deflen; defined = def_find( pfi->deftab, pname, deflen ) != NULL; pfi->ifdefd_out <<= 1; if ( ifndef && defined || ! ifndef && ! defined ) pfi->ifdefd_out |= 1; kill_eol( pfi ); return 0; }
long getdef_long (const char *item, long dflt) { struct itemdef *d; long val; if (!def_loaded) { def_load (); } d = def_find (item); if ((NULL == d) || (NULL == d->value)) { return dflt; } if (getlong (d->value, &val) == 0) { fprintf (stderr, _("configuration error - cannot parse %s value: '%s'"), item, d->value); return dflt; } return val; }
int putdef_str (const char *name, const char *value) { struct itemdef *d; char *cp; if (!def_loaded) { def_load (); } /* * Locate the slot to save the value. If this parameter * is unknown then "def_find" will print an err message. */ d = def_find (name); if (NULL == d) { return -1; } /* * Save off the value. */ cp = strdup (value); if (NULL == cp) { (void) fputs (_("Could not allocate space for config info.\n"), stderr); SYSLOG ((LOG_ERR, "could not allocate space for config info")); return -1; } if (NULL != d->value) { free (d->value); } d->value = cp; return 0; }
unsigned long getdef_ulong (const char *item, unsigned long dflt) { struct itemdef *d; unsigned long val; if (!def_loaded) { def_load (); } d = def_find (item); if ((NULL == d) || (NULL == d->value)) { return dflt; } if (getulong (d->value, &val) == 0) { /* FIXME: we should have a getulong */ fprintf (stderr, _("configuration error - cannot parse %s value: '%s'"), item, d->value); return dflt; } return val; }
/* * def_load - load configuration table * * Loads the user-configured options from the default configuration file */ void def_load(void) { int i; FILE *fp; struct itemdef *d; char buf[BUFSIZ], def_fname[PATH_MAX], *name, *value, *s; struct passwd *pw; /* * Get MBSE BBS root directory */ if ((pw = getpwnam("mbse")) == NULL) { syslog(LOG_CRIT, "cannot find user `mbse' in password file"); return; } snprintf(def_fname, PATH_MAX, "%s/etc/login.defs", pw->pw_dir); /* * Open the configuration definitions file. */ if ((fp = fopen(def_fname, "r")) == NULL) { syslog(LOG_CRIT, "cannot open login definitions %s [%m]", def_fname); return; } /* * Go through all of the lines in the file. */ while (fgets(buf, sizeof(buf), fp) != NULL) { /* * Trim trailing whitespace. */ for (i = strlen(buf)-1 ; i >= 0 ; --i) { if (!isspace(buf[i])) break; } buf[++i] = '\0'; /* * Break the line into two fields. */ name = buf + strspn(buf, " \t"); /* first nonwhite */ if (*name == '\0' || *name == '#') continue; /* comment or empty */ s = name + strcspn(name, " \t"); /* end of field */ if (*s == '\0') continue; /* only 1 field?? */ *s++ = '\0'; value = s + strspn(s, " \"\t"); /* next nonwhite */ *(value + strcspn(value, "\"")) = '\0'; /* * Locate the slot to save the value. If this parameter * is unknown then "def_find" will print an err message. */ if ((d = def_find(name)) == NULL) continue; /* * Save off the value. */ if ((d->value = strdup(value)) == NULL) { fprintf(stderr, "getdef: Could not allocate space for config info.\n"); syslog(LOG_ERR, "getdef: could not allocate space for config info"); break; } } (void) fclose(fp); /* * Set the initialized flag. */ ++def_loaded; }