Home | History | Annotate | Line # | Download | only in dist
      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 #include <stdint.h>
     34 
     35 #include "version.h"
     36 #include "util.h"
     37 
     38 
     39 /*
     40  * Defines
     41  */
     42 #define NumToolboxTraps() (                             \
     43 	(NGetTrapAddress(_InitGraf, ToolTrap)           \
     44 		== NGetTrapAddress(0xAA6E, ToolTrap))   \
     45 	    ? 0x200 : 0x400                             \
     46     )
     47 #define GetTrapType(theTrap) (                          \
     48 	(((theTrap) & 0x800) != 0) ? ToolTrap : OSTrap  \
     49     )
     50 
     51 
     52 /*
     53  * Types
     54  */
     55 
     56 
     57 /*
     58  * Global Constants
     59  */
     60 
     61 
     62 /*
     63  * Global Variables
     64  */
     65 static char dynamic_version[10];
     66 
     67 /*
     68  * Forward declarations
     69  */
     70 
     71 
     72 /*
     73  * Routines
     74  */
     75 void
     76 clear_memory(void *dataPtr, uint32_t size)
     77 {
     78     char           *ptr;
     79 
     80     ptr = (char *) dataPtr;
     81     while (size > 0) {
     82 	*ptr++ = 0;
     83 	--size;
     84     }
     85 }
     86 
     87 
     88 #if !defined(__linux__) && !defined(__unix__)
     89 /* (see Inside Mac VI 3-8) */
     90 int
     91 TrapAvailable(short theTrap)
     92 {
     93 	TrapType                trapType;
     94 
     95 	trapType = GetTrapType(theTrap);
     96 
     97 	if (trapType == ToolTrap) {
     98 	    theTrap &= 0x07FF;
     99 	    if (theTrap >= NumToolboxTraps())
    100 		theTrap = _Unimplemented;
    101 	}
    102 
    103 	return (
    104 	    NGetTrapAddress(theTrap, trapType)
    105 	    != NGetTrapAddress(_Unimplemented, ToolTrap)
    106 	);
    107 }
    108 #endif
    109 
    110 
    111 /* Ascii case-insensitive string comparison */
    112 int
    113 istrncmp(const char *x, const char *y, long len)
    114 {
    115     const uint8_t *p = (const uint8_t *)x;
    116     const uint8_t *q = (const uint8_t *)y;
    117 
    118     while (len > 0) {
    119 	if (tolower(*p) != tolower(*q)) {
    120 	    return (*p - *q);
    121 	} else if (*p == 0) {
    122 	    break;
    123 	}
    124 	p++;
    125 	q++;
    126 	len--;
    127     }
    128     return (0);
    129 }
    130 
    131 
    132 const char *
    133 get_version_string(void)
    134 {
    135     int stage;
    136     /* "copy" of stuff from SysTypes.r, since we can't include that*/
    137     enum {development = 0x20, alpha = 0x40, beta = 0x60, final = 0x80, /* or */ release = 0x80};
    138 
    139     switch (kVersionStage) {
    140     case development:	stage = 'd'; break;
    141     case alpha:		stage = 'a'; break;
    142     case beta:		stage = 'b'; break;
    143     case final:		stage = 'f'; break;
    144     default:		stage = '?'; break;
    145     }
    146 
    147     if (kVersionBugFix != 0) {
    148 	if (kVersionStage == final) {
    149 	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d.%d",
    150 		    kVersionMajor, kVersionMinor, kVersionBugFix);
    151 	} else {
    152 	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d.%d%c%d",
    153 		    kVersionMajor, kVersionMinor, kVersionBugFix, stage, kVersionDelta);
    154 	}
    155     } else {
    156 	if (kVersionStage == final) {
    157 	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d",
    158 		    kVersionMajor, kVersionMinor);
    159 	} else {
    160 	    snprintf(dynamic_version, sizeof(dynamic_version), "%d.%d%c%d",
    161 		    kVersionMajor, kVersionMinor, stage, kVersionDelta);
    162 	}
    163     }
    164     return dynamic_version;
    165 }
    166