back to scratko.xyz
summaryrefslogtreecommitdiff
path: root/queue.c
blob: 378723633173a12293b2d3a0778f37d22d1b6a8a (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "queue.h"
#include <stdlib.h>

#if 0
void w_queue_init(struct w_queue *q)
{
    q->first = NULL;
    q->last = NULL;
}

void w_queue_push(struct w_queue *q, char *word)
{
    struct word_item *tmp = malloc(sizeof(struct word_item));
    tmp->word = word;
    tmp->next = NULL;
    if(!q->first) {
        q->first = tmp;
        q->last = q->first;
    } else {
        q->last->next = tmp;
        q->last = q->last->next;
    }
}
#endif
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;
}

#if 0
void c_queue_push(struct c_queue *q, char **cmdline)
{
    struct cmdline_item *tmp = malloc(sizeof(struct cmdline_item));
    tmp->cmdline = cmdline;
    tmp->next = NULL;
    if(!q->first) {
        q->first = tmp;
        q->last = q->first;
    } else {
        q->last->next = tmp;
        q->last = q->last->next;
    }
}
#endif

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;
}
#if 0
void p_queue_init(struct p_queue *q)
{
    q->first = NULL;
    q->last = NULL;
}

void p_queue_push(struct p_queue *q, int pid)
{
    struct pid_item *tmp = malloc(sizeof(struct pid_item));
    tmp->pid = pid;
    tmp->next = NULL;
    if(!q->first) {
        q->first = tmp;
        q->last = q->first;
    } else {
        q->last->next = tmp;
        q->last = q->last->next;
    }
}
#endif

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;
}