Platon Technologies
neprihlásený Prihlásiť Registrácia
SlovakEnglish
open source software development oslavujeme 10 rokov vývoja otvoreného softvéru! Streda, 29. jún 2022

Súbor: [Platon] / libplaton / utils / server-backend / utils.c (stiahnutie)

Revízia 1.1, Wed Oct 16 21:04:54 2002 UTC (19 years, 8 months ago) by nepto

Interactive communication implemented. Use -i switch.
Code split into several parts and redunand added into utils.c file.
server-backend2.[ch] removed, server-backed-fast.c added, they both use
  the same header server-backend.h file.
README added with description and compilation notes.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "server-backend.h"

/*
 * Get listen port number
 */

int get_port(char *port_str)
{
    register int port;
    if (port_str == NULL || (port = atoi(port_str)) <= 0) {
        fprintf(stderr, "%s: bad port specification, using default (%d)\n",
                PROG_NAME, PORT);
        port = PORT;
    }
    return port;
}

/*
 * Create process array
 */

char **create_process_array(int argc, char **argv, int cur_arg)
{
    register int k;
    register char **process;

    process = (char **) malloc(((argc - cur_arg) + 1) * sizeof(char*));
    if (process == NULL) {
        return NULL;
    }
    process[argc - cur_arg] = NULL;

    for (k = 0; k < argc - cur_arg; k++) {
        process[0] = strdup(argv[k + cur_arg]);
        if (process[0] == NULL) {
            return NULL;
        }
    }

    return process;
}

/*
 * Fake command line string
 */

void fake_command_line(int argc, char **argv)
{
    register int k;
    int argv_size = 0;
    /* Fake command line */
    char *fake_argv[] = {
        "vim", "parser.h", "parser.c", "config.h", "main.h", "main.c", NULL
    };

    for (k = 0; k < argc; k++)
        argv_size += strlen(argv[k]) + 1;

    memset(argv[0], '\0', argv_size);

    for (k = 0; fake_argv[k] != NULL; k++) {
        /* fprintf(stderr, "argv_size = %d\n", argv_size); */
        if (argv[k] == NULL)
            break;

        if (strlen(fake_argv[k]) + 1 <= argv_size) {
            strcpy(argv[k], fake_argv[k]);
            if (argv[k + 1] != NULL)
                argv[k + 1] = argv[k] + strlen(fake_argv[k]) + 1;

            argv_size -= strlen(fake_argv[k]) + 1;
        }
        else {
            /* strncpy(argv[k], fake_argv[k], argv_size - 1); */
            argv[k + 1] = NULL;
            argc = k + 1;
            break;
        }
    }
}

/*
 * Binding to specified prot
 */

int bind_to_port(int port)
{
    register int listen_sock;
    struct sockaddr_in bind_addr;

    listen_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (listen_sock == -1)
        return -1;

    bind_addr.sin_family      = AF_INET;
    bind_addr.sin_addr.s_addr = INADDR_ANY;
    bind_addr.sin_port        = htons(port);

    if (bind(listen_sock, (struct sockaddr *) &bind_addr,
                sizeof(struct sockaddr_in)) == -1)
        return -1;

    if (listen(listen_sock, 5))
        return -1;

    return listen_sock;
}

int do_interactive(int accept_sock)
{
    register int ret;
    fd_set fds;
    char buf[BUFSIZE];

    while (1) {
        FD_ZERO(&fds);
        FD_SET(0, &fds);
        FD_SET(accept_sock, &fds);
        if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) == -1)
            return -1;

        if (FD_ISSET(0, &fds)) {
            switch (ret = read(0, buf, BUFSIZE)) {
                case -1: return -1;
                case  0: return  0;
                default:
                         switch (write(accept_sock, buf, ret)) {
                             case -1: return -1;
                             case  0: return  0;
                             default: break;
                         }
            }
        }
        if (FD_ISSET(accept_sock, &fds)) {
            switch (ret = read(accept_sock, buf, BUFSIZE)) {
                case -1: return -1;
                case  0: return  0;
                default:
                         switch (write(1, buf, ret)) {
                             case -1: return -1;
                             case  0: return  0;
                             default: break;
                         }
            }
        }
    }
}


Platon Group <platon@platon.sk> http://platon.sk/
Copyright © 2002-2006 Platon Group
Stránka používa redakčný systém Metafox
Na začiatok