1
/* ========================================================================
2
* Copyright 1988-2006 University of Washington
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
8
* http://www.apache.org/licenses/LICENSE-2.0
11
* ========================================================================
15
* Program: UNIX/VMS newline routines
17
* Author: Mark Crispin
18
* Networks and Distributed Computing
19
* Computing & Communications
20
* University of Washington
21
* Administration Building, AG-44
23
* Internet: MRC@CAC.Washington.EDU
26
* Last Edited: 30 August 2006
29
/* Copy string with CRLF newlines
30
* Accepts: destination string
31
* pointer to size of destination string buffer
33
* length of source string
34
* Returns: length of copied string
37
unsigned long strcrlfcpy (unsigned char **dst,unsigned long *dstl,
38
unsigned char *src,unsigned long srcl)
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);
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';
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 */
62
*d++ = c; /* copy character */
64
*d = '\0'; /* tie off destination */
65
return d - *dst; /* return length */
68
/* Length of string after strcrlfcpy applied
69
* Accepts: source string
70
* Returns: length of string
73
unsigned long strcrlflen (STRING *s)
75
unsigned long pos = GETPOS (s);
76
unsigned long i = SIZE (s);
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 */
85
case '\012': /* line feed? */
87
default: /* ordinary chararacter */
90
SETPOS (s,pos); /* restore old position */