回文章列表
作者hpo14.bbs@ntust.org (雨) 看板: itoc
標題Re: [功能] 支援pmore2007互動式動畫
時間資工清流站 (2008/05/02 Fri 03:50:27)
因為 pmore.c 有使用到 strlcpy() 這個 *BSD 才有的函式,所以 Linux 使用者

使用 pmore.c 前要先做以下動作後。即可安心使用 pmore.c

(其實是我不知道怎麼樣讓 linux 的函式庫可以有 strlcpy...所以只好偷雞一下)


> 修改 src/lib/Makefile

SRC =   \
    str_ncpy.c str_passwd.c str_stamp.c str_str.c str_sub.c str_tail.c \
-    str_time.c str_trim.c str_ttl.c \
+    str_time.c str_trim.c str_ttl.c strlcat.c strlcpy.c \

OBJ =   \
    str_ncpy.o str_passwd.o str_stamp.o str_str.o str_sub.o str_tail.o \
-    str_time.o str_trim.o str_ttl.o \
+    str_time.o str_trim.o str_ttl.o strlcat.o strlcpy.o \

------------------------------------------------------------------------------

然後將以下兩個檔案儲存在 src/lib 下,檔名就照函式名稱儲存即可

                (複製虛線內的文字,不包含虛線)

> src/lib/strlcpy.c

------------------------------------------------------------------------------
/* strlcpy based on OpenBSDs strlcpy */
#include <stdio.h>
#include <sys/types.h>

size_t strlcpy(char *, const char *, size_t);

/*
 * Copy src to string dst of size siz.  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz == 0).
 * Returns strlen(src); if retval >= siz, truncation occurred.
 */
size_t
strlcpy(char *dst, const char *src, size_t siz)
{
        char *d = dst;
        const char *s = src;
        size_t n = siz;

        /* Copy as many bytes as will fit */
        if (n != 0 && --n != 0) {
                do {
                        if ((*d++ = *s++) == 0)
                                break;
                } while (--n != 0);
        }

        /* Not enough room in dst, add NUL and traverse rest of src */
        if (n == 0) {
                if (siz != 0)
                        *d = '\0';                /* NUL-terminate dst */
                while (*s++)
                        ;
        }

        return(s - src - 1);        /* count does not include NUL */
}
------------------------------------------------------------------------------


> src/lib/strlcat.c
------------------------------------------------------------------------------
/* strlcat based on OpenBSDs strlcat */
#include <sys/types.h>

size_t  strlcat(char *, const char *, size_t);

/*
 * Appends src to string dst of size siz (unlike strncat, siz is the
 * full size of dst, not space left).  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 * If retval >= siz, truncation occurred.
 */
size_t
strlcat(char *dst, const char *src, size_t siz)
{
        char *d = dst;
        const char *s = src;
        size_t n = siz;
        size_t dlen;

        /* Find the end of dst and adjust bytes left but don't go past end */
        while (n-- != 0 && *d != '\0')
                d++;
        dlen = d - dst;
        n = siz - dlen;

        if (n == 0)
                return(dlen + strlen(s));
        while (*s != '\0') {
                if (n != 1) {
                        *d++ = *s;
                        n--;
                }
                s++;
        }
        *d = '\0';

        return(dlen + (s - src));        /* count does not include NUL */
}
------------------------------------------------------------------------------
--
>> 台灣科技大學 資工清流站 # ntust.org #
>> From: 61-59-78-243.adsl.static.seed.net.tw
文章選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