⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ansistdlib.c

📁 VXWORKS源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
*/void div_r    (    int     numer,          /* numerator */    int     denom,          /* denominator */    div_t * divStructPtr    /* div_t structure */    )    {    /* calculate quotient */    divStructPtr->quot = numer / denom;    /* calculate remainder */    divStructPtr->rem = numer - (denom * divStructPtr->quot);    /* check for negative quotient */    if ((divStructPtr->quot < 0) && (divStructPtr->rem > 0))        {        divStructPtr->quot++;        divStructPtr->rem -= denom;        }    }/* labs.c - labs file for stdlib  *//* Copyright 1992-1993 Wind River Systems, Inc. *//*modification history--------------------01c,08feb93,jdi  documentation cleanup for 5.1.01b,20sep92,smb  documentation additions.01a,19jul92,smb  written.*//* DESCRIPTION  INCLUDE FILE: stdlib.h  SEE ALSO: American National Standard X3.159-1989 NOMANUAL*/ #include "vxWorks.h"#include "stdlib.h"/********************************************************************************* labs - compute the absolute value of a `long' (ANSI)** This routine computes the absolute value of a specified `long'.  If the* result cannot be represented, the behavior is undefined.  This routine is* equivalent to abs(), except that the argument and return value are all of* type `long'.** INCLUDE FILES: stdlib.h ** RETURNS: The absolute value of <i>.*/long labs     (    long i          /* long for which to return absolute value */    )    {    return (i >= 0 ? i : -i);    }/* ldiv.c - ldiv file for stdlib  *//* Copyright 1992-1993 Wind River Systems, Inc. *//*modification history--------------------01e,08feb93,jdi  documentation cleanup for 5.1.01d,20sep92,smb  documentation additions.01c,24jul92,smb  corrected parameter ordering.01b,24jul92,smb  added reentrant version of ldiv()01a,19jul92,smb  written.*//*DESCRIPTIONINCLUDE FILES: stdlib.hSEE ALSO: American National Standard X3.159-1989NOMANUAL*/#include "vxWorks.h"#include "stdlib.h"/********************************************************************************* ldiv - compute the quotient and remainder of the division (ANSI)** This routine computes the quotient and remainder of <numer>/<denom>.* This routine is similar to div(), except that the arguments and the* elements of the returned structure are all of type `long'.** This routine is not reentrant.  For a reentrant version, see ldiv_r().** INCLUDE FILES: stdlib.h ** RETURNS:* A structure of type `ldiv_t', containing both the quotient and the * remainder. */ldiv_t ldiv     (    long numer, 	/* numerator */    long denom		/* denominator */    )     {    static ldiv_t divStruct;		    ldiv_r (numer, denom, &divStruct);     return (divStruct);    } /********************************************************************************* ldiv_r - compute a quotient and remainder (reentrant)** This routine computes the quotient and remainder of <numer>/<denom>.* The quotient and remainder are stored in the `ldiv_t' structure* `divStructPtr'.** This routine is the reentrant version of ldiv().** INCLUDE FILES: stdlib.h ** RETURNS: N/A*/void ldiv_r    (    long     numer,          /* numerator */    long     denom,          /* denominator */    ldiv_t * divStructPtr    /* ldiv_t structure */    )    {    /* calculate quotient */    divStructPtr->quot = numer / denom;     /* calculate remainder */    divStructPtr->rem = numer - (denom * divStructPtr->quot);     /* check for negative quotient */    if ((divStructPtr->quot < 0) && (divStructPtr->rem > 0))        {        divStructPtr->quot++;        divStructPtr->rem -= denom;        }    }   /* multibyte.c - multibyte file for stdlib  *//* Copyright 1992-1993 Wind River Systems, Inc. *//*modification history--------------------01c,08feb93,jdi  documentation cleanup for 5.1.01b,20sep92,smb  documentation additions.01a,19jul92,smb  written and documented.*//*DESCRIPTIONThese ignore the current fixed ("C") locale andalways indicate that no multibyte characters are supported.INCLUDE FILES: stdlib.hSEE ALSO: American National Standard X3.159-1989NOMANUAL*/#include "vxWorks.h"#include "stdlib.h"#include "string.h"/******************************************************************************** mblen - calculate the length of a multibyte character (Unimplemented) (ANSI)* * This multibyte character function is unimplemented in VxWorks.** INCLUDE FILES: stdlib.h ** RETURNS: OK, or ERROR if the parameters are invalid.*/int mblen    (    const char * s,    size_t       n    )    {    if ((strcmp (s,NULL) == 0) && (n == 0) && (*s == EOS))    	return (ERROR);    return (OK);    }/******************************************************************************** mbtowc - convert a multibyte character to a wide character (Unimplemented) (ANSI)* * This multibyte character function is unimplemented in VxWorks.** INCLUDE FILES: stdlib.h ** RETURNS: OK, or ERROR if the parameters are invalid.*/int mbtowc    (    wchar_t *    pwc,    const char * s,    size_t       n    )    {    if ((strcmp (s,NULL) == 0) && (n == 0) && (*s == EOS))    	return (ERROR);    return (OK);    }/******************************************************************************** wctomb - convert a wide character to a multibyte character (Unimplemented) (ANSI)* * This multibyte character function is unimplemented in VxWorks.** INCLUDE FILES: stdlib.h ** RETURNS: OK, or ERROR if the parameters are invalid.*/int wctomb    (    char *  s,     wchar_t wchar    )    {    if (strcmp (s,NULL) == 0)    	return (ERROR);    return (OK);    }/******************************************************************************** mbstowcs - convert a series of multibyte char's to wide char's (Unimplemented) (ANSI)* * This multibyte character function is unimplemented in VxWorks.** INCLUDE FILES: stdlib.h ** RETURNS: OK, or ERROR if the parameters are invalid.*/size_t mbstowcs    (    wchar_t *	 pwcs,    const char * s,    size_t       n    )    {    if ((strcmp (s,NULL) == 0) && (n == 0) && (*s == EOS))    	return (ERROR);    return (OK);    }/******************************************************************************** wcstombs - convert a series of wide char's to multibyte char's (Unimplemented) (ANSI)* * This multibyte character function is unimplemented in VxWorks.** INCLUDE FILES: stdlib.h ** RETURNS: OK, or ERROR if the parameters are invalid.*/size_t wcstombs    (    char *          s,    const wchar_t * pwcs,    size_t          n    )    {    if ((strcmp (pwcs,NULL) == 0) && (n == 0) && (*pwcs == (wchar_t) EOS))    	return (ERROR);    return (OK);    }/* qsort.c - qsort file for stdlib  *//* Copyright 1992-1993 Wind River Systems, Inc. *//*modification history--------------------01d,01jul93,jmm  fixed parameter order of quick_sort and insertion_sort (spr 2202)01c,08feb93,jdi  documentation cleanup for 5.1.01b,20sep92,smb  documentation additions.01a,19jul92,smb  written and documented.*//*DESCRIPTION * Copyright (c) 1980, 1983, 1990 The Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. *INCLUDE FILES: stdlib.hSEE ALSO: American National Standard X3.159-1989NOMANUAL*/#include "vxWorks.h"#include "stdlib.h"/* * MTHRESH is the smallest partition for which we compare for a median * value instead of using the middle value. */#define	MTHRESH	6/* * THRESH is the minimum number of entries in a partition for continued * partitioning. */#define	THRESH	4static void insertion_sort(), quick_sort(); /* * Swap two areas of size number of bytes.  Although qsort(3) permits random * blocks of memory to be sorted, sorting pointers is almost certainly the * common case (and, were it not, could easily be made so).  Regardless, it * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer * arithmetic gets lost in the time required for comparison function calls. */#define	SWAP(a, b) 						\    {						 		\    cnt = size; 						\    do 								\	{ 							\    	ch = *a; 						\    	*a++ = *b; 						\    	*b++ = ch; 						\        } while (--cnt); 					\    }/* * Knuth, Vol. 3, page 116, Algorithm Q, step b, argues that a single pass * of straight insertion sort after partitioning is complete is better than * sorting each small partition as it is created.  This isn't correct in this * implementation because comparisons require at least one (and often two) * function calls and are likely to be the dominating expense of the sort. * Doing a final insertion sort does more comparisons than are necessary * because it compares the "edges" and medians of the partitions which are * known to be already sorted. * * This is also the reasoning behind selecting a small THRESH value (see * Knuth, page 122, equation 26), since the quicksort algorithm does less * comparisons than the insertion sort. */#define	SORT(bot, n) 						\    { 								\    if (n > 1) 							\    	if (n == 2) 						\	    { 							\    	    t1 = bot + size; 					\    	    if (compar (t1, bot) < 0) 				\    	    	SWAP (t1, bot); 				\    	    } 							\	else 							\    	    insertion_sort(bot, n, size, compar); 		\    }/******************************************************************************** qsort - sort an array of objects (ANSI)** This routine sorts an array of <nmemb> objects, the initial element of* which is pointed to by <bot>.  The size of each object is specified by* <size>.** The contents of the array are sorted into ascending order according to a* comparison function pointed to by <compar>, which is called with two* arguments that point to the objects being compared.  The function shall* return an integer less than, equal to, or greater than zero if the first* argument is considered to be respectively less than, equal to, or greater* than the second.** If two elements compare as equal, their order in the sorted array is* unspecified.** INCLUDE FILES: stdlib.h** RETURNS: N/A*/void qsort    (    void *	bot,		/* initial element in array */    size_t	nmemb,		/* no. of objects in array */    size_t	size,		/* size of array element */    int	(*compar) (const void *, const void *)  /* comparison function */    )    {    /* static void insertion_sort(), quick_sort(); */    if (nmemb <= 1)    	return;    if (nmemb >= THRESH)    	quick_sort (bot, nmemb, size, compar);    else    	insertion_sort (bot, nmemb, size, compar);    }/******************************************************************************** quick_sort - sort an array of objects.** RETURNS: no value.* NOMANUAL*/static void quick_sort    (    FAST char *  bot,    int          nmemb,    FAST int     size,    int          (*compar)()    )    {    FAST int 	  cnt;    FAST uint_t	  ch;    FAST char *   top;    FAST char *   mid;    FAST char *   t1;    FAST char *   t2;    FAST int      n1;    FAST int      n2;    char *        bsv;    /* bot and nmemb must already be set. */partition:    /* find mid and top elements */    mid = bot + size * (nmemb >> 1);    top = bot + (nmemb - 1) * size;    /*     * Find the median of the first, last and middle element (see Knuth,     * Vol. 3, page 123, Eq. 28).  This test order gets the equalities     * right.     */    if (nmemb >= MTHRESH) 	{    	n1 = compar (bot, mid);    	n2 = compar (mid, top);    	if ((n1 < 0) && (n2 > 0))    		t1 = compar (bot, top) < 0 ? top : bot;    	else 	    if ((n1 > 0) && (n2 < 0))    		t1 = compar (bot, top) > 0 ? top : bot;    	else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -