Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / fileio.c
index 44f509099a160ab2dc6dc0eae831bd5f60304338..410110d3bbe2c6a0273a5e89bbec7cd7827500a7 100644 (file)
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 #include "fileio.h"
-#include "runmalloc.h"         /* RunMalloc, RunFree */
-#include <stdio.h>             /* BUFSIZ, EOF */
-#include <fcntl.h>             /* O_RDONLY, O_WRONLY, ... */
-#include <unistd.h>            /* read, write, open, close */
-#include <assert.h>            /* assert */
+#include "ircd_alloc.h"         /* MyMalloc, MyFree */
+
+#include <assert.h>             /* assert */
+#include <fcntl.h>              /* O_RDONLY, O_WRONLY, ... */
+#include <stdio.h>              /* BUFSIZ, EOF */
+#include <sys/stat.h>           /* struct stat */
+#include <unistd.h>             /* read, write, open, close */
 
 #define FB_EOF  0x01
 #define FB_FAIL 0x02
 
 struct FileBuf {
-  int fd;                      /* file descriptor */
-  char *endp;                  /* one past the end */
-  char *ptr;                   /* current read pos */
-  int flags;                   /* file state */
-  char buf[BUFSIZ];            /* buffer */
+  int fd;                       /* file descriptor */
+  char *endp;                   /* one past the end */
+  char *ptr;                    /* current read pos */
+  int flags;                    /* file state */
+  char buf[BUFSIZ];             /* buffer */
 };
 
-FBFILE *fbopen(const char *filename, const char *mode)
+FBFILEfbopen(const char *filename, const char *mode)
 {
   int openmode = 0;
   int pmode = 0;
@@ -45,27 +47,25 @@ FBFILE *fbopen(const char *filename, const char *mode)
   assert(filename);
   assert(mode);
 
-  while (*mode)
-  {
-    switch (*mode)
-    {
-      case 'r':
-       openmode = O_RDONLY;
-       break;
-      case 'w':
-       openmode = O_WRONLY | O_CREAT | O_TRUNC;
-       pmode = S_IREAD | S_IWRITE;
-       break;
-      case 'a':
-       openmode = O_WRONLY | O_CREAT | O_APPEND;
-       pmode = S_IREAD | S_IWRITE;
-       break;
-      case '+':
-       openmode &= ~(O_RDONLY | O_WRONLY);
-       openmode |= O_RDWR;
-       break;
-      default:
-       break;
+  while (*mode) {
+    switch (*mode) {
+    case 'r':
+      openmode = O_RDONLY;
+      break;
+    case 'w':
+      openmode = O_WRONLY | O_CREAT | O_TRUNC;
+      pmode = S_IREAD | S_IWRITE;
+      break;
+    case 'a':
+      openmode = O_WRONLY | O_CREAT | O_APPEND;
+      pmode = S_IREAD | S_IWRITE;
+      break;
+    case '+':
+      openmode &= ~(O_RDONLY | O_WRONLY);
+      openmode |= O_RDWR;
+      break;
+    default:
+      break;
     }
     ++mode;
   }
@@ -74,8 +74,7 @@ FBFILE *fbopen(const char *filename, const char *mode)
    * 3 seconds. -avalon (curtesy of wumpus)
    */
   alarm(3);
-  if ((fd = open(filename, openmode, pmode)) == -1)
-  {
+  if ((fd = open(filename, openmode, pmode)) == -1) {
     alarm(0);
     return fb;
   }
@@ -86,27 +85,26 @@ FBFILE *fbopen(const char *filename, const char *mode)
   return fb;
 }
 
-FBFILE *fdbopen(int fd, const char *mode)
+FBFILEfdbopen(int fd, const char *mode)
 {
   /*
    * ignore mode, if file descriptor hasn't been opened with the
    * correct mode, the first use will fail
    */
-  FBFILE *fb = (FBFILE *) RunMalloc(sizeof(FBFILE));
-  if (NULL != fb)
-  {
-    fb->ptr = fb->endp = fb->buf;
-    fb->fd = fd;
-    fb->flags = 0;
-  }
+  FBFILE *fb = (FBFILE *) MyMalloc(sizeof(FBFILE));
+  assert(0 != fb);
+  fb->ptr   = fb->endp = fb->buf;
+  fb->fd    = fd;
+  fb->flags = 0;
+
   return fb;
 }
 
-void fbclose(FBFILE * fb)
+void fbclose(FBFILE* fb)
 {
   assert(fb);
   close(fb->fd);
-  RunFree(fb);
+  MyFree(fb);
 }
 
 static int fbfill(FBFILE * fb)
@@ -146,8 +144,7 @@ char *fbgets(char *buf, size_t len, FBFILE * fb)
   if (fb->ptr == fb->endp && fbfill(fb) < 1)
     return 0;
   --len;
-  while (len--)
-  {
+  while (len--) {
     *p = *fb->ptr++;
     if ('\n' == *p)
     {
@@ -157,12 +154,10 @@ char *fbgets(char *buf, size_t len, FBFILE * fb)
     /*
      * deal with CR's
      */
-    else if ('\r' == *p)
-    {
-      if (fb->ptr < fb->endp || fbfill(fb) > 0)
-      {
-       if ('\n' == *fb->ptr)
-         ++fb->ptr;
+    else if ('\r' == *p) {
+      if (fb->ptr < fb->endp || fbfill(fb) > 0) {
+        if ('\n' == *fb->ptr)
+          ++fb->ptr;
       }
       *p++ = '\n';
       break;
@@ -181,8 +176,7 @@ int fbputs(const char *str, FBFILE * fb)
   assert(str);
   assert(fb);
 
-  if (0 == fb->flags)
-  {
+  if (0 == fb->flags) {
     n = write(fb->fd, str, strlen(str));
     if (-1 == n)
       fb->flags |= FB_FAIL;
@@ -196,3 +190,4 @@ int fbstat(struct stat *sb, FBFILE * fb)
   assert(fb);
   return fstat(fb->fd, sb);
 }
+