/* * Validate that the proposed new value for "name" is valid according to the * defined flags for that variable, if any. */ int env_flags_validate_type(const char *name, const char *value) { enum env_flags_vartype type; if (value == NULL) return 0; type = env_flags_get_type(name); if (_env_flags_validate_type(value, type) < 0) { printf("## Error: flags type check failure for " "\"%s\" <= \"%s\" (type: %c)\n", name, value, env_flags_vartype_rep[type]); return -1; } return 0; }
int env_flags_validate(const ENTRY *item, const char *newval, enum env_op op, int flag) { const char *name; const char *oldval = NULL; if (op != env_op_create) oldval = item->data; name = item->key; /* Default value for NULL to protect string-manipulating functions */ newval = newval ? : ""; /* validate the value to match the variable type */ if (op != env_op_delete) { enum env_flags_vartype type = (enum env_flags_vartype) (ENV_FLAGS_VARTYPE_BIN_MASK & item->flags); if (_env_flags_validate_type(newval, type) < 0) { printf("## Error: flags type check failure for " "\"%s\" <= \"%s\" (type: %c)\n", name, newval, env_flags_vartype_rep[type]); return -1; } } /* check for access permission */ if (flag & H_FORCE) return 0; switch (op) { case env_op_delete: if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_DELETE) { printf("## Error: Can't delete \"%s\"\n", name); return 1; } break; case env_op_overwrite: if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_OVERWR) { printf("## Error: Can't overwrite \"%s\"\n", name); return 1; } else if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR) { const char *defval = getenv_default(name); if (defval == NULL) defval = ""; printf("oldval: %s defval: %s\n", oldval, defval); if (strcmp(oldval, defval) != 0) { printf("## Error: Can't overwrite \"%s\"\n", name); return 1; } } break; case env_op_create: if (item->flags & ENV_FLAGS_VARACCESS_PREVENT_CREATE) { printf("## Error: Can't create \"%s\"\n", name); return 1; } break; } return 0; }