-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.c
64 lines (59 loc) · 1.63 KB
/
proc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "proc.h"
void wc(char *file)
{
int lines = 0, chars = 0;
int d = open(file, O_RDONLY);
if (d < 0)
{
printf("Error on opening file: %s\nRunning with user: %d, group: %d\nPlease check if you have access to this file\n", file, getgid(), getuid());
perror("Błąd otwarcia pliku");
return;
}
struct stat *buffer = malloc(sizeof(struct stat));
stat(file, buffer);
// printf("%ld\n", buffer->st_size); // file size
char *ptr = mmap(NULL, buffer->st_size, PROT_READ, MAP_SHARED, d, 0);
free(buffer);
for (int i = 0; i < buffer->st_size; i++)
{
// printf("%c", (char)*(ptr + i));
// liczymy linie według definicji POSIX: linia to ciąg znaków zakończony znakiem nowej linii
if ((char)*(ptr + i) == '\n')
lines++;
if (!isspace((char)*(ptr + i)))
chars++;
}
// printf("procs: %ld, plik: %s znakow %d, linijek %d\n", pthread_self(), file, chars, lines);
close(d);
TWOINTS *tmp = malloc(sizeof(TWOINTS)); // odpowiednikiem byłby malloc(2*sizeof(int))
tmp->chars = chars;
tmp->lines = lines;
stack_push(&results, tmp);
}
void *proc(void *i)
{
// printf("procs\n");
while (1)
{
char *tmp = stack_pop(&paths);
if (tmp != NULL)
{
wc(tmp);
free(tmp);
}
else
break;
}
stack_endData(&results);
// printf("end proc\n");
return NULL;
}