man-pages/man3/insque.3.html
2021-03-31 01:06:50 +01:00

322 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of INSQUE</TITLE>
</HEAD><BODY>
<H1>INSQUE</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
insque, remque - insert/remove an item from a queue
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/search.h">search.h</A>&gt;</B>
<B>void insque(void *</B><I>elem</I><B>, void *</B><I>prev</I><B>);</B>
<B>void remque(void *</B><I>elem</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>insque</B>(),
<B>remque</B>():
<DL COMPACT><DT id="1"><DD>
_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;since&nbsp;2.19:&nbsp;*/&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_SVID_SOURCE
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>insque</B>()
and
<B>remque</B>()
functions manipulate doubly-linked lists.
Each element in the list is a structure of
which the first two elements are a forward and a
backward pointer.
The linked list may be linear (i.e., NULL forward pointer at
the end of the list and NULL backward pointer at the start of the list)
or circular.
<P>
The
<B>insque</B>()
function inserts the element pointed to by <I>elem</I>
immediately after the element pointed to by <I>prev</I>.
<P>
If the list is linear, then the call
<I>insque(elem, NULL)</I>
can be used to insert the initial list element,
and the call sets the forward and backward pointers of
<I>elem</I>
to NULL.
<P>
If the list is circular,
the caller should ensure that the forward and backward pointers of the
first element are initialized to point to that element,
and the
<I>prev</I>
argument of the
<B>insque</B>()
call should also point to the element.
<P>
The
<B>remque</B>()
function removes the element pointed to by <I>elem</I> from the
doubly-linked list.
<A NAME="lbAE">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>insque</B>(),
<B>remque</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAF">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAG">&nbsp;</A>
<H2>NOTES</H2>
On ancient systems,
the arguments of these functions were of type <I>struct qelem *</I>,
defined as:
<P>
struct qelem {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;qelem&nbsp;*q_forw;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;qelem&nbsp;*q_back;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q_data[1];
};
<P>
This is still what you will get if
<B>_GNU_SOURCE</B>
is defined before
including <I>&lt;<A HREF="file:///usr/include/search.h">search.h</A>&gt;</I>.
<P>
The location of the prototypes for these functions differs among several
versions of UNIX.
The above is the POSIX version.
Some systems place them in <I>&lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;</I>.
<A NAME="lbAH">&nbsp;</A>
<H2>BUGS</H2>
In glibc 2.4 and earlier, it was not possible to specify
<I>prev</I>
as NULL.
Consequently, to build a linear list, the caller had to build a list
using an initial call that contained the first two elements of the list,
with the forward and backward pointers in each element suitably initialized.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below demonstrates the use of
<B>insque</B>().
Here is an example run of the program:
<P>
$ <B>./a.out -c a b c</B>
Traversing completed list:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;a
<BR>&nbsp;&nbsp;&nbsp;&nbsp;b
<BR>&nbsp;&nbsp;&nbsp;&nbsp;c
That was a circular list
<A NAME="lbAJ">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/search.h">search.h</A>&gt;
<P>
struct element {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;element&nbsp;*forward;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;element&nbsp;*backward;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*name;
};
<P>
static struct element *
new_element(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;element&nbsp;*e;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;=&nbsp;malloc(sizeof(struct&nbsp;element));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(e&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;malloc()&nbsp;failed\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;e;
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;element&nbsp;*first,&nbsp;*elem,&nbsp;*prev;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;circular,&nbsp;opt,&nbsp;errfnd;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;&quot;-c&quot;&nbsp;command-line&nbsp;option&nbsp;can&nbsp;be&nbsp;used&nbsp;to&nbsp;specify&nbsp;that&nbsp;the
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list&nbsp;is&nbsp;circular&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;errfnd&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;circular&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((opt&nbsp;=&nbsp;getopt(argc,&nbsp;argv,&nbsp;&quot;c&quot;))&nbsp;!=&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(opt)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;'c':
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;circular&nbsp;=&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errfnd&nbsp;=&nbsp;1;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(errfnd&nbsp;||&nbsp;optind&nbsp;&gt;=&nbsp;argc)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&nbsp;&quot;Usage:&nbsp;%s&nbsp;[-c]&nbsp;string...\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Create&nbsp;first&nbsp;element&nbsp;and&nbsp;place&nbsp;it&nbsp;in&nbsp;the&nbsp;linked&nbsp;list&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;elem&nbsp;=&nbsp;new_element();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;first&nbsp;=&nbsp;elem;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;elem-&gt;name&nbsp;=&nbsp;argv[optind];
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(circular)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elem-&gt;forward&nbsp;=&nbsp;elem;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elem-&gt;backward&nbsp;=&nbsp;elem;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insque(elem,&nbsp;elem);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insque(elem,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Add&nbsp;remaining&nbsp;command-line&nbsp;arguments&nbsp;as&nbsp;list&nbsp;elements&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(++optind&nbsp;&lt;&nbsp;argc)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prev&nbsp;=&nbsp;elem;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elem&nbsp;=&nbsp;new_element();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elem-&gt;name&nbsp;=&nbsp;argv[optind];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insque(elem,&nbsp;prev);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Traverse&nbsp;the&nbsp;list&nbsp;from&nbsp;the&nbsp;start,&nbsp;printing&nbsp;element&nbsp;names&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Traversing&nbsp;completed&nbsp;list:\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;elem&nbsp;=&nbsp;first;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;&nbsp;%s\n&quot;,&nbsp;elem-&gt;name);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elem&nbsp;=&nbsp;elem-&gt;forward;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(elem&nbsp;!=&nbsp;NULL&nbsp;&amp;&amp;&nbsp;elem&nbsp;!=&nbsp;first);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(elem&nbsp;==&nbsp;first)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;That&nbsp;was&nbsp;a&nbsp;circular&nbsp;list\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+queue">queue</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="5"><A HREF="#lbAE">ATTRIBUTES</A><DD>
<DT id="6"><A HREF="#lbAF">CONFORMING TO</A><DD>
<DT id="7"><A HREF="#lbAG">NOTES</A><DD>
<DT id="8"><A HREF="#lbAH">BUGS</A><DD>
<DT id="9"><A HREF="#lbAI">EXAMPLE</A><DD>
<DL>
<DT id="10"><A HREF="#lbAJ">Program source</A><DD>
</DL>
<DT id="11"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="12"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:46 GMT, March 31, 2021
</BODY>
</HTML>