File: misc.c

package info (click to toggle)
mailagent 1%3A3.1-106-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 3,184 kB
  • sloc: perl: 15,877; sh: 8,600; ansic: 2,756; makefile: 75
file content (142 lines) | stat: -rw-r--r-- 3,144 bytes parent folder | download
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
/*

 #    #     #     ####    ####            ####
 ##  ##     #    #       #    #          #    #
 # ## #     #     ####   #               #
 #    #     #         #  #        ###    #
 #    #     #    #    #  #    #   ###    #    #
 #    #     #     ####    ####    ###     ####

	Miscellaneous routines.
*/

/*
 * $Id$
 *
 *  Copyright (c) 1990-2006, Raphael Manfredi
 *  
 *  You may redistribute only under the terms of the Artistic License,
 *  as specified in the README file that comes with the distribution.
 *  You may reuse parts of this distribution only within the terms of
 *  that same Artistic License; a copy of which may be found at the root
 *  of the source tree for mailagent 3.0.
 *
 * $Log: misc.c,v $
 * Revision 3.0.1.3  1997/09/15  15:03:04  ram
 * patch57: cosmetic change
 *
 * Revision 3.0.1.2  1996/12/24  13:59:15  ram
 * patch45: new my_exit() to allow exit code tracing for debugging
 *
 * Revision 3.0.1.1  1994/09/22  13:45:30  ram
 * patch12: added fallback implementation for strcasecmp()
 *
 * Revision 3.0  1993/11/29  13:48:16  ram
 * Baseline for mailagent 3.0 netwide release.
 *
 */

#include "config.h"
#include "portable.h"

#include <stdio.h>

#ifdef I_STDLIB
#include <stdlib.h>
#else
#ifdef I_MALLOC
#include <malloc.h>
#else
extern char *malloc();				/* Memory allocation */
#endif
#endif	/* I_STDLIB */

#ifdef I_STRING
#include <string.h>
#else
#include <strings.h>
#endif

#include <ctype.h>

#include "sysexits.h"
#include "msg.h"
#include "logfile.h"
#include "confmagic.h"

extern void my_exit();

public char *strsave(string)
char *string;
{
	/* Save string somewhere in memory and return a pointer to the new string
	 * or NULL if there is not enough memory.
	 */

	char *new = malloc(strlen(string) + 1);		/* +1 for \0 */
	
	if (new == (char *) 0)
		fatal("no more memory to save strings");

	strcpy(new, string);
	return new;
}

public void my_exit(code)
int code;
{
	/* Exit, but log the exit code... */

	char *name;					/* Symbolic error code name */
	char buf[20];				/* For unknown error codes */

#define symname(x)		case x: name = STRINGIFY(x); break;

	switch (code) {
	symname(EX_OK);
	symname(EX_USAGE);
	symname(EX_UNAVAILABLE);
	symname(EX_OSERR);
	symname(EX_OSFILE);
	symname(EX_CANTCREAT);
	symname(EX_IOERR);
	symname(EX_TEMPFAIL);
	default:
		sprintf(buf, "%d", code);
		name = buf;
		break;
	}

#undef symname

	add_log(11, "exit %s", name);
	exit(code);
}

#ifndef HAS_STRCASECMP
/*
 * This is a rather inefficient version of the strcasecmp() routine which
 * compares two strings in a case-independant manner. The libc routine uses
 * an array, which when indexed by character code, directly yields the lower
 * case version of that character. Here however, since the routine is only
 * used in a few places, we don't bother being as efficient.
 */
public int strcasecmp(s1, s2)
char *s1;
char *s2;
{
	char c1, c2;

	while (c1 = *s1++, c2 = *s2++, c1 && c2) {
		if (isupper(c1))
			c1 = tolower(c1);
		if (isupper(c2))
			c2 = tolower(c2);
		if (c1 != c2)
			break;			/* Strings are different */
	}

	return c1 - c2;			/* Will be 0 if both string ended */
}
#endif