LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 638|回复: 2

请帮看一下这段代码

[复制链接]
发表于 2005-10-13 10:40:55 | 显示全部楼层 |阅读模式
这段代码是gnu coreutils-5.2.1 的 id 的源代码。
注意红色部分。

static int
xgetgroups (const char *username, gid_t gid, int *n_groups,
            GETGROUPS_T **groups)
{
  int max_n_groups;
  int ng;
  GETGROUPS_T *g;
  int fail = 0;

  if (username == 0)
    max_n_groups = getgroups (0, NULL);
  else
    max_n_groups = getugroups (0, NULL, username, gid);

  /* Add 1 just in case max_n_groups is zero.  */
  g = xmalloc (max_n_groups * sizeof (GETGROUPS_T) + 1);
  if (username == 0)
    ng = getgroups (max_n_groups, g);
  else
    ng = getugroups (max_n_groups, g, username, gid);

  if (ng < 0)
    {
      error (0, errno, _("cannot get supplemental group list"));
      ++fail;
      free (groups);
    }
  if (!fail)
    {
      *n_groups = ng;
      *groups = g;
    }
  return fail;
}


free(groups)  是否应该是 free(g)
发表于 2005-10-13 12:06:57 | 显示全部楼层
You're right, please have a look at the 5.3.0 version:

  1.     252 static bool
  2.     253 xgetgroups (const char *username, gid_t gid, int *n_groups,
  3.     254         GETGROUPS_T **groups)
  4.     255 {
  5.     256   int max_n_groups;
  6.     257   int ng;
  7.     258   GETGROUPS_T *g = NULL;
  8.     259
  9.     260   if (!username)
  10.     261     max_n_groups = getgroups (0, NULL);
  11.     262   else
  12.     263     max_n_groups = getugroups (0, NULL, username, gid);
  13.     264
  14.     265   if (max_n_groups < 0)
  15.     266     ng = -1;
  16.     267   else
  17.     268     {
  18.     269       g = xnmalloc (max_n_groups, sizeof *g);
  19.     270       if (!username)
  20.     271     ng = getgroups (max_n_groups, g);
  21.     272       else
  22.     273     ng = getugroups (max_n_groups, g, username, gid);
  23.     274     }
  24.     275
  25.     276   if (ng < 0)
  26.     277     {
  27.     278       error (0, errno, _("cannot get supplemental group list"));
  28.     279       free (g);
  29.     280       return false;
  30.     281     }
  31.     282   else
  32.     283     {
  33.     284       *n_groups = ng;
  34.     285       *groups = g;
  35.     286       return true;
  36.     287     }
  37.     288 }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-13 12:41:04 | 显示全部楼层
谢谢.
马上下5.3.0来看看。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表