mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-01 11:32:48 -05:00
Rename 'ret' variables passed from allocation to return.
I mentioned recently (in commit 9e7d4c53d8
) message that I'm no
longer fond of the variable name 'ret', because it's used in two quite
different contexts: it's the return value from a subroutine you just
called (e.g. 'int ret = read(fd, buf, len);' and then check for error
or EOF), or it's the value you're preparing to return from the
_containing_ routine (maybe by assigning it a default value and then
conditionally modifying it, or by starting at NULL and reallocating,
or setting it just before using the 'goto out' cleanup idiom). In the
past I've occasionally made mistakes by forgetting which meaning the
variable had, or accidentally conflating both uses.
If all else fails, I now prefer 'retd' (short for 'returned') in the
former situation, and 'toret' (obviously, the value 'to return') in
the latter case. But even better is to pick a name that actually says
something more specific about what the thing actually is.
One particular bad habit throughout this codebase is to have a set of
functions that deal with some object type (say 'Foo'), all *but one*
of which take a 'Foo *foo' parameter, but the foo_new() function
starts with 'Foo *ret = snew(Foo)'. If all the rest of them think the
canonical name for the ambient Foo is 'foo', so should foo_new()!
So here's a no-brainer start on cutting down on the uses of 'ret': I
looked for all the cases where it was being assigned the result of an
allocation, and renamed the variable to be a description of the thing
being allocated. In the case of a new() function belonging to a
family, I picked the same name as the rest of the functions in its own
family, for consistency. In other cases I picked something sensible.
One case where it _does_ make sense not to use your usual name for the
variable type is when you're cloning an existing object. In that case,
_neither_ of the Foo objects involved should be called 'foo', because
it's ambiguous! They should be named so you can see which is which. In
the two cases I found here, I've called them 'orig' and 'copy'.
As in the previous refactoring, many thanks to clang-rename for the
help.
This commit is contained in:
342
unix/network.c
342
unix/network.c
@ -271,18 +271,18 @@ SockAddr *sk_namelookup(const char *host, char **canonicalname,
|
||||
|
||||
SockAddr *sk_nonamelookup(const char *host)
|
||||
{
|
||||
SockAddr *ret = snew(SockAddr);
|
||||
ret->error = NULL;
|
||||
ret->superfamily = UNRESOLVED;
|
||||
strncpy(ret->hostname, host, lenof(ret->hostname));
|
||||
ret->hostname[lenof(ret->hostname)-1] = '\0';
|
||||
SockAddr *addr = snew(SockAddr);
|
||||
addr->error = NULL;
|
||||
addr->superfamily = UNRESOLVED;
|
||||
strncpy(addr->hostname, host, lenof(addr->hostname));
|
||||
addr->hostname[lenof(addr->hostname)-1] = '\0';
|
||||
#ifndef NO_IPV6
|
||||
ret->ais = NULL;
|
||||
addr->ais = NULL;
|
||||
#else
|
||||
ret->addresses = NULL;
|
||||
#endif
|
||||
ret->refcount = 1;
|
||||
return ret;
|
||||
addr->refcount = 1;
|
||||
return addr;
|
||||
}
|
||||
|
||||
static bool sk_nextaddr(SockAddr *addr, SockAddrStep *step)
|
||||
@ -505,42 +505,42 @@ static const SocketVtable NetSocket_sockvt = {
|
||||
static Socket *sk_net_accept(accept_ctx_t ctx, Plug *plug)
|
||||
{
|
||||
int sockfd = ctx.i;
|
||||
NetSocket *ret;
|
||||
NetSocket *s;
|
||||
|
||||
/*
|
||||
* Create NetSocket structure.
|
||||
*/
|
||||
ret = snew(NetSocket);
|
||||
ret->sock.vt = &NetSocket_sockvt;
|
||||
ret->error = NULL;
|
||||
ret->plug = plug;
|
||||
bufchain_init(&ret->output_data);
|
||||
ret->writable = true; /* to start with */
|
||||
ret->sending_oob = 0;
|
||||
ret->frozen = true;
|
||||
ret->localhost_only = false; /* unused, but best init anyway */
|
||||
ret->pending_error = 0;
|
||||
ret->oobpending = false;
|
||||
ret->outgoingeof = EOF_NO;
|
||||
ret->incomingeof = false;
|
||||
ret->listener = false;
|
||||
ret->parent = ret->child = NULL;
|
||||
ret->addr = NULL;
|
||||
ret->connected = true;
|
||||
s = snew(NetSocket);
|
||||
s->sock.vt = &NetSocket_sockvt;
|
||||
s->error = NULL;
|
||||
s->plug = plug;
|
||||
bufchain_init(&s->output_data);
|
||||
s->writable = true; /* to start with */
|
||||
s->sending_oob = 0;
|
||||
s->frozen = true;
|
||||
s->localhost_only = false; /* unused, but best init anyway */
|
||||
s->pending_error = 0;
|
||||
s->oobpending = false;
|
||||
s->outgoingeof = EOF_NO;
|
||||
s->incomingeof = false;
|
||||
s->listener = false;
|
||||
s->parent = s->child = NULL;
|
||||
s->addr = NULL;
|
||||
s->connected = true;
|
||||
|
||||
ret->s = sockfd;
|
||||
s->s = sockfd;
|
||||
|
||||
if (ret->s < 0) {
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
if (s->s < 0) {
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
ret->oobinline = false;
|
||||
s->oobinline = false;
|
||||
|
||||
uxsel_tell(ret);
|
||||
add234(sktree, ret);
|
||||
uxsel_tell(s);
|
||||
add234(sktree, s);
|
||||
|
||||
return &ret->sock;
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
static int try_connect(NetSocket *sock)
|
||||
@ -750,51 +750,51 @@ static int try_connect(NetSocket *sock)
|
||||
Socket *sk_new(SockAddr *addr, int port, bool privport, bool oobinline,
|
||||
bool nodelay, bool keepalive, Plug *plug)
|
||||
{
|
||||
NetSocket *ret;
|
||||
NetSocket *s;
|
||||
int err;
|
||||
|
||||
/*
|
||||
* Create NetSocket structure.
|
||||
*/
|
||||
ret = snew(NetSocket);
|
||||
ret->sock.vt = &NetSocket_sockvt;
|
||||
ret->error = NULL;
|
||||
ret->plug = plug;
|
||||
bufchain_init(&ret->output_data);
|
||||
ret->connected = false; /* to start with */
|
||||
ret->writable = false; /* to start with */
|
||||
ret->sending_oob = 0;
|
||||
ret->frozen = false;
|
||||
ret->localhost_only = false; /* unused, but best init anyway */
|
||||
ret->pending_error = 0;
|
||||
ret->parent = ret->child = NULL;
|
||||
ret->oobpending = false;
|
||||
ret->outgoingeof = EOF_NO;
|
||||
ret->incomingeof = false;
|
||||
ret->listener = false;
|
||||
ret->addr = addr;
|
||||
START_STEP(ret->addr, ret->step);
|
||||
ret->s = -1;
|
||||
ret->oobinline = oobinline;
|
||||
ret->nodelay = nodelay;
|
||||
ret->keepalive = keepalive;
|
||||
ret->privport = privport;
|
||||
ret->port = port;
|
||||
s = snew(NetSocket);
|
||||
s->sock.vt = &NetSocket_sockvt;
|
||||
s->error = NULL;
|
||||
s->plug = plug;
|
||||
bufchain_init(&s->output_data);
|
||||
s->connected = false; /* to start with */
|
||||
s->writable = false; /* to start with */
|
||||
s->sending_oob = 0;
|
||||
s->frozen = false;
|
||||
s->localhost_only = false; /* unused, but best init anyway */
|
||||
s->pending_error = 0;
|
||||
s->parent = s->child = NULL;
|
||||
s->oobpending = false;
|
||||
s->outgoingeof = EOF_NO;
|
||||
s->incomingeof = false;
|
||||
s->listener = false;
|
||||
s->addr = addr;
|
||||
START_STEP(s->addr, s->step);
|
||||
s->s = -1;
|
||||
s->oobinline = oobinline;
|
||||
s->nodelay = nodelay;
|
||||
s->keepalive = keepalive;
|
||||
s->privport = privport;
|
||||
s->port = port;
|
||||
|
||||
do {
|
||||
err = try_connect(ret);
|
||||
} while (err && sk_nextaddr(ret->addr, &ret->step));
|
||||
err = try_connect(s);
|
||||
} while (err && sk_nextaddr(s->addr, &s->step));
|
||||
|
||||
if (err)
|
||||
ret->error = strerror(err);
|
||||
s->error = strerror(err);
|
||||
|
||||
return &ret->sock;
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
bool local_host_only, int orig_address_family)
|
||||
{
|
||||
int s;
|
||||
int fd;
|
||||
#ifndef NO_IPV6
|
||||
struct addrinfo hints, *ai = NULL;
|
||||
char portstr[6];
|
||||
@ -802,7 +802,7 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
union sockaddr_union u;
|
||||
union sockaddr_union *addr;
|
||||
int addrlen;
|
||||
NetSocket *ret;
|
||||
NetSocket *s;
|
||||
int retcode;
|
||||
int address_family;
|
||||
int on = 1;
|
||||
@ -810,23 +810,23 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
/*
|
||||
* Create NetSocket structure.
|
||||
*/
|
||||
ret = snew(NetSocket);
|
||||
ret->sock.vt = &NetSocket_sockvt;
|
||||
ret->error = NULL;
|
||||
ret->plug = plug;
|
||||
bufchain_init(&ret->output_data);
|
||||
ret->writable = false; /* to start with */
|
||||
ret->sending_oob = 0;
|
||||
ret->frozen = false;
|
||||
ret->localhost_only = local_host_only;
|
||||
ret->pending_error = 0;
|
||||
ret->parent = ret->child = NULL;
|
||||
ret->oobpending = false;
|
||||
ret->outgoingeof = EOF_NO;
|
||||
ret->incomingeof = false;
|
||||
ret->listener = true;
|
||||
ret->addr = NULL;
|
||||
ret->s = -1;
|
||||
s = snew(NetSocket);
|
||||
s->sock.vt = &NetSocket_sockvt;
|
||||
s->error = NULL;
|
||||
s->plug = plug;
|
||||
bufchain_init(&s->output_data);
|
||||
s->writable = false; /* to start with */
|
||||
s->sending_oob = 0;
|
||||
s->frozen = false;
|
||||
s->localhost_only = local_host_only;
|
||||
s->pending_error = 0;
|
||||
s->parent = s->child = NULL;
|
||||
s->oobpending = false;
|
||||
s->outgoingeof = EOF_NO;
|
||||
s->incomingeof = false;
|
||||
s->listener = true;
|
||||
s->addr = NULL;
|
||||
s->s = -1;
|
||||
|
||||
/*
|
||||
* Translate address_family from platform-independent constants
|
||||
@ -850,30 +850,30 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
/*
|
||||
* Open socket.
|
||||
*/
|
||||
s = socket(address_family, SOCK_STREAM, 0);
|
||||
fd = socket(address_family, SOCK_STREAM, 0);
|
||||
|
||||
#ifndef NO_IPV6
|
||||
/* If the host doesn't support IPv6 try fallback to IPv4. */
|
||||
if (s < 0 && address_family == AF_INET6) {
|
||||
if (fd < 0 && address_family == AF_INET6) {
|
||||
address_family = AF_INET;
|
||||
s = socket(address_family, SOCK_STREAM, 0);
|
||||
fd = socket(address_family, SOCK_STREAM, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (s < 0) {
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
if (fd < 0) {
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
cloexec(s);
|
||||
cloexec(fd);
|
||||
|
||||
ret->oobinline = false;
|
||||
s->oobinline = false;
|
||||
|
||||
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(const char *)&on, sizeof(on)) < 0) {
|
||||
ret->error = strerror(errno);
|
||||
close(s);
|
||||
return &ret->sock;
|
||||
s->error = strerror(errno);
|
||||
close(fd);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
retcode = -1;
|
||||
@ -941,7 +941,7 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
}
|
||||
}
|
||||
|
||||
retcode = bind(s, &addr->sa, addrlen);
|
||||
retcode = bind(fd, &addr->sa, addrlen);
|
||||
|
||||
#ifndef NO_IPV6
|
||||
if (ai)
|
||||
@ -949,15 +949,15 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
#endif
|
||||
|
||||
if (retcode < 0) {
|
||||
close(s);
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
close(fd);
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
if (listen(s, SOMAXCONN) < 0) {
|
||||
close(s);
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
if (listen(fd, SOMAXCONN) < 0) {
|
||||
close(fd);
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
#ifndef NO_IPV6
|
||||
@ -975,25 +975,25 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
||||
|
||||
if (other) {
|
||||
if (!other->error) {
|
||||
other->parent = ret;
|
||||
ret->child = other;
|
||||
other->parent = s;
|
||||
s->child = other;
|
||||
} else {
|
||||
/* If we couldn't create a listening socket on IPv4 as well
|
||||
* as IPv6, we must return an error overall. */
|
||||
close(s);
|
||||
sfree(ret);
|
||||
close(fd);
|
||||
sfree(s);
|
||||
return &other->sock;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
ret->s = s;
|
||||
s->s = fd;
|
||||
|
||||
uxsel_tell(ret);
|
||||
add234(sktree, ret);
|
||||
uxsel_tell(s);
|
||||
add234(sktree, s);
|
||||
|
||||
return &ret->sock;
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
static void sk_net_close(Socket *sock)
|
||||
@ -1612,107 +1612,107 @@ char *get_hostname(void)
|
||||
|
||||
SockAddr *platform_get_x11_unix_address(const char *sockpath, int displaynum)
|
||||
{
|
||||
SockAddr *ret = snew(SockAddr);
|
||||
SockAddr *addr = snew(SockAddr);
|
||||
int n;
|
||||
|
||||
memset(ret, 0, sizeof *ret);
|
||||
ret->superfamily = UNIX;
|
||||
memset(addr, 0, sizeof *addr);
|
||||
addr->superfamily = UNIX;
|
||||
/*
|
||||
* In special circumstances (notably Mac OS X Leopard), we'll
|
||||
* have been passed an explicit Unix socket path.
|
||||
*/
|
||||
if (sockpath) {
|
||||
n = snprintf(ret->hostname, sizeof ret->hostname,
|
||||
n = snprintf(addr->hostname, sizeof addr->hostname,
|
||||
"%s", sockpath);
|
||||
} else {
|
||||
n = snprintf(ret->hostname, sizeof ret->hostname,
|
||||
n = snprintf(addr->hostname, sizeof addr->hostname,
|
||||
"%s%d", X11_UNIX_PATH, displaynum);
|
||||
}
|
||||
|
||||
if (n < 0)
|
||||
ret->error = "snprintf failed";
|
||||
else if (n >= sizeof ret->hostname)
|
||||
ret->error = "X11 UNIX name too long";
|
||||
addr->error = "snprintf failed";
|
||||
else if (n >= sizeof addr->hostname)
|
||||
addr->error = "X11 UNIX name too long";
|
||||
|
||||
#ifndef NO_IPV6
|
||||
ret->ais = NULL;
|
||||
addr->ais = NULL;
|
||||
#else
|
||||
ret->addresses = NULL;
|
||||
ret->naddresses = 0;
|
||||
#endif
|
||||
ret->refcount = 1;
|
||||
return ret;
|
||||
addr->refcount = 1;
|
||||
return addr;
|
||||
}
|
||||
|
||||
SockAddr *unix_sock_addr(const char *path)
|
||||
{
|
||||
SockAddr *ret = snew(SockAddr);
|
||||
SockAddr *addr = snew(SockAddr);
|
||||
int n;
|
||||
|
||||
memset(ret, 0, sizeof *ret);
|
||||
ret->superfamily = UNIX;
|
||||
n = snprintf(ret->hostname, sizeof ret->hostname, "%s", path);
|
||||
memset(addr, 0, sizeof *addr);
|
||||
addr->superfamily = UNIX;
|
||||
n = snprintf(addr->hostname, sizeof addr->hostname, "%s", path);
|
||||
|
||||
if (n < 0)
|
||||
ret->error = "snprintf failed";
|
||||
else if (n >= sizeof ret->hostname ||
|
||||
addr->error = "snprintf failed";
|
||||
else if (n >= sizeof addr->hostname ||
|
||||
n >= sizeof(((struct sockaddr_un *)0)->sun_path))
|
||||
ret->error = "socket pathname too long";
|
||||
addr->error = "socket pathname too long";
|
||||
|
||||
#ifndef NO_IPV6
|
||||
ret->ais = NULL;
|
||||
addr->ais = NULL;
|
||||
#else
|
||||
ret->addresses = NULL;
|
||||
ret->naddresses = 0;
|
||||
#endif
|
||||
ret->refcount = 1;
|
||||
return ret;
|
||||
addr->refcount = 1;
|
||||
return addr;
|
||||
}
|
||||
|
||||
Socket *new_unix_listener(SockAddr *listenaddr, Plug *plug)
|
||||
{
|
||||
int s;
|
||||
int fd;
|
||||
union sockaddr_union u;
|
||||
union sockaddr_union *addr;
|
||||
int addrlen;
|
||||
NetSocket *ret;
|
||||
NetSocket *s;
|
||||
int retcode;
|
||||
|
||||
/*
|
||||
* Create NetSocket structure.
|
||||
*/
|
||||
ret = snew(NetSocket);
|
||||
ret->sock.vt = &NetSocket_sockvt;
|
||||
ret->error = NULL;
|
||||
ret->plug = plug;
|
||||
bufchain_init(&ret->output_data);
|
||||
ret->writable = false; /* to start with */
|
||||
ret->sending_oob = 0;
|
||||
ret->frozen = false;
|
||||
ret->localhost_only = true;
|
||||
ret->pending_error = 0;
|
||||
ret->parent = ret->child = NULL;
|
||||
ret->oobpending = false;
|
||||
ret->outgoingeof = EOF_NO;
|
||||
ret->incomingeof = false;
|
||||
ret->listener = true;
|
||||
ret->addr = listenaddr;
|
||||
ret->s = -1;
|
||||
s = snew(NetSocket);
|
||||
s->sock.vt = &NetSocket_sockvt;
|
||||
s->error = NULL;
|
||||
s->plug = plug;
|
||||
bufchain_init(&s->output_data);
|
||||
s->writable = false; /* to start with */
|
||||
s->sending_oob = 0;
|
||||
s->frozen = false;
|
||||
s->localhost_only = true;
|
||||
s->pending_error = 0;
|
||||
s->parent = s->child = NULL;
|
||||
s->oobpending = false;
|
||||
s->outgoingeof = EOF_NO;
|
||||
s->incomingeof = false;
|
||||
s->listener = true;
|
||||
s->addr = listenaddr;
|
||||
s->s = -1;
|
||||
|
||||
assert(listenaddr->superfamily == UNIX);
|
||||
|
||||
/*
|
||||
* Open socket.
|
||||
*/
|
||||
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s < 0) {
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
cloexec(s);
|
||||
cloexec(fd);
|
||||
|
||||
ret->oobinline = false;
|
||||
s->oobinline = false;
|
||||
|
||||
memset(&u, '\0', sizeof(u));
|
||||
u.su.sun_family = AF_UNIX;
|
||||
@ -1728,28 +1728,28 @@ Socket *new_unix_listener(SockAddr *listenaddr, Plug *plug)
|
||||
addrlen = sizeof(u.su);
|
||||
|
||||
if (unlink(u.su.sun_path) < 0 && errno != ENOENT) {
|
||||
close(s);
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
close(fd);
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
retcode = bind(s, &addr->sa, addrlen);
|
||||
retcode = bind(fd, &addr->sa, addrlen);
|
||||
if (retcode < 0) {
|
||||
close(s);
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
close(fd);
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
if (listen(s, SOMAXCONN) < 0) {
|
||||
close(s);
|
||||
ret->error = strerror(errno);
|
||||
return &ret->sock;
|
||||
if (listen(fd, SOMAXCONN) < 0) {
|
||||
close(fd);
|
||||
s->error = strerror(errno);
|
||||
return &s->sock;
|
||||
}
|
||||
|
||||
ret->s = s;
|
||||
s->s = fd;
|
||||
|
||||
uxsel_tell(ret);
|
||||
add234(sktree, ret);
|
||||
uxsel_tell(s);
|
||||
add234(sktree, s);
|
||||
|
||||
return &ret->sock;
|
||||
return &s->sock;
|
||||
}
|
||||
|
@ -12,17 +12,17 @@ struct printer_job_tag {
|
||||
|
||||
printer_job *printer_start_job(char *printer)
|
||||
{
|
||||
printer_job *ret = snew(printer_job);
|
||||
printer_job *pj = snew(printer_job);
|
||||
/*
|
||||
* On Unix, we treat the printer string as the name of a
|
||||
* command to pipe to - typically lpr, of course.
|
||||
*/
|
||||
ret->fp = popen(printer, "w");
|
||||
if (!ret->fp) {
|
||||
sfree(ret);
|
||||
ret = NULL;
|
||||
pj->fp = popen(printer, "w");
|
||||
if (!pj->fp) {
|
||||
sfree(pj);
|
||||
pj = NULL;
|
||||
}
|
||||
return ret;
|
||||
return pj;
|
||||
}
|
||||
|
||||
void printer_job_data(printer_job *pj, const void *data, size_t len)
|
||||
|
51
unix/sftp.c
51
unix/sftp.c
@ -125,14 +125,14 @@ RFile *open_existing_file(const char *name, uint64_t *size,
|
||||
long *perms)
|
||||
{
|
||||
int fd;
|
||||
RFile *ret;
|
||||
RFile *f;
|
||||
|
||||
fd = open(name, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
ret = snew(RFile);
|
||||
ret->fd = fd;
|
||||
f = snew(RFile);
|
||||
f->fd = fd;
|
||||
|
||||
if (size || mtime || atime || perms) {
|
||||
struct stat statbuf;
|
||||
@ -154,7 +154,7 @@ RFile *open_existing_file(const char *name, uint64_t *size,
|
||||
*perms = statbuf.st_mode;
|
||||
}
|
||||
|
||||
return ret;
|
||||
return f;
|
||||
}
|
||||
|
||||
int read_from_file(RFile *f, void *buffer, int length)
|
||||
@ -176,33 +176,33 @@ struct WFile {
|
||||
WFile *open_new_file(const char *name, long perms)
|
||||
{
|
||||
int fd;
|
||||
WFile *ret;
|
||||
WFile *f;
|
||||
|
||||
fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
|
||||
(mode_t)(perms ? perms : 0666));
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
ret = snew(WFile);
|
||||
ret->fd = fd;
|
||||
ret->name = dupstr(name);
|
||||
f = snew(WFile);
|
||||
f->fd = fd;
|
||||
f->name = dupstr(name);
|
||||
|
||||
return ret;
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
WFile *open_existing_wfile(const char *name, uint64_t *size)
|
||||
{
|
||||
int fd;
|
||||
WFile *ret;
|
||||
WFile *f;
|
||||
|
||||
fd = open(name, O_APPEND | O_WRONLY);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
ret = snew(WFile);
|
||||
ret->fd = fd;
|
||||
ret->name = dupstr(name);
|
||||
f = snew(WFile);
|
||||
f->fd = fd;
|
||||
f->name = dupstr(name);
|
||||
|
||||
if (size) {
|
||||
struct stat statbuf;
|
||||
@ -214,7 +214,7 @@ WFile *open_existing_wfile(const char *name, uint64_t *size)
|
||||
*size = statbuf.st_size;
|
||||
}
|
||||
|
||||
return ret;
|
||||
return f;
|
||||
}
|
||||
|
||||
int write_to_file(WFile *f, void *buffer, int length)
|
||||
@ -311,18 +311,15 @@ struct DirHandle {
|
||||
|
||||
DirHandle *open_directory(const char *name, const char **errmsg)
|
||||
{
|
||||
DIR *dir;
|
||||
DirHandle *ret;
|
||||
|
||||
dir = opendir(name);
|
||||
if (!dir) {
|
||||
DIR *dp = opendir(name);
|
||||
if (!dp) {
|
||||
*errmsg = strerror(errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = snew(DirHandle);
|
||||
ret->dir = dir;
|
||||
return ret;
|
||||
DirHandle *dir = snew(DirHandle);
|
||||
dir->dir = dp;
|
||||
return dir;
|
||||
}
|
||||
|
||||
char *read_filename(DirHandle *dir)
|
||||
@ -388,16 +385,16 @@ struct WildcardMatcher {
|
||||
int i;
|
||||
};
|
||||
WildcardMatcher *begin_wildcard_matching(const char *name) {
|
||||
WildcardMatcher *ret = snew(WildcardMatcher);
|
||||
WildcardMatcher *dir = snew(WildcardMatcher);
|
||||
|
||||
if (glob(name, 0, NULL, &ret->globbed) < 0) {
|
||||
sfree(ret);
|
||||
if (glob(name, 0, NULL, &dir->globbed) < 0) {
|
||||
sfree(dir);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret->i = 0;
|
||||
dir->i = 0;
|
||||
|
||||
return ret;
|
||||
return dir;
|
||||
}
|
||||
char *wildcard_get_filename(WildcardMatcher *dir) {
|
||||
if (dir->i < dir->globbed.gl_pathc) {
|
||||
|
@ -9,9 +9,9 @@
|
||||
|
||||
Filename *filename_from_str(const char *str)
|
||||
{
|
||||
Filename *ret = snew(Filename);
|
||||
ret->path = dupstr(str);
|
||||
return ret;
|
||||
Filename *fn = snew(Filename);
|
||||
fn->path = dupstr(str);
|
||||
return fn;
|
||||
}
|
||||
|
||||
Filename *filename_copy(const Filename *fn)
|
||||
|
Reference in New Issue
Block a user