~ubuntu-branches/ubuntu/hardy/transmission/hardy-updates

« back to all changes in this revision

Viewing changes to libtransmission/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Benner
  • Date: 2007-12-05 14:37:05 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20071205143705-env7ahsls1gyb2eo
Tags: upstream-0.95.dfsg
ImportĀ upstreamĀ versionĀ 0.95.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: utils.c 3665 2007-10-31 18:10:54Z charles $
 
2
 * $Id: utils.c 3972 2007-11-26 05:03:34Z charles $
3
3
 *
4
4
 * Copyright (c) 2005-2007 Transmission authors and contributors
5
5
 *
678
678
{
679
679
    char * out = NULL;
680
680
 
681
 
    if( in != NULL )
 
681
    if( len < 0 )
 
682
    {
 
683
        out = tr_strdup( in );
 
684
    }
 
685
    else if( in != NULL )
682
686
    {
683
687
        out = tr_malloc( len+1 );
684
688
        memcpy( out, in, len );
823
827
}
824
828
 
825
829
tr_bitfield*
826
 
tr_bitfieldAnd( tr_bitfield * a, const tr_bitfield * b )
 
830
tr_bitfieldOr( tr_bitfield * a, const tr_bitfield * b )
827
831
{
828
832
    uint8_t *ait;
829
833
    const uint8_t *aend, *bit;
831
835
    assert( a->len == b->len );
832
836
 
833
837
    for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; ++ait, ++bit )
834
 
        *ait &= *bit;
 
838
        *ait |= *bit;
835
839
 
836
840
    return a;
837
841
}
892
896
    usleep( 1000 * delay_milliseconds );
893
897
#endif
894
898
}
 
899
 
 
900
/***
 
901
****
 
902
***/
 
903
 
 
904
 
 
905
#ifndef HAVE_STRLCPY
 
906
 
 
907
/*
 
908
 * Copy src to string dst of size siz.  At most siz-1 characters
 
909
 * will be copied.  Always NUL terminates (unless siz == 0).
 
910
 * Returns strlen(src); if retval >= siz, truncation occurred.
 
911
 */
 
912
size_t
 
913
strlcpy(char *dst, const char *src, size_t siz)
 
914
{
 
915
        char *d = dst;
 
916
        const char *s = src;
 
917
        size_t n = siz;
 
918
 
 
919
        /* Copy as many bytes as will fit */
 
920
        if (n != 0) {
 
921
                while (--n != 0) {
 
922
                        if ((*d++ = *s++) == '\0')
 
923
                                break;
 
924
                }
 
925
        }
 
926
 
 
927
        /* Not enough room in dst, add NUL and traverse rest of src */
 
928
        if (n == 0) {
 
929
                if (siz != 0)
 
930
                        *d = '\0';              /* NUL-terminate dst */
 
931
                while (*s++)
 
932
                        ;
 
933
        }
 
934
 
 
935
        return(s - src - 1);    /* count does not include NUL */
 
936
}
 
937
 
 
938
#endif /* HAVE_STRLCPY */
 
939
 
 
940
 
 
941
#ifndef HAVE_STRLCAT
 
942
 
 
943
/*
 
944
 * Appends src to string dst of size siz (unlike strncat, siz is the
 
945
 * full size of dst, not space left).  At most siz-1 characters
 
946
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
 
947
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 
948
 * If retval >= siz, truncation occurred.
 
949
 */
 
950
size_t
 
951
strlcat(char *dst, const char *src, size_t siz)
 
952
{
 
953
        char *d = dst;
 
954
        const char *s = src;
 
955
        size_t n = siz;
 
956
        size_t dlen;
 
957
 
 
958
        /* Find the end of dst and adjust bytes left but don't go past end */
 
959
        while (n-- != 0 && *d != '\0')
 
960
                d++;
 
961
        dlen = d - dst;
 
962
        n = siz - dlen;
 
963
 
 
964
        if (n == 0)
 
965
                return(dlen + strlen(s));
 
966
        while (*s != '\0') {
 
967
                if (n != 1) {
 
968
                        *d++ = *s;
 
969
                        n--;
 
970
                }
 
971
                s++;
 
972
        }
 
973
        *d = '\0';
 
974
 
 
975
        return(dlen + (s - src));       /* count does not include NUL */
 
976
}
 
977
 
 
978
#endif /* HAVE_STRLCAT */