util: add allocation loop to gettyname_malloc()

This commit is contained in:
Lennart Poettering 2014-12-23 02:10:08 +01:00
parent 611b312b7d
commit 9d8c4979c0
1 changed files with 28 additions and 12 deletions

View File

@ -2785,23 +2785,36 @@ char *getusername_malloc(void) {
return lookup_uid(getuid()); return lookup_uid(getuid());
} }
int getttyname_malloc(int fd, char **r) { int getttyname_malloc(int fd, char **ret) {
char path[PATH_MAX], *c; size_t l = 100;
int k; int r;
assert(r); assert(fd >= 0);
assert(ret);
k = ttyname_r(fd, path, sizeof(path)); for (;;) {
if (k > 0) char path[l];
return -k;
char_array_0(path); r = ttyname_r(fd, path, sizeof(path));
if (r == 0) {
const char *p;
char *c;
c = strdup(startswith(path, "/dev/") ? path + 5 : path); p = startswith(path, "/dev/");
if (!c) c = strdup(p ?: path);
return -ENOMEM; if (!c)
return -ENOMEM;
*ret = c;
return 0;
}
if (r != ERANGE)
return -r;
l *= 2;
}
*r = c;
return 0; return 0;
} }
@ -7437,6 +7450,9 @@ int sethostname_idempotent(const char *s) {
int ptsname_malloc(int fd, char **ret) { int ptsname_malloc(int fd, char **ret) {
size_t l = 100; size_t l = 100;
assert(fd >= 0);
assert(ret);
for (;;) { for (;;) {
char *c; char *c;