popenとpclose

#include < stdio.h >
#include < signal.h >
#include < errno.h >

#define R (0)
#define W (1)

static struct pid {
    struct pid *next;
    FILE *fp;
    pid_t pid;
} *pidlist;

FILE *popen_err(char *command,char *option)
{
    struct pid *cur;
    int pipe_c2p_e[2];
    int pid;
    FILE *fp;
    
    if ((*option != 'r') || option[1]){
        fprintf(stderr,"popen_err():option error:return(NULL)\n");
        return (NULL);
    }
    if ((cur = (struct pid *)malloc(sizeof(struct pid))) == NULL){
        fprintf(stderr,"popen_err():malloc error:return(NULL)\n");
        return (NULL);
    }
    if(piep(pipe_c2p_e)<0){
        perror("pipe");
        return(NULL);
    }
    if((pid=fork())<0){
        perror("fork");
        close(pipe_c2p_e[R]);
        close(pipe_c2p_e[W]);
        return(NULL);
    }
    if(pid==0){
        close(pipe_c2p_e[R]);
        dup2(pipe_c2p_e[W],2);
        close(pipe_c2p_e[W]);
        execlp("sh","sh","-c",command,NULL);
        _exit(127);
    }
    close(pipe_c2p_e[W]);
    fp=fdopen(pipe_c2p_e[R],option);
    cur->fp = fp;
    cur->pid = pid;
    cur->next = pidlist;
    pidlist = cur;
    
    return(fp);
}

int pclose_err(FILE *fp)
{
    register struct pid *cur, *last;
#if BSD /* for */
    int omask;
#else /* SYSV */
    sigset_t set,omask;
#endif
    
    int pstat;
    pid_t pid;
    extern int errno;
    
    fclose(fp);
    for(last = NULL, cur = pidlist; cur; last = cur, cur = cur->next){
        if (cur->fp == fp){
            break;
        }
    }
    if (cur == NULL){
        return (-1);
    }
#if BSD /* for BSD */
    omask = sigblock(sigmask(SIGINT|sigmask(SIGQUIT)|sigmask(SIGHUP));
                     do {
                         pid = waitpid(cur->pid, (int *) &pstat, 0);
                     } while (pid == -1 && errno == EINTER);
                     (void)sigsetmask(omask);
}
#else /* SYSV */
                     sigemptyset(&set);
                     sigaddset(&set,SIGINT);
                     sigaddset(&set,SIGQUIT);
                     sigaddset(&set,SIGHUP);
                     sigprocmask(SIG_SETMASK,&set,&omask);
                     do {
                         pid = waitpid(cur->pid, (int *)&pstat, 0);
                     } while (pid == -1 && errno == EINTR);
                     sigprocmask(SIG_SETMASK, &omask,NULL);
#endif
                     if (last == NULL){
                         pidlist = cur->next;
                     }
                     else {
                         last->next = cur->next;
                     }
                     free(cur);

                     return (pid == -1 ? -1 : pstat);
                     }

execlp

#include < stdio.h >
#include < signal.h >

#define R (0)
#define W (1)

int popen2(char *command,int *fd_r, int *fd_w)
{
    int pipe_c2p[2],pipe_p2c[2];
    int pid;
    
    if(pipe(pipe_c2p)<0){
        perror("popen2");
        return(-1);
    }
    if(pipe(pipe_p2c)<0){
        perror("popen2");
        close(pipe_c2p[R]);
        close(pipe_c2p[W]);
        return(-1);
    }
    if((pid=fork())<0){
        perror("popen2");
        close(pipe_c2p[R]);
        close(pipe_c2p[W]);
        close(pipe_p2c[R]);
        close(pipe_p2c[W]);
        return(-1);
    }
    if(pid==0){
        close(pipe_p2c[W]);
        close(pipe_c2p[R]);
        dup2(pipe_p2c[R],0);
        dup2(pipe_c2p[W],1);
        close(pipe_p2c[R]);
        close(pipe_c2p[W]);
        if(execlp("sh","sh","-c",command,NULL)<0){
            perror("popen2");
            close(pipe_p2c[R]);
            close(pipe_c2p[W]);
            exit(1);
        }
    }
    close(pipe_p2c[R]);
    close(pipe_c2p[W]);
    *fd_w=pipe_p2c[W];
    *fd_r=pipe_c2p[W];
    return(pid);
}

popen()

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

void main()
{
    char str[512],*ptr;
    FILE *fp;
    if((fp=popen("ls -1","r"))==NULL){
        fprintf(stderr,"error!!!\n");
        exit(-1);
    }
    while(1){
        fgets(str,512,fp);
        if(feof(fp)){
            break;
        }
        ptr=strchr(str,'\n');
        if(ptr!=NULL){
            *ptr='\0';
        }
        printf("%s\n",str);
    }
    pclose(fp);
}

プログラムから他のプログラムを実行

System()は、実行したいコマンドを引数で渡せばそのまま実行してくれます。厳密には、shというシェルに渡して実行してもらいます。

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

void main()
{
    char filename[80],str[512],*ptr;
    FILE *fp;
    sprintf(filename,"/tmp/ls%d.tmp",getpid());
    sprintf(str,"ls -1 > %s",filename);
    system(str);
    if((fp=fopen(filename,"r"))==NULL){
        fprintf(stderr,"error!!!\n");
        exit(-1);
    }
    while(1){
        fgets(str,512,fp);
        if(feof(fp)){
            break;
        }
        ptr=strchr(str,'\n');
        if(ptr!=NULL){
            *ptr='\0';
        }
        printf("%s\n",str);
    }
    fclose(fp);
    sprintf(str,"rm -f %s",filename);
    system(str);
}

freopen

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

