aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c26
-rw-r--r--lib/lib.h10
-rw-r--r--lib/net.c19
3 files changed, 51 insertions, 4 deletions
diff --git a/lib/lib.c b/lib/lib.c
index b074a92b..9d5c5f4b 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -248,7 +248,7 @@ struct string_list *find_in_path(char *path, char *filename)
cwd = xgetcwd();
for (;;) {
- char *next = strchr(path, ':');
+ char *res, *next = strchr(path, ':');
int len = next ? next-path : strlen(path);
struct string_list *rnext;
struct stat st;
@@ -257,9 +257,7 @@ struct string_list *find_in_path(char *path, char *filename)
+ (len ? len : strlen(cwd)) + 2);
if (!len) sprintf(rnext->str, "%s/%s", cwd, filename);
else {
- char *res = rnext->str;
-
- memcpy(res, path, len);
+ memcpy(res = rnext->str, path, len);
res += len;
*(res++) = '/';
strcpy(res, filename);
@@ -553,6 +551,26 @@ void msleep(long miliseconds)
nanosleep(&ts, &ts);
}
+// Adjust timespec by nanosecond offset
+void nanomove(struct timespec *ts, long long offset)
+{
+ long long nano = ts->tv_nsec + offset, secs = nano/1000000000;
+
+ ts->tv_sec += secs;
+ nano %= 1000000000;
+ if (nano<0) {
+ ts->tv_sec--;
+ nano += 1000000000;
+ }
+ ts->tv_nsec = nano;
+}
+
+// return difference between two timespecs in nanosecs
+long long nanodiff(struct timespec *old, struct timespec *new)
+{
+ return (new->tv_sec - old->tv_sec)*1000000000LL+(new->tv_nsec - old->tv_nsec);
+}
+
// return 1<<x of highest bit set
int highest_bit(unsigned long l)
{
diff --git a/lib/lib.h b/lib/lib.h
index ed31ffa9..b36d7a75 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -205,6 +205,8 @@ struct string_list **splitpath(char *path, struct string_list **list);
char *readfileat(int dirfd, char *name, char *buf, off_t *len);
char *readfile(char *name, char *buf, off_t len);
void msleep(long miliseconds);
+void nanomove(struct timespec *ts, long long offset);
+long long nanodiff(struct timespec *old, struct timespec *new);
int highest_bit(unsigned long l);
int64_t peek_le(void *ptr, unsigned size);
int64_t peek_be(void *ptr, unsigned size);
@@ -301,6 +303,13 @@ void tty_sigreset(int i);
void start_redraw(unsigned *width, unsigned *height);
// net.c
+
+union socksaddr {
+ struct sockaddr s;
+ struct sockaddr_in in;
+ struct sockaddr_in6 in6;
+};
+
int xsocket(int domain, int type, int protocol);
void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len);
struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype,
@@ -311,6 +320,7 @@ int xpoll(struct pollfd *fds, int nfds, int timeout);
int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout);
char *ntop(struct sockaddr *sa);
void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest);
+int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout);
// password.c
int get_salt(char *salt, char * algo);
diff --git a/lib/net.c b/lib/net.c
index 346d17e9..d4373fbe 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -138,3 +138,22 @@ void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest)
if (rc != len) perror_exit("sendto");
}
+
+// xrecvfrom with timeout in milliseconds
+int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout)
+{
+ socklen_t sl = sizeof(*sa);
+
+ if (timeout >= 0) {
+ struct pollfd pfd;
+
+ pfd.fd = fd;
+ pfd.events = POLLIN;
+ if (!xpoll(&pfd, 1, timeout)) return 0;
+ }
+
+ len = recvfrom(fd, buf, len, 0, (void *)sa, &sl);
+ if (len<0) perror_exit("recvfrom");
+
+ return len;
+}