'시스템프로그래밍'에 해당되는 글 1건

  1. 2007/04/24 uid로 username찾기.
2007/04/24 03:11

uid로 username찾기.

gid로 그룹명 따오는거 까진 문제가 없었는데 uid로 유저명을 찾아오는데서 막혀버렸다.
결국 ls 소스를 뒤져서 답을 찾았다.

char *
getuser (uid_t uid)
{
  struct userid *tail;
  struct userid *match = NULL;

  for (tail = user_alist; tail; tail = tail->next)
    {
      if (tail->id.u == uid)
 {
   match = tail;
   break;
 }
    }

  if (match == NULL)
    {
      struct passwd *pwent = getpwuid (uid);
      char const *name = pwent ? pwent->pw_name : "";
      match = xmalloc (offsetof (struct userid, name) + strlen (name) + 1);
      match->id.u = uid;
      strcpy (match->name, name);

      /* Add to the head of the list, so most recently used is first.  */
      match->next = user_alist;
      user_alist = match;
    }

  return match->name[0] ? match->name : NULL;
}

내가 네트웍 프로그래밍을 듣는 건지 시스템 프로그래밍을 듣는 건지.
점점 뻘짓으로 빠져가는 듯 하다.

크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 Comment 0