-
Notifications
You must be signed in to change notification settings - Fork 0
/
structures.c
119 lines (93 loc) · 2.3 KB
/
structures.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include "structures.h"
void init(NODE** head) {
*head = NULL;
}
NODE* add(NODE* node, user toI) {
NODE* toInsert = (NODE*) malloc(sizeof (NODE));
toInsert->data = toI;
toInsert->next = NULL;
if (!toInsert) exit(0);
if (!node) node = toInsert;
else {
NODE* temp = node;
while (temp->next) temp = temp->next;
temp->next = toInsert;
}
return node;
}
NODE* removeN(NODE* head, int pid){
NODE* temp;
NODE* prev;
temp = head;
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
if (temp && temp->data.pid == pid) {
unlink(temp->data.namedPipe);
head = head->next;
free(temp);
temp = head;
return head;
}
puts("Nem chega aqui...");
while (temp != NULL && temp->data.pid != pid) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return head;
unlink(temp->data.namedPipe);
prev->next = temp->next;
free(temp);
return head;
}
NODE *free_list(NODE *head) {
NODE *tmpPtr = head;
NODE *followPtr;
while (tmpPtr != NULL) {
followPtr = tmpPtr;
tmpPtr = tmpPtr->next;
free(followPtr);
}
return NULL;
}
void print_list(NODE* head) {
NODE * temp;
for (temp = head; temp; temp = temp->next) printf("pid: %d\n",temp->data.pid);
}
int getPipe (NODE* head, int pid) {
NODE * temp;
for (temp = head; temp; temp = temp->next) if (temp->data.pid == pid) return temp->data.fd;
return -1;
}
int sizeList(NODE* head) {
int i = 0;
NODE * temp;
for (temp = head; temp; temp = temp->next) i++;
return i;
}
int pop(NODE* head) {
NODE* temp;
temp = head;
return temp->data.pid;
}
void print_sale(sale s, int i){
printf("Sale %d: Code: %d\t Quantity %d\t PaidAmount\t%d\n",i,s.code,s.quantity,s.paidAmount);
}
int getLine(int fd, char* buffer, int n){
char buf;
int max = n;
int cur = 0;
while(read(fd,&buf,1) && cur < max && buf!='\n') buffer[cur++]=buf;
buffer[cur] = '\0';
return cur;
}
int space_counter(char* buf){
int res = 0;
for(int i = 0; buf[i]!='\0'; i++) if (buf[i]==' ') res++;
return res;
}