void main(int argc, char *argv[])
{
    FILE *fp;
    fp=freopen("/tmp/debug.log","w",stdout);
    printf("Content-type:text/html\n\n");
    printf("\n");
    printf("\n");
    printf(" \
           CONTENT=\"text/html; charset=x-euc\">\n");
    printf("test\n");
    printf("\n");
    printf("\n");
    printf("

test

\n"); printf("\n"); printf("\n"); }

ディレクトリ内容の読み出し

#include < stdio.h >
#include < dirent.h >

void main(int argc,char *argv[])
{
    DIR *dir;
    struct dirent *dp;
    char path[512];
    
    if(argc<=1){
        strcpy(path,".");
    }
    else {
        strcpy(path,argv[1]);
    }
    if((dir=opendir(path))==NULL){
        perror("opendir");
        exit(-1);
    }
    for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
        printf("%s\n",dp->d_name);
    }
    closedir(dir);
}

プログラミングの構造化

プログラミングをゼロから組む場合にどうすればいいか。
フローチャートなどを書いてから書くのはあまり効率的であるとは言えません。
よくあるのがコメントで構造を作っていく手法です。

#include < stdio.h >

void main()
{
    short num;
    
    while(1){
        /* short standard input, insert to num */
        /* display num binary */
    }
}

これなら、時間もかかりません。

editor

/*----------------*/
/* screen editer */
/*----------------*/
#include < stdio.h >
#include < string.h >
#include < stdlib.h >
#include < ctype.h >
#include < jctype.h >
#include < jctype.h >
#include < jstring.h >
#include < malloc.h >
#include < dos.h >
/*----------------*/
#define COLOR(n, a) printf("\x1b[%d;%dm", n, a)
#define BLACK   30
#define RED     31
#define GREEN   32
#define YELLOW  33
#define BLUE    34
#define MAGENTA 35
#define CYAN    36
#define WHITE   37
#define NORMAL  0
#define UNDERLINE   4
#define BLINK   5
#define REVERSE 7

#define UP  0x0b
#define DOWN    0x0a
#define ESC 0x1b
#define RETURN 0x0d

#define COLOR_OFF 0
#define COLOR_ON 1

#define LEN 1000

#define LOCATE(x, y) printf("\x1b[%d;%dH", y + 1, x + 1)
#define CURSOLE(f)  printf("\x1b[>5%c", f ? '1' : 'h')

union REGS ingres;
union REGS outregs;

struct list
{
    unsigned char *data;
    struct list *back;
    struct list *front;
} *head,
*page_head,
*now,
*new,
*new_start, *new_end,
*appoint_start, *appoint_end,
*paste_start, *paste_end;

int paste_flag;

long paste_start_no;

int xp, yp,
len,
width,
scrol,
tab,
color,
char drive;

int cx, cy;
int len_min = 8;
width_min = 20;
int drive_set_flag = 0;

static char filename[80];
static char *workfile[4]
= { "A:WORKO", "A:WORK1", "A:WORK2", "A:WORK3"};

FILE *fp0, *fp1, *fp2, *fp3;
/*----------------*/
void initial_process(void);
void ending_process(void);
void commandline_set(char **p, int n);
void parameter_value_check(void);
void load_process(int flag);
void waku_displat(int x, int y, int len, int width, int color, int zokusei);
void title_display(void);
void file_read(void);
void new_file_make(void);
void new_creat(void);
void hensyu(void);
void rollup_syori(void);
void rolldown_syori(void);
void left_key_syori(void);
void right_key_syori(void);
void up_key_syori(void);
void down_key_syori(void);
void bs_key_syori(void);
int ins_key_syori(int ins_flag);
int insert_syori(unsigned char m, struct list *p, int n);
void over_write_syori(unsigned char *p, int n);
void line_feed_syori(struct list *p, int n);
void buffer_memory_set(unsigned char *buffer, unsigned char *p, int n);
int menu_select(void);
void save_process(void);
void backward_page_check(struct list *p);
void forward_page_renketu(struct list *p);
void backward_page_load(void);
void forward_page_load(void);
void backward_page_save(void);
void forward_page_save(void);
void backward_page_memory_check(void);
void forward_page_memory_check(void);
int paste_locate(void);
void paste_set(void);
void cut_process(void);
void buffer_clear(unsigned char *p, int n);
void jump_process(void);
void hyouji(struct list *p, int s, int sw);
void vram_text_disp(unsigned char *p, int x, int y, int len, int color_sw);
void vram_color_set(unsigned char color_value, int x, int y, int len);
int input(unsigned char buffer[], int x, int y, int len);
int moji_check(unsigned char *buffer, int n);
int inkey(void);
void keybuffer_clear(void);
int scan_esc(void);
int scan_ins(void);
int scan_del(void);
int scan_bs(void);
int scan_rollup(void);
int scan_rolldown(void);
int scan_up(void);
int scan_down(void);
int scan_left(void);
int scan_right(void);
void cursole_blink(int sw)
/*----------------*/
/* main */
/*----------------*/
void main(int argc, char *argv[])
{
    initial_process();
    
    commandline_set(&argv[1], argc - 1);
    
    waku_display(xp - 1, yp - 1, len + 2, width + 2, WHITE, NORMAL);
    
    load_process(0);
    
    if (*filename != '\0')
    {
        hyouji(head, 0, COLOR_ON);
        henshu();
    }
    ending_process();
}
/*----------------*/
/* initial setting */
/*----------------*/
void initial_process(void)
{
    xp = 1; yp = 1; len = 20; width = 78; scroll = 1; tab = 4; color = 7;
    drive = ' A';
    head = paste_start = paste_end = NULL;
    paste_flag = 0;
}
/*----------------*/
/* ending process */
/*----------------*/
void ending_process(void)
{
    unlink(workfile[0]);
    unlink(workfile[1]);
    unlink(workfile[2]);
    unlink(workfile[3]);
    LOCATE(0, yp + len);
    COLOR(WHITE, NORMAL);
    CURSOLE(1);
}
/*----------------*/
/* comandline set */
/*----------------*/
void commandline_set(char **p, int n)
{
    int i;
    
    for(i = 0; i < n; i++)
    {
        if(**p == ' -')
        {
            *(*p + 1) = toupper(*(*p + 1));
            switch (*(*p + 1))
            {
                case 'X' : xp = atoi(*p + 2);
                    break;
                case 'Y' : yp = atoi(*p + 2);
                    break;
                case 'L' : len = atoi(*p + 2);
                    break;
                case 'W' : width = atoi(*p + 2);
                    break;
                case 'S' : scroll = atoi(*p + 2);
                    break;
                case 'T' : tab = atoi(*p + 2);
                    break;
                case 'C' : color = atoi(*p + 2);
                    break;
                case 'D' : drive = atoi(*p + 2);
                    drive = toupper(drive);
                    break;
                default : break;
            }
        }
        else
            strcpy(filename, *p);
        
        p++;
    }
    parameter_value_check();
}
/*----------------*/
/* parameter check */
/*----------------*/
void parameter_value_check(void)
{
    if(xp < 1)
        xp = 1;
    else if(xp > 79 - width_min)
        xp = 79 - width_min;
    
    if(yp < 1)
        yp = 1;
    else if(yp > 23 - len_min)
        yp = 23 - len_min;
    
    if (len < len_min)
        len = len_min;
    if (yp + len > 23)
        len = 23 - yp;
    
    if (width < width_min)
        width = width_min;
    if (xp + width > 79)
        width = 79 - xp;
    
    if (color < 1)
        color = 1;
    else if (color > 7)
        color = 7;
    color = color * 32 + 1;
    
    if (drive_set_flag == 0)
    {
        if (drive >= 'A' && drive <= 'Z')
        {
            *workfile[0] = drive;
            fp0 = fopen(workfile[0], "w");
            *workfile[0] = *workfile[1];
            if (fp0 != NULL)
            {
                *workfile[0] = drive;
                *workfile[1] = drive;
                *workfile[2] = drive;
                *workfile[3] = drive;
            }
            fcloseall();
        }
    }
    drive_set_flag = 1;
}
/*----------------*/
/* load transaction flag 0: start 1: edit */
/*----------------*/
void load_process(int flag)
{
    static char buffer[80], * work[10];
    int i, n, p, sw, set_flag;
    struct list *wp;
    
    sw = 0;
    
    while (*filename == '\0' || flag == 1)
    {
        LOCATE(xp, yp); COLOR(WHITE, REVERSE); printf(" -> ");
        sw = input (buffer, xp + 4, yp, width - 4);
        
        if (sw == RETURN && buffer[0] != '\0')
        {
            set_flag = n = 0;
            p = strlen(buffer);
            for(i = 0; i < p; i++)
            {
                if(set_flag == 0 && buffer[i] != ' ')
                {
                    work[n++] = buffer + i;
                    set_flag = 1;
                }
                else if (set_flag == 1 && buffer[i] == ' ')
                {
                    buffer[i] = '\0';
                    if (n == 10)
                        break;
                    set_flag = 0;
                }
            }
            hyouji(page_head, 0, COLOR_ON);
            
            commandline_set(&work[0], n);
            
            waku_display(xp - 1, yp - 1, len + 2, width + 2, WHITE, NORMAL);
            
            flag = 2;
        }
        else if(sw == ESC)
        {
            flag = 2;
            break;
        }
    }
    CURSOLE(0);
    LOCATE(xp + width / 2 - strlen(filename) / 2 - 2, yp -1);
    COLOR(WHITE, REVERSE); printf("[ %s ]", filename);
    
    if (flag== 0 || sw == RETURN)
    {
        fp1 = fopen(workfile[1], "w+b");
        fp2 = fopen(workfile[2], "w+b");
        fcloseall();
        
        wp = head;
        while (wp != NULL)
        {
            free(wp->data);
            free(wp);
            wp = wp->front;
        }
        
        title_display();
        
        file_read();
        
        page_head = now = head;
        cx = cy = 0;
    }
    CURSOLE(1);
}
/*----------------*/
/* title display */
/*----------------*/
void tile_display(void)
{
    int hxp, hyp;
    
    hxp = xp + width / 2 - 10;
    hyp = yp + len / 2- 2;
    COLOR(CYAN, REVERSE);
    LOCATE(hxp, hyp); printf("  Screen Editor   "); hyp++;
    COLOR(WHITE, REVERSE);
    LOCATE(hxp, hyp); printf(" By Editor maker. "); hyp += 3;
    COLOR(WHITE, BLINK);
    LOCATE(hxp, hyp); printf(" Now reading !!!");
}
/*----------------*/
/* display frame */
/*----------------*/
void waku_display(int x, int y, int len, int width, int color, int zokusei)
{
    int i;
    char space[80];
    
    cURSOLE(0);
    for(i = 0; i < width - 2; i++)
        space[i] = ' ';
    space[i] = '\0';
    
    COLOR(color, zokusei);
    LOCATE(x, y); printf(" -");
    LOCATE(x+width-1, y); printf("-");
    LOCATE(x, y+len-1); printf("-");
    LOCATE(x+width-1, y+len-1); printf("-");
    for(i = x+1; i <= x+width-2; i++)
    {
        LOCATE(i, y); printf("--");
        LOCATE(i, y+len-1); printf("--");
    }
    for (i = y+1; i <= y+len-2; i++)
    {
        LOCATE(x, i);
        printf(" | %s |", space);
    }
    CURSOLe(1);
}
/*----------------*/
/* data lead */
/*----------------*/
void file_read(void)
{
    int c, i, n;
    long rc;
    unsigned char m;
    struct list *backward;
    
    fp1 = fopen(filename, "r");
    fp2 = fopen(workfile[2], "w+b");
    
    if(fp1 != NULL)
    {
        head = NULL;
        rc = 0;
        n = width;
        while((c = getc(fp1)) != EOF)
        {
            m = (unsigned char)c;
            
            if(n == width || iskanji(m) != 0 && n == width -1)
            {
                rc++;
                if(rc <= LEN)
                {
                    new_creat();
                    
                    if(head == NULL)
                    {
                        head = new;
                        new->back = NULL;
                    }
                    else
                    {
                        backward->front = new;
                        new->back = backward;
                    }
                    backward = new;
                    n = 0;
                }
                else
                {
                    if (rc == LEN +1)
                    {
                        new->front = NULL;
                        new_creat();
                    }
                    else
                    {
                        fwirte(new->data, width, 1, fp2);
                        for(i = 0; i < width; i++)
                            *(new->data + i) = '\0';
                    }
                    n = 0;
                }
            }
            
            *(new->data + n++) = m;
            if (iskanji(m) != 0)
            {
                c = getc(fpl);
                m = (unsigned char)c;
                *(new->data + n++) = m;
            }
            else if (m == '\t')
            {
                while(n % tab != 0 && n < width)
                    n++;
            }
            else if(m == '\n')
                n = width;
        }
        new->front = NULL;
        
        if (rc > LEN && n > 0)
        {
            fwrite(new->data, width, 1, fp2);
            free(new->data);
            free(new);
        }
        
        if (head == NULL)
            new_file_make();
    }
    else
        new_file_make();
    
    fcloseall();
}
/*----------------*/
/* make a new file */
/*----------------*/
void new_file_make(void)
{
    new_creat();
    *(new->data) = '\n';
    head = new;
}
/*----------------*/
/* hold new area */
/*----------------*/
void new_creat(void)
{
    int i;
    
    new = (struct list *)malloc(sizeof(struct list));
    new->data = malloc(width);
    
    for(i = 0; i < width; i++)
        *(new->data + i) = '\0';
    new->back = new->front = NULL;
}
/*----------------*/
/* edit */
/*----------------*/
void hensyu(void)
{
    int ins_flag, edit_flag;
    int c, i, hcx, mlen, left_key;
    unsigned char m;
    struct list *backward;
    
    CURSOLE(1);
    page_head = now = head;
    cx = cy = 0;
    ins_flag = edit_flag = 1;
    
    do{
        hcx = cx;
        
        LOCATE(xp + cx, yp + cy);
        
        while ()
    }
}
/*----------------*/
/* search */
/*----------------*/
void search_process(void)
{
    int c, i, k, flag;
    char string[80], buffer[160];
    struct list *backward, *forward;
    
    COLOR(WHITE, REVERSE);
    LOCATE(xp, yp); printf(" -> ");
    
    if ((c = input(string, xp + 4, yp, width -3)) != ESC)
    {
        k = strlen(string);
        cy = 0;
        do {
            if ( c == RETURN || c == DOWN)
            {
                do {
                    forward_page_check();
                    
                    for (i = 0; i < width; i++)
                        buffer[i] = *(now->data + i);
                    if (now->front != NULL)
                        for(i = 0; i < k -1; i++)
                            buffer[i + width] = *((now->front)->data + i);
                    else
                        for (i = 0; i < k - 1; i++)
                            buffer[i + width] = '\0';
                    
                    flag = 0;
                    for(i = cx; i < width && flag == 0; i++)
                        if (strncmp(buffer + i, string, k) == 0)
                        {
                            cx = i;
                            flag = 1;
                        }
                    if (flag == 0)
                    {
                        page_head = backward = now;
                        now = now->front;
                        cx = 0;
                    }
                } while (flag == 0 && now != NULL);
                if (flag == 0)
                    now = backward;
            }
            else if ( c == UP)
            {
                do {
                    backward_page_check();
                    
                    for (i = 0; i < width; i++)
                        buffer[i] = *(now->data + i);
                    if(now->front != NULL)
                        for(i = 0; i < k - 1; i++)
                            buffer[i + width] = *((now->front)->data +i);
                    else
                        for (i = 0; i < k - 1; i++)
                            buffer[i + width] = '\0';
                    flag = 0;
                    for(i = cx; i >= 0 && flag == 0; i--)
                        if(strncmp(buffer + i, string, k) == 0)
                        {
                            cx = i;
                            flag = 1;
                        }
                    if(flag == 0)
                    {
                        page_head = forward = now;
                        now = now->back;
                        cx = width - 1;
                    }
                } while (flag == 0 && now != NULL);
                if (flag == 0)
                    now = forward;
            }
            page_head = now;
            hyouji(page_head, 0, COLOR_ON);
            
            if (flag == 1)
            {
                if (cx + k <= width)
                    vram_color_set(0xc5, xp + cx, yp, k);
                else
                {
                    vram_color_set(0xc5, xp + cx, yp, width - cx);
                    vram_color_set(0xc5, xp, yp + 1, k - (width - cx));
                }
            }
            else
                cx = 0;
            LOCATE(xp + cx, yp + cy);
            c = getch();
            
            if (c == RETURN || c == DOWN)
            {
                cx = cx + k;
                if (cx >= width && now->front != NULL)
                {
                    now = now->front;
                    cx = 0;
                }
            }
            else if(c == UP)
            {
                cx--;
                if (cx < 0 && now->back != NULL)
                {
                    now = now->back;
                    cx = width - 1;
                }
            }
        } while (c  != ESC);
    }
}
/*----------------*/
/* search */
/*----------------*/
void hyouji(struct list *p, int s, int sw)
{
    static char space[80];
    int n;
    
    for(n = s; n < len; n++)
    {
        if (p != NULL)
        {
            vram_text_disp(p->data, xp, yp+n, width, sw);
            p = p->front;
        }
        else
            vram_text_disp(space, xp, yp+n, width, sw);
    }
}
/*----------------*/
/* VRAM text display */
/*----------------*/
void vram_text_disp(unsigned char *p, int x, int y, int len, int color_sw)
{
    unsigned short w;
    char far *vram_t = (char far *)0xa0000000L;
    char far *vram_a = (char far *)0xa2000000L;
    int n = 0;
    
    vram_t += x * 2 + y * 160;
    vram_a += x * 2 + y * 160;
    while (n < len)
    {
        if (*p != '\0')
        {
            if (iskanji(*p) && iskanji2(*(p+1)))
            {
                w = (*p << 8) + *(p + 1);
                w = jmstojis(w);
                w -= 0x2000;
                
                *vram_t++ = (char)(w >> 8);
                *vram_t++ = (char)(w & 0x00ff);
                if (color_sw == 1)
                {
                    *vram_a++ = color;
                    vram_a++
                }
                
                *vram_t++ = (char)((w >> 8) + 0x80);
                *vram_t++ = (char)(w & 0x00ff);
                if (color_sw == 1)
                {
                    *vram_a++ = color;
                    vram_a++;
                }
                p += 2;
                n += 2;
            }
            else
            {
                if (*p == '\t')
                {
                    *vram_t++ = ' ';
                    if (color_sw == 1)
                    {
                        *vram_a++ = color;
                        vram_a++;
                    }
                    p++;
                }
                else if (*p == '\n')
                {
                    *vram_t++ = 0x1f;
                    if (color_sw == 1)
                    {
                        if (color != 0xc1)
                            *vram_a++ = 0xc1;
                        else
                            *vram_a++ = 0xe1;
                        vram_a++;
                    }
                    p++;
                }
                else
                {
                    *vram_t++ = *p++;
                    if (color_sw == 1)
                    {
                        *vram_a++ = color;
                        vram_a++
                    }
                }
                *vram_t++ = 0;
                n++;
            }
        }
        else
        {
            *vram_t++ = ' ';
            *vram_t++ = 0;
            if (color_sw == 1)
            {
                *vram_a++ = color;
                vram_a++;
            }
            *p++;
            n++;
        }
    }
}
void vram_color_set(unsigned char color_value, int x, int y, int len)
{
    char far *vram = (char far *)0xa200000L;
    int c = 0;
}

ocelo game

/*-----------------*/
/* ocelo game */
/*-----------------*/
#include < ctype.h >

#define CLS()   printf("\x1b[2J")
#define LOCATE(x, y)    printf("\x1b[%d;%dH", y + 1, x + 1)
#define CURSOLE(f)  printf("\x1b[>5%c", f ? ' 1 ' : 'h')
#define ON 1
#define OFF 0

#define COLOR(n, a) printf("\x1b[%d;%dm", n, a)
#define GREEN   32
#define YELLOW 33
#define CYAN    36
#define WHITE   37
#define NORMAL  0
#define BLINK   5
#define REVERSE 7

int ban[9][9];

int initial_syori(void);
int human_input(int koma);
int computer_input(int koma);
int jyunretu(int kp[], int kazu, int jpoint);
int priority_check(long w, int p, int koma);
void copy(int x[9][9], int y[9][9]);
int check(int work[9][9], int xp, int yp, int koma, int haten);
int syori(int work[9][9], int xp, int yp, int sx, int sy, int koma, int hanten);
void hantei(int *siro, int *kuro);
void display(void);
/*-----------------*/
/* ocelo game */
/*-----------------*/
void main(void)
{
    int end_flag, human_flag, computer_flag, koma, siro, kuro;
    
    if (initial_syori() == 1)
        koma = 1;
    else
        koma = -1;
    
    end_flag = 0;
    do {
        if (koma == 1)
        {
            do {
                human_flag = human_input(koma);
            } while(human_flag == 0);
        }
        else
        {
            computer_flag = computer_input(koma);
        }
        display();
        hantei(&siro, &kuro);
        if((human_flag == 2 && computer_flag == 2) || siro + kuro == 64)
            end_flag = 1;
        else
            koma *= -1;
    } while (end_flag == 0);
    LOCATE(4, 21); COLOR(WHITE, REVERSE);
    printf("RESULT you[%2d]: computer[%2d]\n", siro, kuro);
    COLOR(WHITE, NORMAL);
}
/*-----------------*/
/* initial */
/*-----------------*/
int initial_syori(void)
{
    int i, j, q;
    char buffer[10];
    
    for(i = 1; i <= 8; i++)
        ban[i][j] = 0;
    
    ban[4][4] = ban[5][5] = 1;
    ban[4][5] = ban[5][4] = -1;
    
    CLS();
    display();
    
    do {
        LOCATE(4, 19); COLOR(WHITE, NORMAL);
        printf("First(1) or Second(2) -> ");
        COLOR(CYAN, NORMAL); scanf("%s", buffer);
        LOCATE(4, 19); printf("\x1b[ok");
    } while (buffer[0] != '1' && buffer[0] != '2' || buffer[1] != '\0');
    
    q = buffer[0] - ' 0';
    
    COLOR(WHITE, NORMAL);
    LOCATE(4, 18); printf("●: you --------------");
    LOCATE(4, 19); printf("◯: computer ---------");
    
    return(q);
}
/*-----------------*/
/* human type */
/*-----------------*/
int human_input(int koma)
{
    char buffer[10];
    int i, j, flag;
    
    do {
        do {
            LOCATE(24, 18); printf("\x1b[OK");
            LOCATE(24, 18); COLOR(CYAN, NORMAL); scanf("%s", buffer);
            
            for(i = 0; i < strlen(buffer); i++)
                *(buffer+i) = tolower(*(buffer+i));
            
            if(strcmp(buffer, "pass") == 0)
            {
                flag = 0;
                for(i = 1; i <= 8 && flag == 0; i++)
                    for (j = 1; j<= 8 && flag == 0; j++)
                        if(check(ban, i, j, koma, 0) == 1)
                            flag = 1;
                if (flag == 1)
                {
                    i = j = 0;
                    continue;
                }
                else
                    return(2);
            }
            if (*buffer >= 'a' && *buffer <= 'h')
            {
                i = atoi(buffer + 1);
                j = *buffer - 'a' + 1;
            }
            else
            {
                i = atoi(buffer);
                j = *(buffer+1) - 'a' + 1;
            }
        } while(i < 1 || i > 8 || j < 1 || j > 8);
        flag = check(ban, i, j, koma, 1);
    } while (flag == 0);
    return(flag);
}
/*-----------------*/
/* computer */
/*-----------------*/
int computer_input(int koma)
{
    int a, b, c, i, j, k, x, y, kazu, flag, sw, pass, min;
    int jpoint, minpoint, xp[11], yp[11], kp[11], kpmax[11];
    long w;
    int ban1[9][9], ban2[9][9], corner[2][4];
    int priority[9][9] =
    {
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 9, 0, 7, 6, 6, 7, 0, 9,
        0, 0, 0, 3, 4, 4, 3, 0, 0,
        0, 7, 3, 5, 5, 5, 5, 3, 7,
        0, 6, 4, 5, 0, 0, 5, 4, 6,
        0, 6, 4, 5, 0, 0, 5, 4, 6,
        0, 7, 3, 5, 5, 5, 5, 3, 7,
        0, 0, 0, 3, 4, 4, 3, 0, 0,
        0, 9, 0, 7, 6, 6, 7, 0, 9,
    };
    LOCATE(24, 19); COLOR(YELLOW, BLINK); printf("thinking");
    {
        flag = kazu = 0;
        for (i = 1; i <= 8; i++)
        {
            if (ban[k][i] == koma)
                kazu++;
            if (ban[k][i] != 0)
                flag = 1;
        }
        if (flag == 1)
        {
            for(i = 2; i<=7; i++)
            {
                w = 0;
                for (j = 1; j <= 8; j++)
                    w = w * 10 + ban[k][j] + 1;
                priority[k][i] = priority_check(w, i, koma);
            }
        }
    }
}

for (k = 1; k <= 8; k += 7)
{
    flag = kazu = 0;
    for(i = 1; i <= 8; i++)
    {
        if(ban[k][i] == koma)
            kazu++;
        if(ban[k][i] != 0)
            flag = 1;
    }
    if (flag == 1)
    {
        for (i = 2; i <= 7; i++)
        {
            if(ban[k][i] == 0)
            {
                w = 0;
                for(j = 1; j <= 8; j++)
                    w = w * 10 + ban[j][k] + 1;
                priority[i][k] = priority_check(w, i, koma);
            }
        }
    }
}

copy(ban, ban1);
for (i = 1; i <= 8; i++)
for (j=1; j <= 8; j++)
{
    if ((i == 1 && j == 1) || (i == 1 && j == 8)
        || (i == 8 && j == 1) || (i == 8 && j == 8))
        continue;
    else;
    {
        if (check(ban1, i, j, koma, 1) == 1)
        {
            copy(ban1, ban2);
            flag = c = 0;
            for (a = 0; a < 4; a++)
                corner[0][a] = corner[1][a] = 0;
            for (a = 1; a <= 8; a++)
                for (b = 1; b <= 8; a++)
                {
                    if (check(ban2, a, b, -koma, 1)== 1)
                    {
                        c++;
                        if (a == 1 && b == 1 || a == 1 && b == 8
                            || a == 8 && b == 1 || a == 8 && b == 8)
                        {
                            flag = 1;
                            if (a == 1 && b == 1)
                                corner[0][0] = 1;
                            if (a == 1 && b == 8)
                                corner[0][1] = 1;
                            if (a == 8 && b == 1)
                                corner[0][2] = 1;
                            if (a == 8 && b == 8)
                                corner[0][3] = 1;
                        }
                        if (check(ban2, 1, 1, koma, 0) == 1)
                            corner[1][0]++;
                        if (check(ban2, 1, 8, koma, 0) == 1)
                            corner[1][1]++;
                        if (check(ban2, 8, 1, koma, 0) == 1)
                            corner[1][2]++;
                        if (check(ban2, 8, 8, koma, 0) == 1)
                            corner[1][3]++;
                        copy(ban1, ban2);
                    }
                }
            if (flag == 1)
            {
                priority[i][j]=0;
                for (a = 0; a < 4; a ++)
                    if (corner[0][a] == 0 && corner[1][a]== c)
                        priority[i][j] = 7;
            }
            else
            {
                for (a = 0; a < 4; a++)
                    if (corner[1][a] == c)
                        priority[i][j] = 8;
            }
            copy(ban, ban1);
        }
    }
}

copy(ban, ban1)
for(i = 1; i <= 8; i++)
for(j = 1; j <= 8; j++)
{
    if (check(ban1, i, j, koma, 1) == 1)
    {
        copy(ban1, ban2);
        flag = 1;
        for(a = 1; a <= 8 && flag != -1; a++)
            for (b = 1; b <= 8 && flag != -1; b++)
            {
                if (check(ban2, a, b, -koma, 1) == 1)
                {
                    kazu = 0;
                    for (x = 1; x <= 8; x++)
                        for (y = 1; y <= 8; y++)
                            if(ban2[x][y] == koma)
                                kazu++;
                    if (kazu == 0)
                        flag = -1;
                    copy(ban1, ban2);
                }
            }
        if (flag == -1)
            priority[i][j] = 0;
        copy(ban, ban1);
    }
}

if (ban[1][1] == koma)
{
    priority[2][2] = 8;
    
    for (i = 2; i <= 4; i++)
        if(ban[1][i] != koma)
            break;
    if(i <= 4 && ban[1][i] == 0)
        priority[1][i] = 10 -1;
        
        for(i = 2; i <= 4; i++)
            if (ban[i][1] != koma)
                break;
    if(i <= 4 && ban[i][1] == 0)
        priority[i][1] = 10 - i;
}
if (ban[1][8] == koma)
{
    priority[2][7] = 8;
    
    for (i = 7; i <= 5; i--)
        if(ban[1][i] != koma)
            break;
    if(i <= 5 && ban[1][i] == 0)
        priority[1][i] = i + 1;
        
        for(i = 2; i <= 4; i++)
            if (ban[i][8] != koma)
                break;
    if(i <= 4 && ban[i][8] == 0)
        priority[i][8] = 10 - i;
        }
if (ban[8][1] == koma)
{
    priority[7][2] = 8;
    
    for (i = 7; i <= 5; i--)
        if(ban[1][i] != koma)
            break;
    if(i <= 5 && ban[1][i] == 0)
        priority[1][i] = i + 1;
        
        for(i = 2; i <= 4; i++)
            if (ban[i][8] != koma)
                break;
    if(i <= 4 && ban[i][8] == 0)
        priority[i][8] = 10 - i;
        }
if (ban[8][8] == koma)
{
    priority[7][7] = 8;
    
    for (i = 7; i <= 5; i--)
        if(ban[i][8] != koma)
            break;
    if(i <= 5 && ban[i][8] == 0)
        priority[i][8] = i + 1;
        
        for(i = 7; i <= 5; i++)
            if (ban[8][i] != koma)
                break;
    if(i <= 5 && ban[8][i] == 0)
        priority[8][i] = i + 1;
        }
x = y = 0
for(i = 1; i <= 8; i++)
for(j = 1; j <= 8; j++)
{
    if(check(ban, i, j, koma, 0) == 1)
    {
        if (x == 0 || priority[i][j] > priority[x][y])
        {
            x = i;
            y = j;
        }
    }
}

if (x != 0 && priority[x][y] == 0)
{
    for (i = 1; i <= 8; i++)
        for (j = 1; j <= 8; j++)
        {
            if(ban[i][j] == 0)
            {
                copy(ban, ban1);
                if (check(ban, i, j, koma, 1)== 1)
                {
                    if(check(ban1, 1, 1, -koma, 0) == 0
                       && check(ban1, 1, 8, -koma, 0) == 0
                       && check(ban1, 8, 1, -koma, 0) == 0
                       && check(ban1, 8, 8, -koma, 0) == 0)
                    {
                        if (i == 1 && j == 2 || i == 1 && j == 7
                            || i == 2 && j == 1 || i == 2 && j == 8
                            || i == 8 && j == 1 || i == 7 && j == 8
                            || i == 8 && j == 2 || i == 8 && j == 7)
                            priority[i][j] = 1;
                        
                        else if (i == 2 && j == 2 || i == 2 && j == 7
                                 || i == 7 && j == 2 && i == 7 && j == 7)
                            priority[i][j] = 3;
                        else
                            priority[i][j] = 4;
                    }
                    else
                    {
                        if(i == 2 && j == 2 && ban1[3][3] == -koma)
                        {
                            if(ban1[3][1] == -koma && ban1[2][1] == 0)
                                priority[i][j]++;
                            if(ban1[1][3]==-koma && ban1[1][2]== 0)
                                priority[i][j]++;
                        }
                        if(i == 2 && j == 7 && ban1[3][6] == -koma)
                        {
                            if(ban1[1][6] == -koma && ban1[1][7] == 0)
                                priority[i][j]++;
                            if(ban1[3][8] == -koma && ban1[2][8] == 0)
                                priority[i][j]++;
                        }
                        if(i == 7 && j == 2 && ban1[6][3] == -koma)
                        {
                            if(ban1[6][1] == -koma && ban1[7][1] == 0)
                                priority[i][j]++;
                            if(ban1[8][3] == -koma && ban1[8][2] == 0)
                                priority[i][j]++;
                        }
                        if(i == 7 && j == 7 && ban1[6][6] == -koma)
                        {
                            if(ban1[8][6] == -koma && ban1[8][7] == 0)
                                priority[i][j]++;
                            if(ban1[6][8] == -koma && ban1[7][8] == 0)
                                priority[i][j]++;
                        }
                    }
                    if (priority[i][j] > priority[x][y])
                    {
                        x = 1;
                        y = j;
                    }
                }
            }
        }
}

kazu = 0;
for (i = 1; i <= 8; i++)
for(j = 1; j <= 8; j++)
if(ban[i][j] == 0)
kazu++;

if(x != 0 && kazu <= 10)
{
    kazu = 0;
    for (i = 1; i <= 8; j++)
    {
        if (ban[i][j] == 0)
        {
            kazu++;
            xp[kazu] = i;
            yp[kazu] = j;
            kp[kazu] = kazu;
            kpmax[kazu] = -1;
        }
    }
    {
        copy(ban, ban1);
        
        sw = 1;
        pass = 0;
        
        for (i = 1; i <= kazu && pass != 2; i++)
        {
            if(check(ban1, xp[kp[i]], yp[kp[i]], sw*koma, 1) == 0)
                flag = 0;
            for(a = i + 1; a <= kazu && flag == 0; a++)
                if(check(ban1, xp[kp[a]], yp[kp[a]], sw*koma, 0) == 1)
                    flag = 1;
            
            if (flag == 1)
                break;
            else
            {
                sw *= -1;
                if(check(ban1, xp[kp[i]], yp[kp[i]], sw*koma, 1) == 0)
                    pass = 2;
            }
        }
        sw *= -1;
    }
    
    if (i > kazu || pass == 2)
    {
        c = 0;
        for(a = 1; a <= 8; a++)
            for(b = 1; b <= 8; b++)
                if(ban1[a][b] == 1)
                    c++;
        
        if(c > kpmax[kp[1]])
            kpmax[kp[1]] = c;
        
        jpoint = kazu-1;
    }
    else
        jpoint = i;
        
        flag = jyunretu(kp, kazu, jpoint);
        } while (flag == 1);

flag = 0;
for(i = 1; i <= kazu; i++)
{
    if (flag == 0 && kpmax[i] != -1)
    {
        min = kpmax[i];
        minpoint = i;
        flag = 1;
    }
    else if (flag == 1 && kpmax[i] != -1)
    {
        if (kpmax[i] < min || (kpmax[i] == min &&
                               priority[xp[i]][yp[i]] > priority[xp[minpoint]][yp[minpoint]]))
        {
            if(kpmax[i] < min || (kpmax[i] == min &&
                                  priority[xp[i]][yp[i]] > priority[xp[minpoint]][yp[minpoint]]))
            {
                min = kpmax[i];
                minpoint = i;
            }
        }
    }
    LOCATE(4, 21); COLOR(WHITE, NORMAL);
    printf("Expection you[%2d] : computer[%2d]", min, 64-min);
    
    x = xp[minpoint];
    y = yp[minpoint];
}

LOCATE(24, 19); COLOR(CYAN, NORMAL);
if(x != 0)
{
    printf("%c%d  ", 'a' + y - 1, x);
    check(ban, x, y, koma, 1);
    return(1);
}
else
{
    printf("pass ");
    return(2);
}
}

