Skip to content

Instantly share code, notes, and snippets.

@kokosabu kokosabu/B004.c
Created Jun 23, 2014

Embed
What would you like to do?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define M (500 + 1)
struct _logdata {
char *ip;
char *date;
char *filename;
};
typedef struct _logdata logdata;
#define START 0
#define END 1
int logcmp(const void *a, const void *b)
{
return strcmp(((logdata *)a)->date, ((logdata *)b)->date);
}
int main()
{
char input1[M];
char input2[M];
char ip_start[16];
char ip_end[16];
char octet3[2][4];
char octet4[2][4];
char *octet[4];
int int_ip_s[4];
int int_ip_e[4];
int int_ip_o[4];
int i;
int j;
int N;
logdata *logs;
logdata log;
char *token;
/* オクテットの分析 */
// オクテットを分解する
fgets(input1, M, stdin);
octet[0] = strtok(input1, ".\n");
octet[1] = strtok(NULL, ".\n");
octet[2] = strtok(NULL, ".\n");
octet[3] = strtok(NULL, ".\n");
// 3番目のオクテットを処理
if(strcmp(octet[2], "*") == 0) {
strcpy(octet3[START], "0");
strcpy(octet3[END], "255");
} else if(strchr(octet[2], '[') != NULL) {
sscanf(octet[2], "[%[0-9]-%[0-9]]", octet3[START], octet3[END]);
} else {
strcpy(octet3[START], octet[2]);
strcpy(octet3[END], octet[2]);
}
// 4番目のオクテットを処理
if(strcmp(octet[3], "*") == 0) {
strcpy(octet4[START], "0");
strcpy(octet4[END], "255");
} else if(strchr(octet[3], '[') != NULL) {
sscanf(octet[3], "[%[0-9]-%[0-9]]", octet4[START], octet4[END]);
} else {
strcpy(octet4[START], octet[3]);
strcpy(octet4[END], octet[3]);
}
sprintf(ip_start, "%s.%s.%s.%s", octet[0], octet[1], octet3[START], octet4[START]);
sprintf(ip_end, "%s.%s.%s.%s", octet[0], octet[1], octet3[END], octet4[END]);
sscanf(ip_start, "%d.%d.%d.%d", &int_ip_s[0], &int_ip_s[1], &int_ip_s[2], &int_ip_s[3]);
sscanf(ip_end, "%d.%d.%d.%d", &int_ip_e[0], &int_ip_e[1], &int_ip_e[2], &int_ip_e[3]);
fgets(input2, M, stdin);
sscanf(input2, "%d", &N);
logs = (logdata *)malloc(sizeof(logdata) * N);
j = 0;
for(i = 0; i < N; i++) {
fgets(input2, M, stdin);
token = strtok(input2, " ");
log.ip = (char *)malloc((sizeof(char) * strlen(token)) + 1);
strcpy(log.ip, token);
token = strtok(NULL, " "); // identユーザー名
token = strtok(NULL, " "); // 認証ユーザー名
token = strtok(NULL, " "); // [アクセス日時]
log.date = (char *)malloc((sizeof(char) * strlen(token)) - 2 + 1);
strncpy(log.date, token+1, strlen(token)-1);
token = strtok(NULL, " "); //
token = strtok(NULL, " "); // "リクエストヘッダ
token = strtok(NULL, " "); // ファイル名
log.filename = (char *)malloc((sizeof(char) * strlen(token)) + 1);
strncpy(log.filename, token, strlen(token));
sscanf(log.ip, "%d.%d.%d.%d", &int_ip_o[0], &int_ip_o[1], &int_ip_o[2], &int_ip_o[3]);
if(((int_ip_s[0] <= int_ip_o[0]) && (int_ip_o[0] <= int_ip_e[0])) &&
((int_ip_s[1] <= int_ip_o[1]) && (int_ip_o[1] <= int_ip_e[1])) &&
((int_ip_s[2] <= int_ip_o[2]) && (int_ip_o[2] <= int_ip_e[2])) &&
((int_ip_s[3] <= int_ip_o[3]) && (int_ip_o[3] <= int_ip_e[3]))) {
logs[j] = log;
j++;
}
}
qsort(logs, j, sizeof(logdata), logcmp);
for(i = 0; i < j; i++) {
printf("%s %s %s\n", logs[i].ip, logs[i].date, logs[i].filename);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.