simplify conditionals in aucat_mkcookie()

This commit is contained in:
Alexandre Ratchov 2011-04-28 01:31:15 +02:00
parent cc099911f6
commit a583ebc5d9
1 changed files with 24 additions and 21 deletions

View File

@ -206,7 +206,7 @@ aucat_wdata(struct aucat *hdl, const void *buf, size_t len, unsigned wbpf, int *
} }
int int
aucat_loadcookie(unsigned char *cookie) aucat_mkcookie(unsigned char *cookie)
{ {
struct stat sb; struct stat sb;
char buf[PATH_MAX], tmp[PATH_MAX], *path; char buf[PATH_MAX], tmp[PATH_MAX], *path;
@ -285,27 +285,30 @@ bad_gen:
#endif #endif
/* /*
* save the cookie, ignore errors * try to save the cookie
*/ */
if (path != NULL) { if (path == NULL)
if (strlcpy(tmp, path, PATH_MAX) >= PATH_MAX || return 1;
strlcat(tmp, ".XXXXXXXX", PATH_MAX) >= PATH_MAX) { if (strlcpy(tmp, path, PATH_MAX) >= PATH_MAX ||
DPRINTF("%s: too long\n", path); strlcat(tmp, ".XXXXXXXX", PATH_MAX) >= PATH_MAX) {
return 1; DPRINTF("%s: too long\n", path);
} return 1;
fd = mkstemp(tmp); }
if (fd < 0) { fd = mkstemp(tmp);
DPERROR(tmp); if (fd < 0) {
return 1; DPERROR(tmp);
} return 1;
if (write(fd, cookie, AMSG_COOKIELEN) < 0) { }
DPERROR(tmp); if (write(fd, cookie, AMSG_COOKIELEN) < 0) {
} DPERROR(tmp);
unlink(tmp);
close(fd); close(fd);
if (rename(tmp, path) < 0) { return 1;
DPERROR(tmp); }
unlink(tmp); close(fd);
} if (rename(tmp, path) < 0) {
DPERROR(tmp);
unlink(tmp);
} }
return 1; return 1;
} }
@ -449,7 +452,7 @@ aucat_open(struct aucat *hdl, const char *str, unsigned mode, int isaudio)
*/ */
AMSG_INIT(&hdl->wmsg); AMSG_INIT(&hdl->wmsg);
hdl->wmsg.cmd = AMSG_AUTH; hdl->wmsg.cmd = AMSG_AUTH;
if (!aucat_loadcookie(hdl->wmsg.u.auth.cookie)) if (!aucat_mkcookie(hdl->wmsg.u.auth.cookie))
goto bad_connect; goto bad_connect;
hdl->wtodo = sizeof(struct amsg); hdl->wtodo = sizeof(struct amsg);
if (!aucat_wmsg(hdl, &eof)) if (!aucat_wmsg(hdl, &eof))