Skip to content

add dup() function#380

Open
diamant3 wants to merge 2 commits into
pspdev:masterfrom
diamant3:add-dup-function
Open

add dup() function#380
diamant3 wants to merge 2 commits into
pspdev:masterfrom
diamant3:add-dup-function

Conversation

@diamant3

Copy link
Copy Markdown
Member

Tested only on PPSSPP

#include <pspkernel.h>
#include <pspdebug.h>

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

PSP_MODULE_INFO("dup_test", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);

#define printf pspDebugScreenPrintf

#define TEST_ASSERT(cond, msg)                 \
    do {                                       \
        if (cond)                              \
            printf("[PASS] %s\n", msg);        \
        else {                                 \
            printf("[FAIL] %s\n", msg);        \
            printf("       errno=%d (%s)\n",   \
                   errno, strerror(errno));    \
        }                                      \
    } while(0)

int exit_callback(int arg1, int arg2, void *common)
{
    sceKernelExitGame();
    return 0;
}


int CallbackThread(SceSize args, void *argp)
{
    int cbid = sceKernelCreateCallback(
        "Exit Callback",
        exit_callback,
        NULL);

    sceKernelRegisterExitCallback(cbid);

    sceKernelSleepThreadCB();

    return 0;
}


int main()
{
    pspDebugScreenInit();

    int fd_orig = open("test_dup.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd_orig < 0) {
        perror("Failed to open file");
        return 1;
    }

    int fd_copy = dup(fd_orig);
    if (fd_copy < 0) {
        perror("dup() failed");
        close(fd_orig);
        return 1;
    }
    printf("Original FD: %d, Duplicated FD: %d\n", fd_orig, fd_copy);

    const char *msg1 = "Hello ";
    write(fd_orig, msg1, strlen(msg1));

    const char *msg2 = "World!\n";
    write(fd_copy, msg2, strlen(msg2));


    close(fd_orig);
    close(fd_copy);
    
    printf("Test complete. Check 'test_dup.txt' for 'Hello World!'\n");

    sceKernelSleepThread();

    return 0;
}

@bucanero bucanero left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep this change as draft until it can be verified and working on real hardware

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants