Back to index

im-sdk  12.3.91
genutil.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1985, 1986, 1987, 1991, 1998  The Open Group
00003 
00004 Portions Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a
00007 copy of this software and associated documentation files (the
00008 "Software"), to deal in the Software without restriction, including
00009 without limitation the rights to use, copy, modify, merge, publish,
00010 distribute, sublicense, and/or sell copies of the Software, and to
00011 permit persons to whom the Software is furnished to do so, subject to
00012 the following conditions: The above copyright notice and this
00013 permission notice shall be included in all copies or substantial
00014 portions of the Software.
00015 
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00018 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00019 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00020 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00021 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00022 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00023 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00024 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00025 
00026 
00027 Except as contained in this notice, the names of The Open Group and/or
00028 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00029 promote the sale, use or other dealings in this Software without prior
00030 written authorization from The Open Group and/or Sun Microsystems,
00031 Inc., as applicable.
00032 
00033 
00034 X Window System is a trademark of The Open Group
00035 
00036 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00037 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00038 Group. All other trademarks and registered trademarks mentioned herein
00039 are the property of their respective owners. No right, title or
00040 interest in or to any trademark, service mark, logo or trade name of
00041 Sun Microsystems, Inc. or its licensors is granted.
00042 
00043 */
00044 
00045 #include <string.h>
00046 #include <sys/types.h>
00047 
00048 /*
00049  * Appends src to the dstsize buffer at dst. The append will never
00050  * overflow the destination buffer and the buffer will always be null
00051  * terminated.
00052  */
00053 
00054 size_t
00055 _genutil_strlcat(char *dst, const char *src, size_t dstsize)
00056 {
00057        size_t l1 = strlen(dst);
00058        size_t l2 = strlen(src);
00059        size_t copied;
00060 
00061        if (dstsize == 0 || l1 >= dstsize - 1)
00062               return (l1 + l2);
00063 
00064        copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
00065        memcpy(dst + l1, src, copied);
00066        dst[l1+copied] = '\0';
00067        return (l1 + l2);
00068 }
00069 
00070 /*
00071  * Copies src to the dstsize buffer at dst. The copy will never
00072  * overflow the destination buffer and the buffer will always be null
00073  * terminated.
00074  */
00075 
00076 size_t
00077 _genutil_strlcpy(char *dst, const char *src, size_t len)
00078 {
00079        size_t slen = strlen(src);
00080        size_t copied;
00081 
00082        if (len == 0)
00083               return (slen);
00084 
00085        if (slen >= len)
00086               copied = len - 1;
00087        else
00088               copied = slen;
00089        memcpy(dst, src, copied);
00090        dst[copied] = '\0';
00091        return (slen);
00092 }
00093