1/*
2 * util.c -
3 *
4 * Written by Eryk Vershen
5 */
6
7/*
8 * Copyright 1997,1998 by Apple Computer, Inc.
9 *              All Rights Reserved
10 *
11 * Permission to use, copy, modify, and distribute this software and
12 * its documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appears in all copies and
14 * that both the copyright notice and this permission notice appear in
15 * supporting documentation.
16 *
17 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE.
20 *
21 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
23 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
24 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
25 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 */
27
28
29// for sprintf()
30#include <stdio.h>
31// for tolower()
32#include <ctype.h>
33
34#include "version.h"
35#include "util.h"
36
37
38/*
39 * Defines
40 */
41#define NumToolboxTraps() (                             \
42	(NGetTrapAddress(_InitGraf, ToolTrap)           \
43		== NGetTrapAddress(0xAA6E, ToolTrap))   \
44	    ? 0x200 : 0x400                             \
45    )
46#define GetTrapType(theTrap) (                          \
47	(((theTrap) & 0x800) != 0) ? ToolTrap : OSTrap  \
48    )
49
50
51/*
52 * Types
53 */
54
55
56/*
57 * Global Constants
58 */
59
60
61/*
62 * Global Variables
63 */
64static char dynamic_version[10];
65
66/*
67 * Forward declarations
68 */
69
70
71/*
72 * Routines
73 */
74void
75clear_memory(void *dataPtr, unsigned long size)
76{
77    char           *ptr;
78
79    ptr = (char *) dataPtr;
80    while (size > 0) {
81	*ptr++ = 0;
82	--size;
83    }
84}
85
86
87#if !defined(__linux__) && !defined(__unix__)
88/* (see Inside Mac VI 3-8) */
89int
90TrapAvailable(short theTrap)
91{
92	TrapType                trapType;
93
94	trapType = GetTrapType(theTrap);
95
96	if (trapType == ToolTrap) {
97	    theTrap &= 0x07FF;
98	    if (theTrap >= NumToolboxTraps())
99		theTrap = _Unimplemented;
100	}
101
102	return (
103	    NGetTrapAddress(theTrap, trapType)
104	    != NGetTrapAddress(_Unimplemented, ToolTrap)
105	);
106}
107#endif
108
109
110/* Ascii case-insensitive string comparison */
111int
112istrncmp(const char *x, const char *y, long len)
113{
114    const unsigned char *p = (const unsigned char *)x;
115    const unsigned char *q = (const unsigned char *)y;
116
117    while (len > 0) {
118	if (tolower(*p) != tolower(*q)) {
119	    return (*p - *q);
120	} else if (*p == 0) {
121	    break;
122	}
123	p++;
124	q++;
125	len--;
126    }
127    return (0);
128}
129
130
131const char *
132get_version_string(void)
133{
134    int stage;
135    /* "copy" of stuff from SysTypes.r, since we can't include that*/
136    enum {development = 0x20, alpha = 0x40, beta = 0x60, final = 0x80, /* or */ release = 0x80};
137
138    switch (kVersionStage) {
139    case development:	stage = 'd'; break;
140    case alpha:		stage = 'a'; break;
141    case beta:		stage = 'b'; break;
142    case final:		stage = 'f'; break;
143    default:		stage = '?'; break;
144    }
145
146    if (kVersionBugFix != 0) {
147	if (kVersionStage == final) {
148	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d.%d",
149		    kVersionMajor, kVersionMinor, kVersionBugFix);
150	} else {
151	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d.%d%c%d",
152		    kVersionMajor, kVersionMinor, kVersionBugFix, stage, kVersionDelta);
153	}
154    } else {
155	if (kVersionStage == final) {
156	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d",
157		    kVersionMajor, kVersionMinor);
158	} else {
159	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d%c%d",
160		    kVersionMajor, kVersionMinor, stage, kVersionDelta);
161	}
162    }
163    return dynamic_version;
164}
165