File: misc.c

package info (click to toggle)
pine 3.96M-2
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 10,740 kB
  • ctags: 15,029
  • sloc: ansic: 155,190; makefile: 1,083; sh: 451; csh: 62; perl: 30
file content (270 lines) | stat: -rw-r--r-- 8,175 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Program:	Miscellaneous utility routines
 *
 * Author:	Mark Crispin
 *		Networks and Distributed Computing
 *		Computing & Communications
 *		University of Washington
 *		Administration Building, AG-44
 *		Seattle, WA  98195
 *		Internet: MRC@CAC.Washington.EDU
 *
 * Date:	5 July 1988
 * Last Edited:	19 April 1995
 *
 * Sponsorship:	The original version of this work was developed in the
 *		Symbolic Systems Resources Group of the Knowledge Systems
 *		Laboratory at Stanford University in 1987-88, and was funded
 *		by the Biomedical Research Technology Program of the National
 *		Institutes of Health under grant number RR-00785.
 *
 * Original version Copyright 1988 by The Leland Stanford Junior University
 * Copyright 1995 by the University of Washington
 *
 *  Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notices appear in all copies and that both the
 * above copyright notices and this permission notice appear in supporting
 * documentation, and that the name of the University of Washington or The
 * Leland Stanford Junior University not be used in advertising or publicity
 * pertaining to distribution of the software without specific, written prior
 * permission.  This software is made available "as is", and
 * THE UNIVERSITY OF WASHINGTON AND THE LELAND STANFORD JUNIOR UNIVERSITY
 * DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE,
 * INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF
 * WASHINGTON OR THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */


#include <ctype.h>
#include "mail.h"
#include "osdep.h"
#include "misc.h"

/* Convert string to all uppercase
 * Accepts: string pointer
 * Returns: string pointer
 */

char *ucase (s)
	char *s;
{
  char c;
  char *ret = s;
				/* if lowercase covert to upper */
  for (; c = *s; s++) if (islower (c)) *s -= 'a'-'A';
  return (ret);			/* return string */
}


/* Convert string to all lowercase
 * Accepts: string pointer
 * Returns: string pointer
 */

char *lcase (s)
	char *s;
{
  char c;
  char *ret = s;
				/* if uppercase covert to lower */
  for (; c = *s; s++) if (isupper (c)) *s += 'a'-'A';
  return (ret);			/* return string */
}


/* Copy string to free storage
 * Accepts: source string
 * Returns: free storage copy of string
 */

char *cpystr (string)
	char *string;
{
  if (string) {			/* make sure argument specified */
    char *dst = (char *) fs_get (1+strlen (string));
    strcpy (dst,string);
    return (dst);
  }
  else return NIL;
}

/* Returns index of rightmost bit in word
 * Accepts: pointer to a 32 bit value
 * Returns: -1 if word is 0, else index of rightmost bit
 *
 * Bit is cleared in the word
 */

unsigned long find_rightmost_bit (valptr)
	unsigned long *valptr;
{
  register long value= *valptr;
  register long clearbit;	/* bit to clear */
  register bitno;		/* bit number to return */
  if (value == 0) return (-1);	/* no bits are set */
  if (value & 0xFFFF) {		/* low order halfword has a bit? */
    bitno = 0;			/* yes, start with bit 0 */
    clearbit = 1;		/* which has value 1 */
  } else {			/* high order halfword has the bit */
    bitno = 16;			/* start with bit 16 */
    clearbit = 0x10000;		/* which has value 10000h */
    value >>= 16;		/* and slide the halfword down */
  }
  if (!(value & 0xFF)) {	/* low quarterword has a bit? */
    bitno += 8;			/* no, start 8 bits higher */
    clearbit <<= 8;		/* bit to clear is 2^8 higher */
    value >>= 8;		/* and slide the quarterword down */
  }
  while (T) {			/* search for bit in quarterword */
    if (value & 1) break;	/* found it? */
    value >>= 1;		/* now, slide the bit down */
    bitno += 1;			/* count one more bit */
    clearbit <<= 1;		/* bit to clear is 1 bit higher */
  }
  *valptr ^= clearbit;		/* clear the bit in the argument */
  return (bitno);		/* and return the bit number */
}

