]> andersk Git - moira.git/blame - lib/sq.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / sq.c
CommitLineData
fa59b86f 1/* $Id$
1992b825 2 *
3 * Generic Queue Routines
babbc197 4 *
7ac48069 5 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
1992b825 8 */
9
babbc197 10#include <mit-copyright.h>
8defc06b 11#include <moira.h>
7ac48069 12
a43ce477 13#include <stdlib.h>
7ac48069 14#include <string.h>
15
16RCSID("$Header$");
24582af9 17
5eaef520 18struct save_queue *sq_create(void)
1992b825 19{
44d12d58 20 struct save_queue *sq;
5eaef520 21
22 sq = malloc(sizeof(struct save_queue));
23 if (!sq)
24 return sq;
25 sq->q_next = sq;
26 sq->q_prev = sq;
27 sq->q_lastget = 0;
28 return sq;
1992b825 29}
30
7ac48069 31int sq_save_data(struct save_queue *sq, void *data)
1992b825 32{
44d12d58 33 struct save_queue *q;
5eaef520 34
35 q = malloc(sizeof(struct save_queue));
36 if (!q)
37 return 0;
38 q->q_next = sq;
39 q->q_prev = sq->q_prev;
40 sq->q_prev->q_next = q;
41 sq->q_prev = q;
42 q->q_data = data;
43 return 1;
1992b825 44}
45
44d12d58 46int sq_save_args(int argc, char *argv[], struct save_queue *sq)
1992b825 47{
44d12d58 48 char **argv_copy;
49 int i, n;
5eaef520 50
51 argv_copy = malloc(argc * sizeof(char *));
52 if (!argv_copy)
53 return 0;
54 for (i = 0; i < argc; i++)
55 {
56 n = strlen(argv[i]) + 1;
57 argv_copy[i] = malloc(n);
58 if (!argv_copy[i])
59 {
60 for (i--; i >= 0; i--)
61 free(argv_copy[i]);
62 free(argv_copy);
9d627547 63 return 0;
64 }
5eaef520 65 memcpy(argv_copy[i], argv[i], n);
1992b825 66 }
67
7ac48069 68 return sq_save_data(sq, argv_copy);
1992b825 69}
70
7ac48069 71int sq_save_unique_data(struct save_queue *sq, void *data)
1992b825 72{
44d12d58 73 struct save_queue *q;
1992b825 74
5eaef520 75 for (q = sq->q_next; q != sq; q = q->q_next)
76 {
77 if (q->q_data == data)
78 return 1;
79 }
1992b825 80
5eaef520 81 return sq_save_data(sq, data);
1992b825 82}
83
44d12d58 84int sq_save_unique_string(struct save_queue *sq, char *data)
1992b825 85{
44d12d58 86 struct save_queue *q;
1992b825 87
5eaef520 88 for (q = sq->q_next; q != sq; q = q->q_next)
89 {
90 if (!strcmp(q->q_data, data))
91 return 1;
92 }
1992b825 93
5eaef520 94 return sq_save_data(sq, data);
1992b825 95}
96
7ac48069 97/* in sq_get_data and sq_remove_data, `data' is actually a pointer to the
98 variable to put the data in to. */
99
100int sq_get_data(struct save_queue *sq, void *data)
1992b825 101{
7ac48069 102 void **dptr = data;
103
104 if (sq->q_lastget == NULL)
5eaef520 105 sq->q_lastget = sq->q_next;
106 else
107 sq->q_lastget = sq->q_lastget->q_next;
108
109 if (sq->q_lastget == sq)
110 return 0;
7ac48069 111 *dptr = sq->q_lastget->q_data;
5eaef520 112 return 1;
1992b825 113}
114
7ac48069 115int sq_remove_data(struct save_queue *sq, void *data)
bfa8d940 116{
7ac48069 117 void **dptr = data;
118
5eaef520 119 if (sq->q_next != sq)
120 {
7ac48069 121 *dptr = sq->q_next->q_data;
5eaef520 122 sq->q_next = sq->q_next->q_next;
123 free(sq->q_next->q_prev);
124 sq->q_next->q_prev = sq;
125 return 1;
bfa8d940 126 }
5eaef520 127 return 0;
bfa8d940 128}
129
444b050f 130void sq_remove_last_data(struct save_queue *sq)
131{
132 struct save_queue *rem = sq->q_lastget;
133
134 if (rem != NULL)
135 {
136 rem->q_next->q_prev = sq->q_lastget = rem->q_prev;
137 rem->q_prev->q_next = rem->q_next;
138 free(rem);
139 }
140}
141
44d12d58 142int sq_empty(struct save_queue *sq)
bfa8d940 143{
5eaef520 144 if (sq->q_next == sq)
145 return 1;
146 else
147 return 0;
bfa8d940 148}
149
7ac48069 150void sq_destroy(struct save_queue *sq)
1992b825 151{
44d12d58 152 struct save_queue *q;
1992b825 153
5eaef520 154 for (q = sq->q_next; q != sq; q = sq->q_next)
155 {
156 sq->q_next = q->q_next;
157 free(q);
1992b825 158 }
5eaef520 159 free(sq);
1992b825 160}
161
This page took 1.022239 seconds and 5 git commands to generate.