Diff for /libaitcfg/src/pq.c between versions 1.1.2.1 and 1.1.2.2

version 1.1.2.1, 2012/09/18 15:50:59 version 1.1.2.2, 2012/09/19 11:47:39
Line 47  SUCH DAMAGE. Line 47  SUCH DAMAGE.
   
   
 static inline struct tagUser *  static inline struct tagUser *
_selectAttribute(pwd_root_t * __restrict pwd, u_int uid, const char *csName)_selectPasswd(pwd_root_t * __restrict pwd, u_int uid, const char *csName)
 {  {
         struct tagUser fu;          struct tagUser fu;
   
Line 144  cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite Line 144  cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite
         va_start(lst, criteria);          va_start(lst, criteria);
         switch (criteria) {          switch (criteria) {
                 case PWD_CRIT_NAME:                  case PWD_CRIT_NAME:
                        u = _selectAttribute(pwd, 0, va_arg(lst, char*));                        u = _selectPasswd(pwd, 0, va_arg(lst, char*));
                         break;                          break;
                 case PWD_CRIT_UID:                  case PWD_CRIT_UID:
                        u = _selectAttribute(pwd, va_arg(lst, u_int), NULL);                        u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
                         break;                          break;
                 default:                  default:
                         va_end(lst);                          va_end(lst);
Line 176  cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite Line 176  cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite
         return 1;          return 1;
 }  }
   
 #if 0  
 /*  /*
 * cfg_setAttribute() - Set item in config or adding new item if not exists * cfg_setPasswd() - Set item in password or adding new item if not exists
  *   *
 * @cfg = Config root * @cfg = Password root
 * @csSec = Config section //[{csSec}], if NULL set in *default* section * @csName = Username
 * @csAttr = Config attribute //{csAttr} = ... * @csPass = Password
 * @csVal = Config value //... = {csVal} to setup * @uid = UID
  * @gid = GID
  * @Class = Login class
  * @change = Chage date
  * @expire = Expire date
  * @csRealm = Realm
  * @csHome = Home dir
  * @csShell = Shell
  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item   * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  */   */
 int  int
cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)cfg_setPasswd(pwd_root_t * __restrict pwd, const char *csName, const char *csPass, 
                 u_int uid, u_int gid, const char *csClass, u_int change, u_int expire, 
                 const char *csRealm, const char *csHome, const char *csShell)
 {  {
        struct tagCfg *av, *section;        struct tagUser *u;
   
        if (!cfg || !csAttr)        if (!pwd || !csName)
                 return -1;                  return -1;
   
        av = _selectAttribute(cfg, csSec, csAttr);        u = _selectPasswd(pwd, 0, csName);
        if (!av) {        if (!u) {
                 /* adding new element */                  /* adding new element */
                section = _selectAttribute(cfg, csSec, NULL);                u = io_malloc(sizeof(struct tagUser));
                if (!u) {
                av = io_malloc(sizeof(struct tagCfg));                        cfg_SetErr(io_GetErrno(), "%s", io_GetError());
                if (!av) { 
                        LOGERR; 
                         return -1;                          return -1;
                 } else {                  } else {
                        memset(av, 0, sizeof(struct tagCfg));                        memset(u, 0, sizeof(struct tagUser));
   
                        CFG_RC_LOCK(cfg);                        PWD_LOCK(pwd);
                        if (!section)                        SLIST_INSERT_HEAD(pwd, u, usr_next);
                                SLIST_INSERT_HEAD(cfg, av, cfg_next);                        PWD_UNLOCK(pwd);
                        else 
                                SLIST_INSERT_AFTER(section, av, cfg_next); 
                        CFG_RC_UNLOCK(cfg); 
                 }                  }
   
                if (csSec && *csSec) {                AIT_SET_STR(&u->usr_name, csName);
                        AIT_SET_STR(&av->cfg_sec, csSec);                AIT_SET_STR(&u->usr_pass, csPass);
                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),                 AIT_SET_U32(&u->usr_uid, uid);
                                        io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);                AIT_SET_U32(&u->usr_gid, gid);
                }                AIT_SET_STR(&u->usr_class, csClass);
                AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");                AIT_SET_U32(&u->usr_change, change);
                AIT_SET_STR(&av->cfg_attr, csAttr);                AIT_SET_U32(&u->usr_expire, expire);
                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),                 AIT_SET_STR(&u->usr_realm, csRealm);
                                io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);                AIT_SET_STR(&u->usr_home, csHome);
                 AIT_SET_STR(&u->usr_shell, csShell);
   
                CFG_RC_LOCK(cfg);                AIT_KEY(&u->usr_name) = crcFletcher16(AIT_GET_LIKE(&u->usr_name, u_short*), 
                RB_INSERT(tagRC, cfg, av);                                io_align(AIT_LEN(&u->usr_name) - 1, 2) / 2);
                CFG_RC_UNLOCK(cfg);
                 PWD_LOCK(pwd);
                 RB_INSERT(tagPWD, pwd, u);
                 PWD_UNLOCK(pwd);
                 return 2;                  return 2;
        }        } else {
                 AIT_FREE_VAL(&u->usr_pass);
                 AIT_FREE_VAL(&u->usr_uid);
                 AIT_FREE_VAL(&u->usr_gid);
                 AIT_FREE_VAL(&u->usr_class);
                 AIT_FREE_VAL(&u->usr_change);
                 AIT_FREE_VAL(&u->usr_expire);
                 AIT_FREE_VAL(&u->usr_realm);
                 AIT_FREE_VAL(&u->usr_home);
                 AIT_FREE_VAL(&u->usr_shell);
   
         if (csVal && AIT_ADDR(&av->cfg_val) &&   
                         strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {  
                 /* Update element */                  /* Update element */
                AIT_FREE_VAL(&av->cfg_val);                AIT_SET_STR(&u->usr_pass, csPass);
                AIT_SET_STR(&av->cfg_val, csVal);                AIT_SET_U32(&u->usr_uid, uid);
                 AIT_SET_U32(&u->usr_gid, gid);
                 AIT_SET_STR(&u->usr_class, csClass);
                 AIT_SET_U32(&u->usr_change, change);
                 AIT_SET_U32(&u->usr_expire, expire);
                 AIT_SET_STR(&u->usr_realm, csRealm);
                 AIT_SET_STR(&u->usr_home, csHome);
                 AIT_SET_STR(&u->usr_shell, csShell);
                 return 1;                  return 1;
         }          }
   
Line 243  cfg_setAttribute(cfg_root_t * __restrict cfg, const ch Line 264  cfg_setAttribute(cfg_root_t * __restrict cfg, const ch
 }  }
   
 /*  /*
 * cfg_getAttribute() - Get item from config and return value from it * cfg_getPasswd() - Get item from passwords and return structure from it
  *   *
 * @cfg = Config root * @pwd = Password root
 * @csSec = Config section //[{csSec}], if NULL unset in *default* section * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
 * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute * @arg1 = Username | UID
 * return: NULL item not found or null parameters, !=NULL value const string * return: NULL item not found, !=NULL structure found
  */   */
inline const char *inline const struct tagUser *
cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
 {  {
        struct tagCfg *av;        struct tagUser *u;
         va_list lst;
   
        if (!cfg || !csAttr)        if (!pwd)
                 return NULL;                  return NULL;
   
        av = _selectAttribute(cfg, csSec, csAttr);        va_start(lst, criteria);
        if (!av)        switch (criteria) {
                return NULL;                case PWD_CRIT_NAME:
                        u = _selectPasswd(pwd, 0, va_arg(lst, char*));
        return AIT_GET_STR(&av->cfg_val);                        break;
}                case PWD_CRIT_UID:
                        u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
/*                        break;
 * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*                default:
 *                        u = NULL;
 * @cfg = Config root                        break;
 * @csSec = Config section //[{csSec}], if NULL unset in *default* section 
 * @csAttr = Config attribute //{csAttr} = ... 
 * @val = Return buffer for item Value //... = {val} 
 * @csDefValue = *Default Value* for return in //{val}, if not found item in config 
 * return: 0 item not found, -1 error or >0 number of copied bytes in //{val} 
 */ 
int 
cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr,  
                ait_val_t * __restrict val, const char *csDefValue) 
{ 
        struct tagCfg *av; 
        int ret = 0; 
 
        if (!cfg || !csAttr || !val) { 
                cfg_SetErr(EINVAL, "Invalid argument(s)"); 
                return -1; 
         }          }
           va_end(lst);
   
        AIT_INIT_VAL(val);        return u;
        av = _selectAttribute(cfg, csSec, csAttr); 
        if (!av) { 
                /* not found item */ 
                if (csDefValue) { 
                        AIT_SET_STR(val, csDefValue); 
                        ret = AIT_LEN(val); 
                } else 
                        AIT_INIT_VAL(val); 
                return ret; 
        } 
 
        if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) ||  
                        !*AIT_GET_LIKE(&av->cfg_val, char*)) { 
                /* empty value */ 
                if (csDefValue) { 
                        AIT_SET_STR(val, csDefValue); 
                        ret = AIT_LEN(val); 
                } else 
                        AIT_INIT_VAL(val); 
        } else { 
                /* copy value */ 
                AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val)); 
                ret = AIT_LEN(val); 
        } 
 
        return ret; 
 }  }
 #endif  

Removed from v.1.1.2.1  
changed lines
  Added in v.1.1.2.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>