[or-cvs] r17454: {tor} Add two lseek wrappers to compat.[ch]: one to return current (tor/trunk/src/common)

nickm at seul.org nickm at seul.org
Tue Dec 2 23:26:04 UTC 2008


Author: nickm
Date: 2008-12-02 18:26:04 -0500 (Tue, 02 Dec 2008)
New Revision: 17454

Modified:
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
Log:
Add two lseek wrappers to compat.[ch]: one to return current fd position, and one to move the fd to the end of the file.

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2008-12-02 23:13:40 UTC (rev 17453)
+++ tor/trunk/src/common/compat.c	2008-12-02 23:26:04 UTC (rev 17454)
@@ -581,6 +581,37 @@
   tor_free(lockfile);
 }
 
+/* Some old versions of unix didn't define constants for these values,
+ * and instead expect you to say 0, 1, or 2. */
+#ifndef SEEK_CUR
+#define SEEK_CUR 1
+#endif
+#ifndef SEEK_END
+#define SEEK_END 2
+#endif
+
+/** Return the position of fd with respect to the start of the file */
+off_t
+tor_fd_getpos(int fd)
+{
+#ifdef WIN32
+  return (off_t) _lseek(fd, 0, SEEK_CUR);
+#else
+  return (off_t) lseek(fd, 0, SEEK_CUR);
+#endif
+}
+
+/** Move fd to the end of the file. Return -1 on error, 0 on success. */
+int
+tor_fd_seekend(int fd)
+{
+#ifdef WIN32
+  return _lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
+#else
+  return lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
+#endif
+}
+
 #undef DEBUG_SOCKET_COUNTING
 #ifdef DEBUG_SOCKET_COUNTING
 /** A bitarray of all fds that should be passed to tor_socket_close(). Only

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2008-12-02 23:13:40 UTC (rev 17453)
+++ tor/trunk/src/common/compat.h	2008-12-02 23:26:04 UTC (rev 17454)
@@ -294,6 +294,9 @@
                                   int *locked_out);
 void tor_lockfile_unlock(tor_lockfile_t *lockfile);
 
+off_t tor_fd_getpos(int fd);
+int tor_fd_seekend(int fd);
+
 #ifdef MS_WINDOWS
 #define PATH_SEPARATOR "\\"
 #else



More information about the tor-commits mailing list