/*
    ・・・Q・・・・・・・・・・・Z・p・メ・・・ア    ・ス・ャ5・N・x・E・t・・・E・゚・・ ・・18・i・ン・・・Q・j
                                BohYoh Shibata PREPARATION
*/

#include <stdio.h>
#define  MAXLINE 256

int search(char *, char *);

main()
{
    int  n;
    char file[32], pattern[MAXLINE], buffer[MAXLINE];
    FILE *fp ;

    puts("File name ?");
    gets(file);

    puts("Pattern ?");
    gets(pattern);

    if ((fp = fopen(file, "r")) != NULL) {
        for (n = 1; fgets(buffer, MAXLINE, fp) != NULL;  n++ )
            if (search(buffer, pattern) >= 0)
                printf("%4d:%s", n, buffer);
        fclose(fp);
    } else
        fprintf(stderr, "%s: open error\n", file);
}

int search(char *b, char *p)
{
    int  n, s, t, sw;

    for (n = 0; b[n] != '\0'; n++) {
        for (s = n, t = 0, sw = 0; p[t] != '\0'; s++, t++) {
            if (p[t] == '?') {
                if (b[s] == '\n')
                    return -1;
            } else if (p[t] == '*') {
                sw = 1 ;
                s--;
            } else {
                if (sw == 1) {
                    for ( ;  p[t] != b[s]  && b[s] != '\0'; s++) ;
                    if (b[s] == '\0')
                        return -1;
                    sw = 0;
                } else if ( p[t] != b[s] )
                    break;
            }
        }
        if (p[t] == '\0')
            return n;
    }
    return -1;
}