Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

sendline.c

Go to the documentation of this file.
00001 /************************************************************************
00002 * File: sendn.c
00003 * Purpose: Provides a C file for the sendn function from
00004 * the book Advanced Programming in the UNIX Environment by Richard Stevens.
00005 * The sendn function calls the send function repeatedly until n bytes
00006 * have been written to the file descriptor.
00007 *************************************************************************/
00008 
00009 /* This is neccessary to avoid muliple definitions of fd_set, etc when both
00010 * RPC via PCNFS and Windows Sockets are to be available. */
00011 #ifdef USE_PCNFS
00012 #undef USE_PCNFS
00013 #endif
00014 
00015 #include "rcs_defs.hh"          /* _Windows */
00016 
00017 #include "sendline.h"           /* forward prototype, PRINT_SOCKET_WRITE_SIZE */
00018 #include "sendn.h"              /* sendn() */
00019 #include "rcs_prnt.hh"          /* PRINT_SOCKET_WRITE_SIZE, rcs_print_  */
00020 
00021 #include <stdlib.h>             /* realloc() */
00022 #include <string.h>             /* strcpy(), strcat() */
00023 
00024 #include "dbg_mem.h"            /* DEBUG_FREE,DEBUG_MALLOC,DEBUG_CALLOC */
00025 
00026 
00027 #if defined(MSDOS) && !defined(_Windows)
00028 static char sendline_buffer[0x2000];
00029 static long sendline_buffer_size = 0x2000;
00030 #else
00031 static char *sendline_buffer = NULL;
00032 static long sendline_buffer_size = 0;
00033 #endif
00034 /* Write "n" bytes to a descriptor. */
00035 int
00036 sendline (int fd, const char *cptr, int _flags, double _timeout)
00037 {
00038   int length;
00039   length = strlen (cptr);
00040 #if defined(MSDOS) && !defined(_Windows)
00041   if (length + 2 > sendline_buffer_size)
00042     {
00043       rcs_print_error ("Since DOS doesn't seem to handle realloc properly\n");
00044       rcs_print_error
00045         ("sending strings over %d with sendline are prohibited.",
00046          sendline_buffer_size);
00047       return (-1);
00048     }
00049 #else
00050   sendline_buffer_size = length + 18 - (length % 16);
00051   sendline_buffer = realloc (sendline_buffer, sendline_buffer_size);
00052   if (NULL == sendline_buffer)
00053     {
00054       rcs_print_error
00055         ("sendline could not allocate %d bytes for temperary buffer.\n",
00056          sendline_buffer_size);
00057       rcs_print_error ("String to send was: %s", cptr);
00058       return (-1);
00059     }
00060 #endif
00061   strcpy (sendline_buffer, cptr);
00062   strcat (sendline_buffer, "\n");
00063   rcs_print_debug (PRINT_SOCKET_WRITE_SIZE, "sendline %s to %d.\n", cptr, fd);
00064   return sendn (fd, sendline_buffer, length + 1, _flags, _timeout);
00065 }

Generated on Sun Dec 2 15:56:52 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001