Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You have a will that can be influenced by all with whom you come in contact.


comp / comp.lang.python / glibc strverscmp called from python

SubjectAuthor
o glibc strverscmp called from pythonvallor

1
Subject: glibc strverscmp called from python
From: vallor
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Thu, 20 Jun 2024 22:13 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vallor@cultnix.org (vallor)
Newsgroups: comp.lang.python
Subject: glibc strverscmp called from python
Date: Thu, 20 Jun 2024 22:13:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <v529iv$2lkuv$3@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 21 Jun 2024 00:13:51 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a414156ff05630fd2d65fa3742155188";
logging-data="2806751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+16KFx8LGSKhX0FR1TkTck"
User-Agent: Pan/0.159 (Vovchansk; 873418b; Linux-6.9.5)
Cancel-Lock: sha1:ENqlIR48b2IblwUfGR9ikRoGvI8=
X-Face: \}2`P"_@pS86<'EM:'b.Ml}8IuMK"pV"?FReF$'c.S%u9<Q#U*4QO)$l81M`{Q/n
XL'`91kd%N::LG:=*\35JS0prp\VJN^<s"b#bff@fA7]5lJA.jn,x_d%Md$,{.EZ
View all headers

So there's been discussion in comp.lang.c and comp.unix.shell
about doing a "versionsort(3)" type sort on a list
of parameters. glibc offers strverscmp(3) for this type
of sort, and here I am posting a q&d python program to expose
that to its sort routine for commentary and future reference.

Caveat: I know just enough python to be dangerous -- wrote
this using ChatGPT. It is a learning experience, comments
very much appreciated.

- -%<- -

#!/usr/bin/python3

import ctypes
from ctypes import c_char_p, c_int
import os
import sys

# Load the C standard library (libc)
libc = ctypes.CDLL("libc.so.6")

# Define the prototype of strverscmp
# int strverscmp (const char *s1, const char *s2)
libc.strverscmp.argtypes = [c_char_p, c_char_p]
libc.strverscmp.restype = c_int

# Define a comparison function for Python sorting
def version_compare(x, y):
return libc.strverscmp(x.encode('utf-8'), y.encode('utf-8'))

# Define a key function for sorting
def version_key(s):
class K:
def __init__(self, s):
self.s = s
def __lt__(self, other):
return version_compare(self.s, other.s) < 0
def __gt__(self, other):
return version_compare(self.s, other.s) > 0
def __eq__(self, other):
return version_compare(self.s, other.s) == 0
def __le__(self, other):
return version_compare(self.s, other.s) <= 0
def __ge__(self, other):
return version_compare(self.s, other.s) >= 0
def __ne__(self, other):
return version_compare(self.s, other.s) != 0
return K(s)

# Function to escape special characters
def shell_escape(s):
return s.replace(" ", "\\ ").replace("\n", "\\n").replace("\t", "\\t")

# Parse command-line arguments
args = sys.argv[1:]

# Sort the list using the version key
sorted_args = sorted(args, key=version_key)

# Print each sorted, escaped value on a new line
for arg in sorted_args:
print(shell_escape(arg))

- -%<- -

--
-v

1

rocksolid light 0.9.8
clearnet tor