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:	Mac 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:	26 January 1992
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,j;
41   unsigned char c,*d = src;
42   if (*dst) {			/* destination provided? */
43     if ((i = srcl * 2) > *dstl)	/* calculate worst-case situation */
44       for (i = j = srcl; j; --j) if (*d++ == '\015') i++;
45 				/* flush destination buffer if too small */
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 {		/* copy string */
52     c = *d++ = *src++;		/* copy character */
53 				/* append line feed to bare CR */
54     if ((c == '\015') && (*src != '\012')) *d++ = '\012';
55   } while (--srcl);
56   *d = '\0';			/* tie off destination */
57   return d - *dst;		/* return length */
58 }
59 
60 
61 /* Length of string after strcrlfcpy applied
62  * Accepts: source string
63  * Returns: length of string
64  */
65 
strcrlflen(STRING * s)66 unsigned long strcrlflen (STRING *s)
67 {
68   unsigned long pos = GETPOS (s);
69   unsigned long i = SIZE (s);
70   unsigned long j = i;
71   while (j--) if ((SNX (s) == '\015') && ((CHR (s) != '\012') || !j)) i++;
72   SETPOS (s,pos);		/* restore old position */
73   return i;
74 }
75