Jak szybki jest grep? -> grep vs awk, python, rugby, java, perl, C - przeszukiwanie logów - część 5.





C - file
//
// Copyright (c) 2016 Rafal Jackiewicz
//
// Indent style: Berkeley
//
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#define Dnew_line 10
// shmid len shmdiff needle
int
main(int argc, char *argv[])
{
    int             fd;
    int             madret;
    int             shmid = 0;
    const int       needle_max = 6;
    unsigned long   len_max;
    unsigned long   shmdiff = 0;
    char           *fileinmemory;
    struct stat     fd_st;
    char            needle[needle_max];
    if (argc < 3) {
    fprintf(stderr, "Onlyfind_str_in: za mamlo danych.\n");
    return 1;
    }
    if (strlen(argv[3]) != needle_max) {
    fprintf(stderr, "%sZla dlugośc needle.\n", argv[3]);
    return 1;
    else {
    strcpy(needle, argv[3]);
    }
    shmid = atoi(argv[0]);
    len_max = strtol(argv[1], NULL, 16);
    shmdiff = strtol(argv[2], NULL, 16);
    fileinmemory = (char *) shmat(shmid, NULL, 0);
    if (*fileinmemory == -1) {
    perror("Onlyfind_str_in: shmat.");
    return 1;
    }
    fileinmemory += shmdiff;
    int             needle_max_minusone = needle_max - 1;
    char           *endfileinmemory =
    fileinmemory + len_max - needle_max + 1;
    char           *mark = fileinmemory;
    char           *copymarkstart,
                   *copymarkstop;
    char           *needle_maxminus = needle + needle_max_minusone;
    char           *needle_plus = needle + 1;
    unsigned long   needle_max_minustwo = needle_max - 2;
    unsigned long   copylen_max = len_max;
    while ((mark = memchr(mark, *needle, copylen_max))
       && (mark < endfileinmemory)) {
    if (mark[needle_max_minusone] == *needle_maxminus) {
        if (memcmp(++mark, needle_plus, needle_max_minustwo) == 0) {
        copymarkstart = mark;
        while (*(--copymarkstart) != (char) Dnew_line);
        copymarkstop = mark + needle_max;
        while (*(++copymarkstop) != (char) Dnew_line);
        mark = copymarkstop;
        printf("%-.*s", copymarkstop - copymarkstart,
               copymarkstart++);
        copylen_max = endfileinmemory - mark;
        else {
        --copylen_max;
        }
    else {
        ++mark;
        --copylen_max;
    }
    }
    return 44;


********

Więcej informacji:
Informatyka, FreeBSD, Debian


***

Inne wpisy:



Update: 2018.07.17
Create: 2018.07.17

Brak komentarzy:

Prześlij komentarz