/* Return minimum of two integers
 * Accepts: integer 1
 *	    integer 2
 * Returns: minimum
 */

long min (i,j)
	long i;
	long j;
{
  return ((i < j) ? i : j);
}


/* Return maximum of two integers
 * Accepts: integer 1
 *	    integer 2
 * Returns: maximum
 */

long max (i,j)
	long i;
	long j;
{
  return ((i > j) ? i : j);
}

/* Case independent search (machines)
	fast on 32-bit machines;
 * Accepts: base string
 *	    length of base string
 *	    pattern string
 *	    length of pattern string
 * Returns: T if pattern exists inside base, else NIL
 */

#define Word unsigned long

long search (s,c,pat,patc)
	char *s;
	long c;
	char *pat;
	long patc;
{
  register Word m;
  long cc;
  union {
    unsigned long wd;
    char ch[9];
  } wdtest;
  strcpy (wdtest.ch,"AAAA1234");/* constant for word testing */
				/* validate arguments, c becomes # of tries */
  if (!(s && c > 0 && pat && patc > 0 && (c -= (patc - 1)) > 0)) return NIL;
				/* do slow search if long is not 4 chars */
  if (wdtest.wd != 0x41414141) return ssrc (&s,&c,pat,(long) T);
  /*
   * Fast search algorithm XORs the mask with each word from the base string
   * and complements the result. This will give bytes of all ones where there
   * are matches.  We then mask out the high order and case bits in each byte
   * and add 21 (case + overflow) to all the bytes.  If we have a resulting
   * carry, then we have a match.
   */
  if (cc = ((int) s & 3)) {	/* any chars before word boundary? */
    c -= (cc = 4 - cc);		/* yes, calculate how many, account for them */
				/* search through those */
    if (ssrc (&s,&cc,pat,(long) NIL)) return T;
  }
  m = *pat * 0x01010101;	/* search mask */
  do {				/* interesting word? */
    if (0x80808080&(0x21212121+(0x5F5F5F5F&~(m^*(Word *) s)))) {
				/* yes, commence a slow search through it */
      if (ssrc (&s,&c,pat,(long) NIL)) return T;
    }
    else s += 4,c -= 4;		/* try next word */
  } while (c > 0);		/* continue until end of string */
  return NIL;			/* string not found */
}

/* Case independent slow search within a word
 * Accepts: base string
 *	    number of tries left
 *	    pattern string
 *	    multi-word hunt flag
 * Returns: T if pattern exists inside base, else NIL
 */

long ssrc (base,tries,pat,multiword)
	char **base;
	long *tries;
	char *pat;
	long multiword;
{
  register char *s = *base;
  register long c = multiword ? *tries : min (*tries,(long) 4);
  register char *p = pat;
				/* search character at a time */
  if (c > 0) do if (!((*p ^ *s++) & (char) 0xDF)) {
    char *ss = s;		/* remember were we began */
    do if (!*++p) return T;	/* match case-independent until end */
    while (!((*p ^ *s++) & (char) 0xDF));
    s = ss;			/* try next character */
    p = pat;			/* start at beginning of pattern */
  } while (--c);		/* continue if multiword or not at boundary */
  *tries -= s - *base;		/* update try count */
  *base = s;			/* update base */
  return NIL;			/* string not found */
}

/* Wildcard pattern match
 * Accepts: base string
 *	    pattern string
 * Returns: T if pattern matches base, else NIL
 */

long pmatch (s,pat)
	char *s;
	char *pat;
{
  switch (*pat) {
  case '*':			/* match 0 or more characters */
    if (!pat[1]) return T;	/* pattern ends with * wildcard */
				/* if still more, hunt through rest of base */
    do if (pmatch (s,pat+1)) return T;
    while (*s++);
    break;
  case '%':			/* match 1 character (RFC-1176) */
  case '?':			/* match 1 character (common) */
    return pmatch (s+1,pat+1);	/* continue matching remainder */
  case '\0':			/* end of pattern */
    return *s ? NIL : T;	/* success if also end of base */
  default:			/* match this character */
    return ((isupper (*pat) ? *pat + 'a'-'A' : *pat) ==
	    (isupper (*s) ? *s + 'a'-'A' : *s)) ? pmatch (s+1,pat+1) : NIL;
  }
  return NIL;
}