FDIO: don't set fd's to 0, -1 is better since its safe for abuse.
[citadel.git] / libcitadel / lib / stringbuf.c
index 103377bba0fef110b7e1da6915820e5f6907cfd8..1b9ea072366221b9bea1f8cf1f438a229f795bb3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1987-2011 by the citadel.org team
+ * Copyright (c) 1987-2013 by the citadel.org team
  *
  * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -1173,29 +1173,31 @@ void StrBufSpaceToBlank(StrBuf *Buf)
 
 void StrBufStripAllBut(StrBuf *Buf, char leftboundary, char rightboundary)
 {
-       const char *pBuff;
        const char *pLeft;
        const char *pRight;
 
-       if ((Buf == NULL) || (Buf->buf == NULL))
+       if ((Buf == NULL) || (Buf->buf == NULL)) {
+               StrBufCutAt(Buf, 0, Buf->buf);
                return;
-       pLeft = pBuff = Buf->buf;
-       while (pBuff != NULL) {
-               pLeft = pBuff;
-               pBuff = strchr(pBuff, leftboundary);
-               if (pBuff != NULL)
-                       pBuff++;
        }
-               
-       if (pLeft != NULL)
-               pBuff = pLeft;
-       else
-               pBuff = Buf->buf;
-       pRight = strchr(pBuff, rightboundary);
-       if (pRight != NULL)
+
+       pRight = strchr(Buf->buf, rightboundary);
+       if (pRight != NULL) {
                StrBufCutAt(Buf, 0, pRight);
-       if (pLeft != NULL)
-               StrBufCutLeft(Buf, pLeft - Buf->buf);
+       }
+       else {
+               StrBufCutAt(Buf, 0, Buf->buf);
+               return;
+       }
+
+       pLeft = strrchr(ChrPtr(Buf), leftboundary);
+       if (pLeft != NULL) {
+               StrBufCutLeft(Buf, pLeft - Buf->buf + 1);
+       }
+       else {
+               StrBufCutAt(Buf, 0, Buf->buf);
+               return;
+       }
 }
 
 
@@ -3932,9 +3934,17 @@ long IOBufferStrLength(IOBuffer *FB)
        return StrLength(FB->Buf) - (FB->ReadWritePointer - FB->Buf->buf);
 }
 
-void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
+inline static void FDIOBufferFlush(FDIOBuffer *FDB)
 {
        memset(FDB, 0, sizeof(FDIOBuffer));
+       FDB->OtherFD = -1;
+       FDB->SplicePipe[0] = -1;
+       FDB->SplicePipe[1] = -1;
+}
+
+void FDIOBufferInit(FDIOBuffer *FDB, IOBuffer *IO, int FD, long TotalSendSize)
+{
+       FDIOBufferFlush(FDB);
        FDB->ChunkSize = 
                FDB->TotalSendSize = TotalSendSize;
        FDB->IOB = IO;
@@ -3953,15 +3963,18 @@ void FDIOBufferDelete(FDIOBuffer *FDB)
 #ifdef LINUX_SPLICE
        if (EnableSplice)
        {
-               close(FDB->SplicePipe[0]);
-               close(FDB->SplicePipe[1]);
+               if (FDB->SplicePipe[0] > 0)
+                       close(FDB->SplicePipe[0]);
+               if (FDB->SplicePipe[1] > 0)
+                       close(FDB->SplicePipe[1]);
        }
        else
 #endif
                FreeStrBuf(&FDB->ChunkBuffer);
        
-       close(FDB->OtherFD);
-       memset(FDB, 0, sizeof(FDIOBuffer));     
+       if (FDB->OtherFD > 0)
+               close(FDB->OtherFD);
+       FDIOBufferFlush(FDB);
 }
 
 int FileSendChunked(FDIOBuffer *FDB, const char **Err)