掌機 - Pandora(Rebirth) - SuperZaxxon - 解決Implicit declaration of function getline問題



司徒在編譯Pandora的Perf工具時,tools/perf/util/probe-finder.c遇到implicit declaration of function getline問題,網路上找到的解法大致上是沒有定義_GNU_SOURCE和_POSIX_C_SOURCE 200809L的問題,不過Pandora的Library比較特別,即使定義__USE_GNU也是無效,最後只好重新抄寫一份,就可以成功編譯Perf工具了

int getline(char **lineptr, size_t *n, FILE *stream);
int getline(char **lineptr, size_t *n, FILE *stream)
{
    static char line[256];

    char *ptr;
    unsigned int len;

    if (lineptr == NULL || n == NULL) {
        errno = EINVAL;
        return -1;
    }

    if (ferror(stream)) {
        return -1;
    }

    if (feof(stream)) {
        return -1;
    }

    ptr = fgets(line, 256, stream);
    ptr = strchr(line, '\n');
    if (ptr) {
        *ptr = '\0';
    }

    len = strlen(line);

    if ((len + 1) < 256) {
        ptr = realloc(*lineptr, 256);
        if (ptr == NULL) {
            return(-1);
        }
        *lineptr = ptr;
        *n = 256;
    }

    strcpy(*lineptr, line);
    return len;
}