src/tycommander/tycommanderc.c
static bool start_echo_thread(HANDLE desc, echo_direction dir, char *path, size_t path_size)
{
    struct echo_context *ctx;
    HANDLE thread;

    ctx = calloc(1, sizeof(*ctx));
    if (!ctx)
        goto error;

    for (unsigned int i = 0; i < 8 && !handle_is_valid(ctx->pipe); i++) {
        unsigned int rnd;

        rand_s(&rnd);
        snprintf(path, path_size, "\\\\.\\pipe\\tycommanderc-pipe-%04x", rnd);

        ctx->pipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
                                    PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 512, 512, 0, NULL);
    }
    if (!handle_is_valid(ctx->pipe))
        goto error;

    switch (dir) {
        case ECHO_IN: {
            ctx->in = desc;
            ctx->out = ctx->pipe;
        } break;

        case ECHO_OUT: {
            ctx->in = ctx->pipe;
            ctx->out = desc;
        } break;
    }

    thread = CreateThread(NULL, 0, echo_thread, ctx, 0, NULL);
    if (!thread)
        goto error;
    CloseHandle(thread);

    return true;

error:
    if (ctx && handle_is_valid(ctx->pipe))
        CloseHandle(ctx->pipe);
    free(ctx);
    return false;
}
/* If this function succeeds, resources will be leaked when the thread ends but it
   is supposed to run until the end anyway. */
If this function succeeds, resources will be leaked when the thread ends but it
is supposed to run until the end anyway.
If this function succeeds resources will be leaked when the thread ends but it is supposed to run until the end anyway .
static bool start_echo_thread(HANDLE desc, echo_direction dir, char *path, size_t path_size)
{
    struct echo_context *ctx;
    HANDLE thread;
    ctx = calloc(1, sizeof(*ctx));
    if (!ctx)
        goto error;
    for (unsigned int i = 0; i < 8 && !handle_is_valid(ctx->pipe); i++) {
        unsigned int rnd;
        rand_s(&rnd);
        snprintf(path, path_size, "\\\\.\\pipe\\tycommanderc-pipe-%04x", rnd);
        ctx->pipe = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
                                    PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1, 512, 512, 0, NULL);
    }
    if (!handle_is_valid(ctx->pipe))
        goto error;
    switch (dir) {
        case ECHO_IN: {
            ctx->in = desc;
            ctx->out = ctx->pipe;
        } break;
        case ECHO_OUT: {
            ctx->in = ctx->pipe;
            ctx->out = desc;
        } break;
    }
    thread = CreateThread(NULL, 0, echo_thread, ctx, 0, NULL);
    if (!thread)
        goto error;
    CloseHandle(thread);
    return true;
error:
    if (ctx && handle_is_valid(ctx->pipe))
        CloseHandle(ctx->pipe);
    free(ctx);
    return false;
}
static bool start_echo_thread ( HANDLE desc , echo_direction dir , char * path , size_t path_size ) { struct echo_context * ctx ; HANDLE thread ; ctx = calloc ( 1 , sizeof ( * ctx ) ) ; if ( ! ctx ) goto error ; for ( unsigned int i = 0 ; i < 8 && ! handle_is_valid ( ctx -> pipe ) ; i ++ ) { unsigned int rnd ; rand_s ( & rnd ) ; snprintf ( path , path_size , " \\ \\ \\ \\ " , rnd ) ; ctx -> pipe = CreateNamedPipe ( path , PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE , PIPE_TYPE_BYTE | PIPE_READMODE_BYTE , 1 , 512 , 512 , 0 , NULL ) ; } if ( ! handle_is_valid ( ctx -> pipe ) ) goto error ; switch ( dir ) { case ECHO_IN : { ctx -> in = desc ; ctx -> out = ctx -> pipe ; } break ; case ECHO_OUT : { ctx -> in = ctx -> pipe ; ctx -> out = desc ; } break ; } thread = CreateThread ( NULL , 0 , echo_thread , ctx , 0 , NULL ) ; if ( ! thread ) goto error ; CloseHandle ( thread ) ; return true ; error : if ( ctx && handle_is_valid ( ctx -> pipe ) ) CloseHandle ( ctx -> pipe ) ; free ( ctx ) ; return false ; }
If this function succeeds, resources will be leaked when the thread ends but it
is supposed to run until the end anyway.
If this function succeeds resources will be leaked when the thread ends but it is supposed to run until the end anyway .