back to scratko.xyz
summaryrefslogtreecommitdiff
path: root/queue.c
blob: 9d77d00991fcd51e10f667ef46fb93d2b670a934 (plain)
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
#include "queue.h"
#include <stdlib.h>

void w_queue_clear(struct w_queue *q)
{
    struct word_item *tmp;
    while(q->first) {
        tmp = q->first;
        q->first = q->first->next;
        free(tmp);
    }
    q->last = NULL;
}

int w_queue_get_word_count(const struct w_queue *q)
{
    struct word_item *tmp;
    int counter;
    for(counter = 0, tmp = q->first; tmp; tmp = tmp->next, ++counter)
    {}
    return counter;
}

void w_queue_copy_words_to_args(const struct w_queue *q, char **cmdline)
{
    struct word_item *tmp;
    int mas_idx;
    for(tmp = q->first, mas_idx = 0; tmp; tmp = tmp->next, ++mas_idx)
        cmdline[mas_idx] = tmp->word;
    cmdline[mas_idx] = NULL;
}

void c_queue_init(struct c_queue *q)
{
    q->first = NULL;
    q->last = NULL;
    q->last_extracted_item = NULL;
}

int c_queue_is_empty(struct c_queue *q)
{
    return q->last_extracted_item->next == NULL;
}

char** c_queue_pop(struct c_queue *q)
{
    if(!q->last_extracted_item) {
        if(q->first) {
            q->last_extracted_item = q->first;
            return q->last_extracted_item->cmdline;
        }
        return NULL;
    } else {
        if(q->last_extracted_item->next) {
            q->last_extracted_item = q->last_extracted_item->next;
            return q->last_extracted_item->cmdline;
        }
        return NULL;
    }
}

void c_queue_clear(struct c_queue *q)
{
    struct cmdline_item *tmp = NULL;
    while(q->first) {
        int i;
        for(i = 0; q->first->cmdline[i]; ++i)
            free(q->first->cmdline[i]);
        free(q->first->cmdline);
        tmp = q->first;
        q->first = q->first->next;
        free(tmp);
    }
    q->last = NULL;
    q->last_extracted_item = NULL;
}

int p_queue_find_pid(struct p_queue *q, int pid)
{
    struct pid_item *tmp = q->first;
    while(tmp) {
        if(tmp->pid == pid)
            return 1;
        tmp = tmp->next;
    }
    return 0;
}

int p_queue_get_process_quantity(struct p_queue *q)
{
    int counter = 0;
    struct pid_item *tmp = q->first;
    while(tmp) {
        ++counter;
        tmp = tmp->next;
    }
    return counter;
}

void p_queue_clear(struct p_queue *q)
{
    struct pid_item *tmp = NULL;
    while(q->first) {
        tmp = q->first;
        q->first = q->first->next;
        free(tmp);
    }
    q->last = NULL;
}