+char* buf;
+int buflen;
+char tmpbuf[BUFSIZE];
+int tmpbufpos;
+int tmpread;
+
+char*
+read_line(int fd)
+{
+ if (! buflen)
+ buf = xmalloc(BUFSIZE);
+ char* p = buf;
+ do {
+ if (tmpbufpos >= tmpread) {
+ tmpread = read(fd, tmpbuf, BUFSIZE);
+ tmpbufpos = 0;
+ }
+ if (!tmpread)
+ return NULL;
+ if (p >= (buf + buflen - 1))
+ buf = xrealloc(buf, buflen *= 2);
+ *p++ = tmpbuf[tmpbufpos++];
+ } while (*p != '\n');
+ *++p = 0;
+
+ return buf;
+}
+
+char*
+get_home_from_passwd(void)
+{
+ struct passwd* pas;
+ pas = getpwuid(getuid());
+ if (! pas)
+ die("Cannot get uid, please specify config file path");
+
+ char* login = pas->pw_name;
+ int len = strlen(pas->pw_name);
+ char* buf;
+ char* p, *q;
+ int r, i;
+
+ int fd;
+ fd = open("/etc/passwd", O_RDONLY);
+ if (fd < 0)
+ die("Cannot open /etc/passwd, please specify config file path");
+
+ do {
+ buf = read_line(fd);
+ r = strncmp(buf, login, len);
+ if (!r)
+ break;
+ } while (buf);
+ if (!r)
+ die("Cannot find your login %s in /etc/passwd, please specify config file path", login);
+ p = buf;
+ for (i = 0; i < 5; i++) {
+ p = strchr(p, ':');
+ if (! p)
+ die("Cannot parse /etc/passwd, please specify config file path");
+ }
+ q = p + 1;
+ while (*q && *q != '\n' && *q != ':')
+ q++;
+ *q = 0;
+
+ return xstrdup(p + 1);
+}
+