imapext-2007

annotate src/osdep/unix/nl_unix.c @ 0:ada5e610ab86

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

UW-IMAP'd extensions by yuuji