1 /* ========================================================================
2  * Copyright 1988-2006 University of Washington
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *
11  * ========================================================================
12  */
13 
14 /*
15  * Program:	UNIX/VMS newline routines
16  *
17  * Author:	Mark Crispin
18  *		Networks and Distributed Computing
19  *		Computing & Communications
20  *		University of Washington
21  *		Administration Building, AG-44
22  *		Seattle, WA  98195
23  *		Internet: MRC@CAC.Washington.EDU
24  *
25  * Date:	1 August 1988
26  * Last Edited:	30 August 2006
27  */
28 
29 /* Copy string with CRLF newlines
30  * Accepts: destination string
31  *	    pointer to size of destination string buffer
32  *	    source string
33  *	    length of source string
34  * Returns: length of copied string
35  */
36 
strcrlfcpy(unsigned char ** dst,unsigned long * dstl,unsigned char * src,unsigned long srcl)37 unsigned long strcrlfcpy (unsigned char **dst,unsigned long *dstl,
38 			  unsigned char *src,unsigned long srcl)
39 {
40   long i = srcl * 2,j;
41   unsigned char c,*d = src;
42   if (*dst) {			/* candidate destination provided? */
43 				/* count NLs if doesn't fit worst-case */
44     if (i > *dstl) for (i = j = srcl; j; --j) if (*d++ == '\012') i++;
45 				/* still too small, must reset destination */
46     if (i > *dstl) fs_give ((void **) dst);
47   }
48 				/* make a new buffer if needed */
49   if (!*dst) *dst = (char *) fs_get ((*dstl = i) + 1);
50   d = *dst;			/* destination string */
51   if (srcl) do {		/* main copy loop */
52     if ((c = *src++) < '\016') {
53 				/* prepend CR to LF */
54       if (c == '\012') *d++ = '\015';
55 				/* unlikely CR */
56       else if ((c == '\015') && (srcl > 1) && (*src == '\012')) {
57 	*d++ = c;		/* copy the CR */
58 	c = *src++;		/* grab the LF */
59 	--srcl;			/* adjust the count */
60       }
61     }
62     *d++ = c;			/* copy character */
63   } while (--srcl);
64   *d = '\0';			/* tie off destination */
65   return d - *dst;		/* return length */
66 }
67 
68 /* Length of string after strcrlfcpy applied
69  * Accepts: source string
70  * Returns: length of string
71  */
72 
strcrlflen(STRING * s)73 unsigned long strcrlflen (STRING *s)
74 {
75   unsigned long pos = GETPOS (s);
76   unsigned long i = SIZE (s);
77   unsigned long j = i;
78   while (j--) switch (SNX (s)) {/* search for newlines */
79   case '\015':			/* unlikely carriage return */
80     if (j && (CHR (s) == '\012')) {
81       SNX (s);			/* eat the line feed */
82       j--;
83     }
84     break;
85   case '\012':			/* line feed? */
86     i++;
87   default:			/* ordinary chararacter */
88     break;
89   }
90   SETPOS (s,pos);		/* restore old position */
91   return i;
92 }
93