darwin-xnu/osfmk/device/subrs.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
   3 *
   4 * @APPLE_LICENSE_HEADER_START@
   5 * 
   6 * The contents of this file constitute Original Code as defined in and
   7 * are subject to the Apple Public Source License Version 1.1 (the
   8 * "License").  You may not use this file except in compliance with the
   9 * License.  Please obtain a copy of the License at
  10 * http://www.apple.com/publicsource and read it before using this file.
  11 * 
  12 * This Original Code and all software distributed under the License are
  13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
  17 * License for the specific language governing rights and limitations
  18 * under the License.
  19 * 
  20 * @APPLE_LICENSE_HEADER_END@
  21 */
  22/*
  23 * @OSF_COPYRIGHT@
  24 */
  25/*
  26 *(C)UNIX System Laboratories, Inc. all or some portions of this file are
  27 *derived from material licensed to the University of California by
  28 *American Telephone and Telegraph Co. or UNIX System Laboratories,
  29 *Inc. and are reproduced herein with the permission of UNIX System
  30 *Laboratories, Inc.
  31 */
  32
  33/* 
  34 * Mach Operating System
  35 * Copyright (c) 1993,1991,1990,1989,1988 Carnegie Mellon University
  36 * All Rights Reserved.
  37 * 
  38 * Permission to use, copy, modify and distribute this software and its
  39 * documentation is hereby granted, provided that both the copyright
  40 * notice and this permission notice appear in all copies of the
  41 * software, derivative works or modified versions, and any portions
  42 * thereof, and that both notices appear in supporting documentation.
  43 * 
  44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  45 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  47 * 
  48 * Carnegie Mellon requests users of this software to return to
  49 * 
  50 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  51 *  School of Computer Science
  52 *  Carnegie Mellon University
  53 *  Pittsburgh PA 15213-3890
  54 * 
  55 * any improvements or extensions that they make and grant Carnegie Mellon
  56 * the rights to redistribute these changes.
  57 */
  58/*
  59 */
  60/*
  61 * Copyright (c) 1988 Regents of the University of California.
  62 * All rights reserved.
  63 *
  64 * Redistribution and use in source and binary forms, with or without
  65 * modification, are permitted provided that the following conditions
  66 * are met:
  67 * 1. Redistributions of source code must retain the above copyright
  68 *    notice, this list of conditions and the following disclaimer.
  69 * 2. Redistributions in binary form must reproduce the above copyright
  70 *    notice, this list of conditions and the following disclaimer in the
  71 *    documentation and/or other materials provided with the distribution.
  72 * 3. All advertising materials mentioning features or use of this software
  73 *    must display the following acknowledgement:
  74 *      This product includes software developed by the University of
  75 *      California, Berkeley and its contributors.
  76 * 4. Neither the name of the University nor the names of its contributors
  77 *    may be used to endorse or promote products derived from this software
  78 *    without specific prior written permission.
  79 *
  80 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  81 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  83 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  86 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  87 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  88 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  89 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  90 * SUCH DAMAGE.
  91 */
  92/*
  93 * Random device subroutines and stubs.
  94 */
  95
  96#include <vm/vm_kern.h>
  97#include <kern/misc_protos.h>
  98
  99/* String routines, from CMU */
 100#ifdef  strcpy
 101#undef strcmp
 102#undef strncmp
 103#undef strcpy
 104#undef strncpy
 105#undef strlen
 106#endif
 107
 108/*
 109 * Abstract:
 110 *      strcmp (s1, s2) compares the strings "s1" and "s2".
 111 *      It returns 0 if the strings are identical. It returns
 112 *      > 0 if the first character that differs in the two strings
 113 *      is larger in s1 than in s2 or if s1 is longer than s2 and
 114 *      the contents are identical up to the length of s2.
 115 *      It returns < 0 if the first differing character is smaller
 116 *      in s1 than in s2 or if s1 is shorter than s2 and the
 117 *      contents are identical upto the length of s1.
 118 */
 119
 120int
 121strcmp(
 122        register const char *s1,
 123        register const char *s2)
 124{
 125        register unsigned int a, b;
 126
 127        do {
 128                a = *s1++;
 129                b = *s2++;
 130                if (a != b)
 131                        return a-b;     /* includes case when
 132                                           'a' is zero and 'b' is not zero
 133                                           or vice versa */
 134        } while (a != '\0');
 135
 136        return 0;       /* both are zero */
 137}
 138
 139/*
 140 * Abstract:
 141 *      strncmp (s1, s2, n) compares the strings "s1" and "s2"
 142 *      in exactly the same way as strcmp does.  Except the
 143 *      comparison runs for at most "n" characters.
 144 */
 145
 146int
 147strncmp(
 148        register const char *s1,
 149        register const char *s2,
 150        size_t n)
 151{
 152        register unsigned int a, b;
 153
 154        while (n != 0) {
 155                a = *s1++;
 156                b = *s2++;
 157                if (a != b)
 158                        return a-b;     /* includes case when
 159                                           'a' is zero and 'b' is not zero
 160                                           or vice versa */
 161                if (a == '\0')
 162                        return 0;       /* both are zero */
 163                n--;
 164        }
 165
 166        return 0;
 167}
 168
 169
 170//
 171// Lame implementation just for use by strcasecmp/strncasecmp
 172//
 173static int
 174tolower(unsigned char ch)
 175{
 176    if (ch >= 'A' && ch <= 'Z')
 177        ch = 'a' + (ch - 'A');
 178
 179    return ch;
 180}
 181
 182int
 183strcasecmp(const char *s1, const char *s2)
 184{
 185    const unsigned char *us1 = (const u_char *)s1,
 186                 *us2 = (const u_char *)s2;
 187
 188    while (tolower(*us1) == tolower(*us2++))
 189        if (*us1++ == '\0')
 190            return (0);
 191    return (tolower(*us1) - tolower(*--us2));
 192}
 193
 194int
 195strncasecmp(const char *s1, const char *s2, size_t n)
 196{
 197    if (n != 0) {
 198        const unsigned char *us1 = (const u_char *)s1,
 199                     *us2 = (const u_char *)s2;
 200
 201        do {
 202            if (tolower(*us1) != tolower(*us2++))
 203                return (tolower(*us1) - tolower(*--us2));
 204            if (*us1++ == '\0')
 205                break;
 206        } while (--n != 0);
 207    }
 208    return (0);
 209}
 210
 211
 212/*
 213 * Abstract:
 214 *      strcpy copies the contents of the string "from" including
 215 *      the null terminator to the string "to". A pointer to "to"
 216 *      is returned.
 217 */
 218
 219char *
 220strcpy(
 221        register char *to,
 222        register const char *from)
 223{
 224        register char *ret = to;
 225
 226        while ((*to++ = *from++) != '\0')
 227                continue;
 228
 229        return ret;
 230}
 231
 232
 233/*
 234 * Abstract:
 235 *      strncpy copies "count" characters from the "from" string to
 236 *      the "to" string. If "from" contains less than "count" characters
 237 *      "to" will be padded with null characters until exactly "count"
 238 *      characters have been written. The return value is a pointer
 239 *      to the "to" string.
 240 */
 241
 242char *
 243strncpy(
 244        char *s1, 
 245        const char *s2,
 246        size_t n)
 247{
 248        char *os1 = s1;
 249        unsigned long i;
 250
 251        for (i = 0; i < n;)
 252                if ((*s1++ = *s2++) == '\0')
 253                        for (i++; i < n; i++)
 254                                *s1++ = '\0';
 255                else
 256                        i++;
 257        return (os1);
 258}
 259
 260/*
 261 * atoi:
 262 *
 263 *      This function converts an ascii string into an integer.
 264 *
 265 * input        : string
 266 * output       : a number
 267 */
 268
 269int
 270atoi(
 271        u_char  *cp)
 272{
 273        int     number;
 274
 275        for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++)
 276                number = (number * 10) + (*cp - '0');
 277
 278        return( number );
 279}
 280
 281/*
 282 * convert an ASCII string (decimal radix) to an integer
 283 * inputs:
 284 *      p       string pointer.
 285 *      t       char **, return a pointer to the cahr which terminates the
 286 *              numeric string.
 287 * returns:
 288 *      integer value of the numeric string.
 289 * side effect:
 290 *      pointer to terminating char.
 291 */
 292
 293int
 294atoi_term(
 295        char    *p,     /* IN */
 296        char    **t)    /* OUT */
 297{
 298        register int n;
 299        register int f;
 300
 301        n = 0;
 302        f = 0;
 303        for(;;p++) {
 304                switch(*p) {
 305                case ' ':
 306                case '\t':
 307                        continue;
 308                case '-':
 309                        f++;
 310                case '+':
 311                        p++;
 312                }
 313                break;
 314        }
 315        while(*p >= '0' && *p <= '9')
 316                n = n*10 + *p++ - '0';
 317
 318        /* return pointer to terminating character */
 319        if ( t )
 320                *t = p;
 321
 322        return(f? -n: n);
 323}
 324
 325/*
 326 * convert an integer to an ASCII string.
 327 * inputs:
 328 *      num     integer to be converted
 329 *      str     string pointer.
 330 *
 331 * outputs:
 332 *      pointer to string start.
 333 */
 334
 335char *
 336itoa(
 337        int     num,
 338        char    *str)
 339{
 340        char    digits[11];
 341        register char *dp;
 342        register char *cp = str;
 343
 344        if (num == 0) {
 345            *cp++ = '0';
 346        }
 347        else {
 348            dp = digits;
 349            while (num) {
 350                *dp++ = '0' + num % 10;
 351                num /= 10;
 352            }
 353            while (dp != digits) {
 354                *cp++ = *--dp;
 355            }
 356        }
 357        *cp++ = '\0';
 358
 359        return str;
 360}
 361
 362char *
 363strcat(
 364        register char *dest,
 365        register const char *src)
 366{
 367        char *old = dest;
 368
 369        while (*dest)
 370                ++dest;
 371        while (*dest++ = *src++)
 372                ;
 373        return (old);
 374}
 375
 376
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.