荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: jjk (UNIX+C+XML+?? 傻了?), 信区: Linux
标  题: [Z]SmartGirl-0.0.0-beta1               guru (转寄)[转载]
发信站: 荔园晨风BBS站 (Thu Apr 25 10:25:41 2002), 转信

【 以下文字转载自 jjk 的信箱 】
【 原文由 jjk.bbs@apue.dhs.org 所发表 】
发信人: SingleBoy (孤鹰★忧伤的夜鸟), 信区: Linux
标  题: SmartGirl-0.0.0-beta1
发信站: 华南网木棉站 (Tue Jan  4 04:23:32 2000), 转信


/****************************************************************
*        本程序遵循 GPL 发布, 可以自由和免费地被复制, 使用
*            发布的目的是使它有用, 不提供任何承诺和保障.
*
*        如果本程序被修改, 您必须保证本段文字随改动后的程序
*            一起发布, 并且修改者的信息追加到下面的作者列表
*            的后面
*
*                作    者: Rocky S. Lee
*                邮    箱: coolrocky@21cn.com
*****************************************************************/
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>

#define VERSION "0.0.0-beta1"

#define MAX_FD_NUM              20

#define TRUE    1
#define FALSE   0

#define FAILED                  -1
#define SUCCESS                 0
#define NICKNAME_UNKNOWN        1
#define LOGIN_NAME_ERROR        2
//HERE CAN DEFINE OTHER CONSTANTS

char* error[] = {
        "Success.",
        "The LOGIN NAME may be wrong."
};

#define TIME_OUT 60

#define MAXPATHLEN 4095
#define COMMAND "telnet"

struct account_info {
        char nickname[32];
        char realaddress[32];
};

int setup_mode(struct termios* old_term_param);
int reset_mode(struct termios* old_term_param);

int read_string(int handle, char* buffer, int buf_len);

int get_account_info(char* nickname, struct account_info* info_record);
int enjoy_it(int fd_read, int fd_write);

int main(int argc, char* argv[])
{
        int fd_read, fd_write, f_in[2], f_out[2];
        char* argus[3];
        char* version = VERSION;
        struct account_info info_record;
        pthread_t* thread_anti_idle_id;

        printf(";34mThanks for using SmartGirl %s!\n0m", version);

        if (argc <2) {
                printf("Too few arguments!\n");
                printf("Usage:\n\t%s <NICK NAME>\n", argv[0]);
                exit(0);
        }

        if (get_account_info(argv[1], &info_record) != 0) {
                printf("The target \"%s\" is unknown ...\n", argv[1]);
                /* The code passes argv[1] to telnet goes here */
                exit(1);
        }

        pipe(f_in);
        pipe(f_out);
        switch (fork()) {
                case -1:                        /* cannot fork child process */
                        perror("Cannot fork a subroutine.\n");
                        exit(1);
                case 0:                 /* child process */
                        dup2(f_in[0], STDIN_FILENO);
                        close(f_in[0]);
                        close(f_in[1]);
                        dup2(f_out[1], STDOUT_FILENO);
                        close(f_out[0]);
                        close(f_out[1]);
                        argus[0] = COMMAND;
                        argus[1] = info_record.realaddress;
                        argus[2] = NULL;
                        if ((execvp("telnet", &argus[0]))<0) {
                                perror("The execvp of command faild ...\n");
                                exit(1);
                        }
                        break;
                default:                        /* parent process */
                        close(f_in[0]);
                        fd_write = f_in[1];
                        close(f_out[1]);
                        fd_read = f_out[0];
                        enjoy_it(fd_read, fd_write);
                        break;
        }

        close(f_in[0]);
        close(f_in[1]);
        close(f_out[0]);
        close(f_out[1]);

        return 0;
}

int get_account_info(char* nickname, struct account_info* info_record)
{
        int i, conf_file, found;
        char path[MAXPATHLEN];

        if ((getenv("HOME")) == NULL) {
                perror("Home directory no found!\n");
                return -1;
        }

        strcpy(path, getenv("HOME"));
        strcat(path, "/.go_rc");

        if ((conf_file=open(path, O_RDONLY)) == -1) {
                perror("Cannot open config file ...\n");
                return -1;
        }

        found = 0;
        while (read_string(conf_file, info_record->nickname, 32) != 0
                && read_string(conf_file, info_record->realaddress, 32) != 0) {
                if ((strcmp(nickname, info_record->nickname)) == 0) {
                        found = 1;
                        break;
                }
        }
        close(conf_file);

        if (found)
                return SUCCESS;
        else
                return NICKNAME_UNKNOWN;
}

int read_string(int handle, char* buffer, int buf_len)
{
        char* bufp = buffer;
        int counter = 0, i;

        for (i = 0; i < buf_len; i++)
                buffer[i] = '\0';

        while (((i = read(handle, bufp, 1)) != 0) && counter < buf_len) {
                if (*bufp == ' ' || *bufp == '\t'
                        || *bufp == '\n' || *bufp == '\r') {
                        *bufp = '\0';
                        break;
                }
                counter++;
                bufp++;
        }

        if (counter >= buf_len)
                *bufp = '\0';

        return counter;
}

int enjoy_it(int fd_read, int fd_write)
{
        char buff;
#define BUFFER_SIZE 1024
        char buffer[BUFFER_SIZE];
        int counter;
        fd_set fdR, fdW;
        struct timeval time_value;
        struct termios old_term_param;

        setup_mode(&old_term_param);
        while (1) {
                FD_ZERO(&fdR);
                FD_SET(fd_read, &fdR);
                FD_SET(STDIN_FILENO, &fdR);

                time_value.tv_sec = TIME_OUT;
                time_value.tv_usec = 0;
                if (select(MAX_FD_NUM + 1, &fdR, NULL, NULL, &time_value) == 0)

                        buff = 12;
                        write(fd_write, &buff, 1);
                        continue;
                }

                if (FD_ISSET(STDIN_FILENO, &fdR)) {
                        if (read(STDIN_FILENO, &buff, 1) == 1) {
                                if (buff == 29) {
                                        write(fd_write, "\035quit\n", 6);
                                        write(STDOUT_FILENO, "\n", 1);
                                        break;
                                }
                                if (write(fd_write, &buff, 1) != 1)
                                        break;
                        }
                }
                if (FD_ISSET(fd_read, &fdR)) {
                        if ((counter = read(fd_read, buffer, BUFFER_SIZE)) > 0)
                                write(STDOUT_FILENO, buffer, counter);
                        else
                                break;
                }
        }
        reset_mode(&old_term_param);

        return SUCCESS;
}

int setup_mode(struct termios* old_term_param)
{
        struct termios current_term_param;

        if (!isatty(STDIN_FILENO))
                return FAILED;

        if (tcgetattr(STDIN_FILENO, &current_term_param) == -1)
                return FAILED;

        *old_term_param = current_term_param;
        current_term_param.c_lflag &= ~(ECHO | ICANON | ISIG);
        current_term_param.c_cc[VMIN] = 0;
        current_term_param.c_cc[VTIME] = 0;
        if(tcsetattr(0,TCSANOW,&current_term_param) == -1)
                return FAILED;
        else
                return SUCCESS;
}

int reset_mode(struct termios* old_term_param)
{
        if (tcsetattr(STDIN_FILENO, TCSANOW, old_term_param) != 0)
                return FAILED;
        else
                return SUCCESS;
}

--
--
※ 转寄:·UNIX编程 apue.dhs.org·[FROM: 210.39.3.50]
--
※ 转载:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.0.146]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店