cbashshelllibraries

How to execute library calls commands from shell?


Sorry for If I look stupid asking this question, but I am very curious to know about it.

I wanted to simply calculate the length of a string (that is hash value). So, I opened terminal and did this:

$ apropos length

that returned me with a bunch of commands/functions having (3) or (3ssl) appended at the end of them. Now man man gives us information about what these section numbers mean.

3   Library calls (functions within program libraries)

Out of curiosity, I just tried with all these commands (in hope at least one would work)

strcspn (3)          - get length of a prefix substring
strlen (3)           - calculate the length of a string
strnlen (3)          - determine the length of a fixed-size string
strspn (3)           - get length of a prefix substring
wcslen (3)           - determine the length of a wide-character string
wcsnlen (3)          - determine the length of a fixed-size wide-character string

and got nothing but same error for every command

$ strnlen HelloWorld 
$ strnlen: command not found

Well, I know how to find length of string in shell using wc -m , expr length and other workarounds.But,

Is it possible to write a bash script that can internally call these library commands and get the task done ?


Solution

  • You cannot execute any library function from your Bash shell (in general) - excluding of course the many library functions that your shell calls (for example its cd builtin will call the chdir(2) system call).

    You should use some program (usually some executable produced by the C compiler, or some other compiled language like Go, Ocaml, C++, Rust, ...) to call these functions. Once you've got a program your shell would use fork(2) then execve(2) to run it.

    Some shells (e.g. zsh) also accept plugins. You could write (in C, then compile it) an extension, make a plugin of it and load that plugin.

    In practice, to run the wcslen(3) function from your shell, you should write some C source code calling that (e.g. yourprog.c), compile that source code into an ELF executable (e.g. with gcc -Wall -Wextra -g yourprog.c -o yourbin with GCC) then run that ./yourbin executable with appropriate arguments. You should expect to make bugs in your program and to spend time debugging it (so use the gdb debugger to understand what is going wrong, then improve your code in yourprog.c and repeat).

    BTW, a shell deals mostly with strings (and perhaps arrays of them), but most C functions consume and/or return data which are not strings, but something more complex (for examples: fopen(3) returns a FILE* handle, readdir(3) and fgetpwent(3) don't manipulate strings, etc etc...). So there is an "impedance mismatch", and you need to have or write code to solve that.

    You could be interested in using some scripting language like Guile or Python.

    BTW, be aware that in practice UTF-8 is used everywhere today on Linux (so it is unlikely you want to call wcslen).