]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - fd.c
Change year of copyright from 2008 to 2009.
[paraslash.git] / fd.c
diff --git a/fd.c b/fd.c
index 48e2faf97c6d3e2bcb1616ebef04db48e2003d9e..6042a58f531af44e941f012fdc273d78c2f9fb37 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -15,7 +15,7 @@
 #include "para.h"
 #include "error.h"
 
 #include "para.h"
 #include "error.h"
 
-/*
+/**
  * Write a buffer to a file descriptor, re-write on short writes.
  *
  * \param fd The file descriptor.
  * Write a buffer to a file descriptor, re-write on short writes.
  *
  * \param fd The file descriptor.
@@ -154,16 +154,16 @@ void para_fd_set(int fd, fd_set *fds, int *max_fileno)
 }
 
 /**
 }
 
 /**
-* Paraslash's wrapper for fgets(3).
-
-* \param line Pointer to the buffer to store the line.
-* \param size The size of the buffer given by \a line.
-* \param f The stream to read from.
-*
-* \return Unlike the standard fgets() function, an integer value
-* is returned. On success, this function returns 1. On errors, -E_FGETS
-* is returned. A zero return value indicates an end of file condition.
-*/
+ * Paraslash's wrapper for fgets(3).
+ *
+ * \param line Pointer to the buffer to store the line.
+ * \param size The size of the buffer given by \a line.
+ * \param f The stream to read from.
+ *
+ * \return Unlike the standard fgets() function, an integer value
+ * is returned. On success, this function returns 1. On errors, -E_FGETS
+ * is returned. A zero return value indicates an end of file condition.
+ */
 __must_check int para_fgets(char *line, int size, FILE *f)
 {
 again:
 __must_check int para_fgets(char *line, int size, FILE *f)
 {
 again:
@@ -285,9 +285,10 @@ int para_opendir(const char *dirname, DIR **dir, int *cwd)
        if (*dir)
                return 1;
        ret = -ERRNO_TO_PARA_ERROR(errno);
        if (*dir)
                return 1;
        ret = -ERRNO_TO_PARA_ERROR(errno);
-/* Ignore return value of fchdir() and close(). We're busted anyway. */
-       if (cwd)
-               fchdir(*cwd);
+       /* Ignore return value of fchdir() and close(). We're busted anyway. */
+       if (cwd) {
+               int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
+       }
 close_cwd:
        if (cwd)
                close(*cwd);
 close_cwd:
        if (cwd)
                close(*cwd);
@@ -393,17 +394,19 @@ out:
  */
 int para_munmap(void *start, size_t length)
 {
  */
 int para_munmap(void *start, size_t length)
 {
+       int err;
        if (munmap(start, length) >= 0)
                return 1;
        if (munmap(start, length) >= 0)
                return 1;
+       err = errno;
        PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
        PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
-               strerror(errno));
-       return -E_MUNMAP;
+               strerror(err));
+       return -ERRNO_TO_PARA_ERROR(err);
 }
 
 /**
 }
 
 /**
- * check a file descriptor for writability
+ * Check a file descriptor for writability.
  *
  *
- * \param fd the file descriptor
+ * \param fd The file descriptor.
  *
  * \return positive if fd is ready for writing, zero if it isn't, negative if
  * an error occurred.
  *
  * \return positive if fd is ready for writing, zero if it isn't, negative if
  * an error occurred.
@@ -424,3 +427,24 @@ again:
                goto again;
        return ret;
 }
                goto again;
        return ret;
 }
+
+/**
+ * Ensure that file descriptors 0, 1, and 2 are valid.
+ *
+ * Common approach that opens /dev/null until it gets a file descriptor greater
+ * than two.
+ *
+ * \sa okir's Black Hats Manual.
+ */
+void valid_fd_012(void)
+{
+       while (1) {
+               int fd = open("/dev/null", O_RDWR);
+               if (fd < 0)
+                       exit(EXIT_FAILURE);
+               if (fd > 2) {
+                       close(fd);
+                       break;
+               }
+       }
+}