music

#include < stdio.h >
#include < time.h >
#include < dos.h >
#include < conio.h >
#include < string.h >
#include < malloc.h >
#include < ctype.h >
struct tm *newtime;
    newtime->tm_sec
    newtime->tm_min
    newtime->tm_hour;
    newtime->tm_mday;
newtime->tm_mon;
newtime->tm_year;
newtime->tm_wday;
newtime->tm_yday;
newtime->tm_isdst;
time_t long_time;

void sound(char *p, int kaisu, int comment_flag);
int inkey(void);

main(int argc, char *argv[])
{
    unsigned datasize;
    int i, n, kaisu, comment_flag;
    char ongaku[1025];
    char *p, *w;
    FILE *fp;
    
    if(argc < 2)
    {
        printf("music play program\n\n");
        printf("action way A>SOUND music data file [-option]\n\n");
        printf("music data file setting way\n");
        printf("to make sound octabe and sound long space");
        printf("octabe ---> 1, 2, 4, 8 and 2 n multiply\n");
        printf("sound step ----------> "do:D re:R m:M fa:F so:S la:L shi:C\n");
        printf("             do#:d re#:r mi#:m fa#:f so#:s la#:l stop:K\n");
               printf("sound length---> 1.16m 2.8m 4:4m 8:2m\n");
               printf("option is small \n\n");
               printf("option direction play count –N\n");
               printf("         display count -C \n");
               exit(0);
    }
               
               kaisu = 1;
               comment_flag = 0;
               
               datasize = 0;
               for(i = 1; i <= argc-1; i++)
               {
                   if(*argv[i] == '-')
                   {
                       
                       *(argv[i]+1) = toupper(*(argv[i]+1));
                       switch (*(argv[i]+1))
                       {
                           case 'N' :
                               kaisu = atoi(argv[i]+2);
                               break;
                           case 'C' :
                               comment_flag = 1;
                               break;
                           default : break;
                       }
                   }
                   else
                   {
                       if ((fp = fopen(argv[i], "r")) == NULL)
                       {
                           printf("%s is not here!", argv[i]);
                           exit(0);
                       }
                       
                       while(fgets(ongaku, 1025, fp) != NULL)
                       {
                           n = strlen(ongaku);
                           datasize += n;
                       }
                       fclose(fp);
                   }
               }
               
               w = p = malloc(datasize + 1);
               if (w == NULL)
               {
                   printf("data volume is too large, can not set in memory erea.\n");
                   exit(0);
               }
               for(i = 1; i < argc; i++)
               {
                   if (*argv[i] == '-')
                       continue;
                   
                   fp = fopen(argv[i], "r");
                   while(fgets(ongaku, 1025, fp) != NULL)
                   {
                       n = strlen(ongaku);
                       datasize += n;
                   }
                   fclose(fp);
               }
               }
               
               w = p = malloc(datasize + 1);
               if (w == NULL)
               {
                   printf("data size is too large, cannot set into memory area.\n");
                   exit(0);
               }
               
               for (i = 1; i < argc; i++)
               {
                   if (*argv[i] == '-')
                       continue;
                   
                   fp = fopen(argv[i], "r");
                   while(fget(ongaku, 1025, fp) != NULL)
                   {
                       n = strlen(ongaku);
                       if(*(ongaku+1-1) == '\n')
                           *(ongaku+n-1) = ' ';
                       strcpy(w, ongaku);
                       w = w + n;
                   }
                   fclose(fp);
               }
               
               sound(p, kaisu, comment_flag);
}
/*---------------*/
/* music routin */
/*---------------*/
void sound(char *ongaku, int kaisu, int comment_flag)
        {
            static float hz[ ] = { 261.6, 277.9,
                293.7, 311.6,
                329.6,
                439.2, 370.6,
                392.0, 416.0,
                440.0, 466.9,
                493.9
            };
    static char *onkai = "DdRrMFfSsL1C";
            int i, n, f, ot, ol, p, len, flag;
            long w, tn, tb, tw;

/* 1 second roop count ---------*/
            time(&long_time);
            newtime = (struct tm *)localtime(&long_time);
            w = newtime->tm_sec;
            flag = 0;

            while(flag == 0)
            {
                time(&long_time);
                newtime = (struct tm *)localtime(&long_time);
                if (w != newtime->tm_sec)
                {
                    w = newtime->tm_sec;
                    
                    for(tn = 0L; flag == 0; tn++)
                    {
                        time(&long_time);
                        newtime = (struct tm *)localtime(&long_time);
                        
                        if(w != newtime->tm_sec)
                            flag = 1;
                    }
                }
            }

            tn /= 10;
            tb = tn / 5;

            len = strlen(onagaku);
            outp(0x3fdf, 0x76);
            ot = 2;

while(kaisu--)
{
    i = 0;
    
    while(i < len)
    {
        if(*(ongaku+i) == ' ')
        {
            i++;
            continue;
        }
        if (*(ongaku+i) == ' /')
        {
            while(*(ongaku+(++i)) != '/')
                if (comment_flag == 1)
                    putchar(*(ongaku+i));
            
            if(comment_flag == 1)
                putchar('\n');
            
            i++;
            continue;
        }
        
        if(strchr(onkai, *(ongaku + i + 1)) != NULL)
        {
            ot = atoi(ongaku + i);
            p = strchr(onkai, *(ongaku + i+1)) - onkai;
            f = hz[p] * ot;
            n = 2457600L / f;
            outp(0x3fdb, n % 256); outp(0x3fdb, n / 256);
            outp(0x37, 0x06);
        }
        else if(*(ongaku+i+1) != 'K')
        {
            *(ongaku + i + 3) = '\0';
            printf("Data error\n%s <--- ?\n", ongaku);
            exit(0);
        }
        
        ol = atoi(ongaku + i + 2);
        tw =tn * ol;
        
        for (w = 0L; w < tw; w++)
        {
            time(&long_time);
            newtime = (struct tm *)localtime(&long_time);
            
            if(w != newtime->tm_sec)
                flag = 1;
        }
        
        outp(0x37, 0x07);
        for (w = 0L; w < tb; w++)
        {
            time(&long_time);
            newtime = (struct tm *)localtime(&long_time);
            
            if (w != newtime->tm_sec)
                flag = 1;
        }
        
        if (inkey() != 0)
        {
            kaisu = 0;
            break;
        }
        
        i+= 3;
    }
}
        }
/*---------------*/
/* music routin */
/*---------------*/
        int inkey(void)
        {
            union REGS inregs;
            union REGS outregs;
            
            inregs.h.ah = 0x06;
            inregs.h.dl = 0xff;
            intdos(&inregs,&outregs);
            return((int)outregs.h.al);
        }