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

221 lines
5.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of STRVERSCMP</TITLE>
</HEAD><BODY>
<H1>STRVERSCMP</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>
strverscmp - compare two version strings
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
<B>#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;</B>
<B>int strverscmp(const char *</B><I>s1</I><B>, const char *</B><I>s2</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
Often one has files
<I>jan1</I>, <I>jan2</I>, ..., <I>jan9</I>, <I>jan10</I>, ...
and it feels wrong when
<B><A HREF="/cgi-bin/man/man2html?1+ls">ls</A></B>(1)
orders them
<I>jan1</I>, <I>jan10</I>, ..., <I>jan2</I>, ..., <I>jan9</I>.
In order to rectify this, GNU introduced the
<I>-v</I>
option to
<B><A HREF="/cgi-bin/man/man2html?1+ls">ls</A></B>(1),
which is implemented using
<B><A HREF="/cgi-bin/man/man2html?3+versionsort">versionsort</A></B>(3),
which again uses
<B>strverscmp</B>().
<P>
Thus, the task of
<B>strverscmp</B>()
is to compare two strings and find the &quot;right&quot; order, while
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3)
finds only the lexicographic order.
This function does not use
the locale category
<B>LC_COLLATE</B>,
so is meant mostly for situations
where the strings are expected to be in ASCII.
<P>
What this function does is the following.
If both strings are equal, return 0.
Otherwise, find the position
between two bytes with the property that before it both strings are equal,
while directly after it there is a difference.
Find the largest consecutive digit strings containing (or starting at,
or ending at) this position.
If one or both of these is empty,
then return what
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3)
would have returned (numerical ordering of byte values).
Otherwise, compare both digit strings numerically, where digit strings with
one or more leading zeros are interpreted as if they have a decimal point
in front (so that in particular digit strings with more leading zeros
come before digit strings with fewer leading zeros).
Thus, the ordering is
<I>000</I>, <I>00</I>, <I>01</I>, <I>010</I>, <I>09</I>, <I>0</I>, <I>1</I>, <I>9</I>, <I>10</I>.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>strverscmp</B>()
function returns an integer
less than, equal to, or greater than zero if
<I>s1</I>
is found, respectively, to be earlier than, equal to,
or later than
<I>s2</I>.
<A NAME="lbAF">&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>strverscmp</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
This function is a GNU extension.
<A NAME="lbAH">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below can be used to demonstrate the behavior of
<B>strverscmp</B>().
It uses
<B>strverscmp</B>()
to compare the two strings given as its command-line arguments.
An example of its use is the following:
<P>
$ <B>./a.out jan1 jan10</B>
jan1 &lt; jan10
<A NAME="lbAI">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#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;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;res;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;3)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;string1&gt;&nbsp;&lt;string2&gt;\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;res&nbsp;=&nbsp;strverscmp(argv[1],&nbsp;argv[2]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s&nbsp;%s&nbsp;%s\n&quot;,&nbsp;argv[1],
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(res&nbsp;&lt;&nbsp;0)&nbsp;?&nbsp;&quot;&lt;&quot;&nbsp;:&nbsp;(res&nbsp;==&nbsp;0)&nbsp;?&nbsp;&quot;==&quot;&nbsp;:&nbsp;&quot;&gt;&quot;,&nbsp;argv[2]);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+rename">rename</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?3+strcasecmp">strcasecmp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strcoll">strcoll</A></B>(3)
<A NAME="lbAK">&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="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="6"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="7"><A HREF="#lbAH">EXAMPLE</A><DD>
<DL>
<DT id="8"><A HREF="#lbAI">Program source</A><DD>
</DL>
<DT id="9"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="10"><A HREF="#lbAK">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:58 GMT, March 31, 2021
</BODY>
</HTML>