⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commu_db.cxx

📁 LINUX下的收发E-MAIL的程序
💻 CXX
字号:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
//#include <sys/sem.h>
#ifdef	__cplusplus
extern "C" {
#endif
#include <lldbs.h>
#ifdef	__cplusplus
}
#endif
#include "commu_db.h"
#include "ppp_control.h"
#define RECORDSIZE (sizeof(struct commu_style_record))
struct commu_style_record {
    char ap_name[20];
    char commu_style[10];
    int style;
};

int create_reg_db()
{
    struct commu_style_record record_IA_huc = { "IA-huc", "obex", 0 };
    struct commu_style_record record_Email = { "Email", "ppp", 0 };
    struct commu_style_record record_INFO = { "INFO", "ppp", 0 };

    struct commu_style_record *p;
    int key_offset, key_length;
    struct key_field kf;
    int data_id;

    get_field_offset_and_length(commu_style_record, ap_name, key_offset,
				key_length);

    kf.offset = key_offset;
    kf.size = key_length;
    kf.key_type = KEY_STRING;
    data_id = (int) db_register("cm_sl", &kf, RECORDSIZE, RECORDSIZE);
    if (data_id == 0) {
	db_create("cm_sl");
	data_id = db_register("cm_sl", &kf, RECORDSIZE, RECORDSIZE);
	p = &record_Email;
	db_add_record(data_id, p, RECORDSIZE);
	p = &record_INFO;
	db_add_record(data_id, p, RECORDSIZE);
	p = &record_IA_huc;
	db_add_record(data_id, p, RECORDSIZE);
    }
    return data_id;
}

int get_commu_style(int data_id, char *ap_name)
{
    int k;
    char *buffer = NULL;
    buffer = (char *) malloc(RECORDSIZE);
    if (buffer == NULL)
	return -1;
    k = db_search_key(data_id, ap_name, buffer, strlen(ap_name));
    if (k == 0) {
	free(buffer);
	return -1;
    }
    k = ((struct commu_style_record *) buffer)->style;
    free(buffer);
    return k;
}

int set_commu_style(int data_id, char *ap_name, int style)
{
    int k;
    char *buffer;

    buffer = (char *) malloc(RECORDSIZE);
    k = db_search_key(data_id, ap_name, buffer, strlen(ap_name));
    if (k == 0) {
	free(buffer);
	return -1;
    }
    ((struct commu_style_record *) buffer)->style = style;
    k = db_modify_current(data_id, buffer, RECORDSIZE);
    if (k == 0) {
	free(buffer);
	return -1;
    }
    return 1;
}

int exit_commu_db(int data_id)
{
    db_unregister(data_id);

    return 0;
}

int ApStartCommuStyle(char *ap_name)
{
    int OB_style, p1_style, p2_style;
    int data_id;
    char str1[10] = "IA-huc";
    char str2[10] = "Email";
    char str3[10] = "INFO";
    data_id = create_reg_db();
    if (data_id < 0) {
	return -1;
    }
    OB_style = get_commu_style(data_id, str1);
    p1_style = get_commu_style(data_id, str2);
    p2_style = get_commu_style(data_id, str3);

    if ((OB_style < 0) || (p1_style < 0) || (p2_style < 0)) {
	exit_commu_db(data_id);
	return -1;
    }

    if (!strcmp(ap_name, str1)) {
	//"IA-huc" starting.............
	if ((p1_style == 1) || (p2_style == 1)) {
	    exit_commu_db(data_id);
	    return 0;
	}
	printf("IA-huc obex starting.........................\n");
	if (set_commu_style(data_id, str1, 1) < 0) {
	    exit_commu_db(data_id);
	    return -1;
	}
	exit_commu_db(data_id);
    }
    if (!strcmp(ap_name, str2)) {
	//"Email" starting .............
	if (OB_style == 1) {
	    exit_commu_db(data_id);
	    return 0;
	}
	if ((p1_style == 0) && (p2_style == 0)) {
	    printf("Email starting ppp.............\n");
	    start_ppp();
	}
	printf("Email ap starting ..........................\n");
	if (set_commu_style(data_id, str2, 1) < 0) {
	    exit_commu_db(data_id);
	    return -1;
	}
	exit_commu_db(data_id);
    }
    if (!strcmp(ap_name, str3)) {
	//"INFO " starting .............
	if (OB_style == 1) {
	    exit_commu_db(data_id);
	    return 0;
	}
	if ((p1_style == 0) && (p2_style == 0)) {
	    printf("INFO starting ppp.................\n");
	    start_ppp();
	}
	printf("INFO ap starting ............................\n");
	if (set_commu_style(data_id, str3, 1) < 0) {
	    exit_commu_db(data_id);
	    return -1;
	}
	exit_commu_db(data_id);
    }
    return 1;
}

int ApStopCommuStyle(char *ap_name)
{
    int data_id;
    int p1_style, p2_style;
    char str1[10] = "IA-huc";
    char str2[10] = "Email";
    char str3[10] = "INFO";
    data_id = create_reg_db();
    if (data_id < 0) {
	return -1;
    }
    p1_style = get_commu_style(data_id, str2);
    p2_style = get_commu_style(data_id, str3);

    if (!strcmp(ap_name, str1)) {
	//"IA-huc" stop .................
	if (set_commu_style(data_id, str1, 0) < 0) {
	    exit_commu_db(data_id);
	    return -1;
	}
	exit_commu_db(data_id);
    }
    if (!strcmp(ap_name, str2)) {
	//"Email " stop .................
	printf("Email.stop.....................\n");
	if (p2_style == 0)
	    stop_ppp();
	if (set_commu_style(data_id, str2, 0) < 0) {
	    exit_commu_db(data_id);
	    return -1;
	}
	exit_commu_db(data_id);
    }
    if (!strcmp(ap_name, str3)) {
	//"INFO "  stop .................
	if (p1_style == 0)
	    stop_ppp();
	if (set_commu_style(data_id, str3, 0) < 0) {
	    exit_commu_db(data_id);
	    return -1;
	}
	exit_commu_db(data_id);
    }
    return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -