#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h> // necesar pentru PATH_MAX
#define SECTOR_SIZE 4096
void reverse(char *sir)
{
int lg = strlen(sir) - 1;
for (int i = 0; i <= lg / 2; i++)
{
char aux = sir[i];
sir[i] = sir[lg - i];
sir[lg - i] = aux;
}
}
void afiseaza(char *map_addr, int length)
{
char aux[SECTOR_SIZE] = {0};
for (int i = length - 1; i >= 0; i--)
if (map_addr[i] != '\n')
{
int lg = strlen(aux);
aux[lg++] = map_addr[i];
aux[lg] = '\0';
}
else
{
if (strlen(aux) > 0)
{
reverse(aux);
printf("%s\n", aux);
}
aux[0] = '\0';
}
if (strlen(aux) > 0)
{
reverse(aux);
printf("%s\n", aux);
}
}
void recursive(int f, int length, int offset, int PageSize)
{
int ok = 0;
if (length > PageSize)
{
recursive(f, length - PageSize, offset + PageSize, PageSize);
ok = 1;
}
if (ok == 1)
length = PageSize;
char *map_addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, f, offset);
afiseaza(map_addr, length);
if (munmap(map_addr, length) == -1)
{
perror("Erroare la munmap.");
exit(4);
}
}
void scrieFisier(char *numeFisier)
{
int f = open(numeFisier, O_RDONLY);
if (f == -1)
{
perror("A aparut o eroare cand am incercat sa deschide fisierul");
exit(2);
}
long PageSize = sysconf(_SC_PAGE_SIZE);
struct stat sb;
if (PageSize == -1)
{
perror("Eroare la sysconf");
exit(1);
}
if (fstat(f, &sb) == -1)
{
perror("Eroare la fstat");
exit(2);
}
recursive(f, sb.st_size, 0, PageSize);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Introdu numele unui sau mai multor fisiere.");
exit(1);
}
for (int i = 1; i < argc; i++)
scrieFisier(argv[i]);
printf("\n");
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}