#include <sys/types.h>
#include <pwd.h>
#include "user.h"
#include "str.h"
#include "error.h"

int user_fromuid(int uid, char *user, unsigned len) {
  struct passwd *p;

  /* XXX should use getpwuid_r? */
  p = getpwuid(uid);
  if(!p)
    return -1;
  if(str_len(p->pw_name)+1 > len) {
    /* XXX should use better? */
    errno = error_nomem;
    return -1;
  }
  str_copy(user, p->pw_name);
  return 0;
}
