Skip to content

lib_profan

pf4 edited this page Sep 18, 2025 · 27 revisions

profan Library

profan is a library with small practical additions to the standard C library and other internal functions. It can be used to write userland programs.

Related files

Index

Functions

serial_debug

Exactly like printf, but sends the output to the serial portA.

int serial_debug (
    char *frm,
    ...
);
  • frm: The format string.
  • ...: The arguments to the format string.
  • Returns: The number of characters printed.

profan_fn_name

Return the name of a function from a pointer to it. The pointer does not have to be the start of the function. If libname is not NULL, it will be set to the name of the library the function is in. The returned string and the library name should not be freed.

char *profan_fn_name (
    void *ptr,
    char **libname
);
  • ptr: A pointer to the function.
  • libname: A pointer to a char *
  • Returns: The name of the function.

profan_print_trace

Print a backtrace with the names of the functions and the libraries in stderr. The stack trace is limited to 5 lines.

void profan_print_trace (
    void
);

profan_nimpl

Print a message to stderr that the function is not implemented and abort the program (the abort can be disabled in config file).

void profan_nimpl (
    char *func
);
  • func: The name of the function.

This macro can be used to call the function with the name of the current function.

#define PROFAN_FNI profan_nimpl(__FUNCTION__)

profan_libc_version

Return the version of the libc as a string. The returned string should not be freed.

char *profan_libc_version (void);
  • Returns: The version of the libc.

profan_path_join

Assemble a path from two parts, adding a '/' between them if necessary. The result is a newly allocated string that must be freed. If new is an absolute path, old is ignored.

char *profan_path_join (
    char *old,
    char *new
);
  • old: The first part of the path.
  • new: The second part of the path.
  • Returns: The assembled path (must be freed).

profan_path_sep

Separate a full path into a parent path and a content path. The parent is the path to the directory containing the content, and the content is the base name of the file or directory. This function does not check if the path is valid / exists. parent and cnt can be NULL if the value is not needed else they must be freed after usage.

void profan_path_sep (
    const char *fullpath,
    char **parent,
    char **cnt
);
  • fullpath: The full path to separate.
  • parent: A char * pointer to store the parent path.
  • cnt: A char * pointer to store the content path.

profan_path_path

Resolve a executable name to a full path from the PATH environment variable. The function will return NULL if the executable is not found. If allow_path is set to 1, the function accepts paths (absolute or relative).

char *profan_path_path (
    const char *exec,
    int allow_path
);
  • exec: The executable name.
  • allow_path: A flag to allow paths.
  • Returns: The full path to the executable (must be freed).

Note

The resolution and tolerant on the extensions ls can be resolved to /bin/cmd/ls.elf or /bin/cmd/ls.sh if the file exists.

profan_path_resolve

Resolve a path (absolute or relative) to a sector id. The function will a SID_NULL if the path is invalid or not found.

uint32_t profan_path_resolve (
    const char *path
);
  • path: The path to resolve.
  • Returns: The sector id of the path.

profan_path_simplify

Simplify a absolute path by removing . and .. components. The input path will be modified.

int profan_path_simplify (
    char *path
);
  • path: The path to simplify (will be modified).
  • Returns: 0 on success, -1 on failure.

profan_wd_sid

Get the sector id of the current working directory of the process.

uint32_t profan_wd_sid (void);
  • Returns: The sector id of the working directory.

profan_wd_path

Get the path of the current working directory of the process. The returned string should not be freed.

const char *profan_wd_path (void);
  • Returns: The path of the working directory.

profan_kalloc

Use the kernel allocator to allocate or free memory. The as_kernel flag is used to determine if the memory should be free when the process exits. Check the unix man page for information about aguments and return values.

void *profan_kmalloc (
    uint32_t size,
    int as_kernel
);

void *profan_kcalloc (
    uint32_t nmemb,
    uint32_t lsize,
    int as_kernel
);

void *profan_krealloc (
    void *mem,
    uint32_t new_size,
    int as_kernel
);

void profan_kfree (
    void *mem
);

profan_kb_load_map

Load a keyboard map from the a keymap file. The file should start with #KEYMAP and then have a list characters corresponding to the scancodes.

int profan_kb_load_map (
    char *path
);
  • path: The path to the keymap file.
  • Returns: 0 on success, 1 on failure.

profan_kb_get_char

Get the character corresponding to a scancode and shift state from the current loaded keymap. If the scancode is not found, it will return 0.

char profan_kb_get_char (
    uint8_t scancode,
    uint8_t shift
);
  • scancode: The scancode of the key.
  • shift: The shift state of the keyboard.
  • Returns: The corresponding character.

profan_input_keyboard

Wait for a key to be pressed, resolve it to a character using the current keymap, display it on the given terminal and append it to the output buffer. Returns a pointer to the output buffer and edits the size pointer to the size of the buffer after '\n' is pressed. The buffer must be freed after usage. Returns NULL on failure.

char *profan_input_keyboard (
    int  *size,
    char *term_path
);
  • size: A pointer to the size of the buffer.
  • term_path: The path to the terminal.
  • Returns: The input buffer (must be freed).

run_ifexist

Run a program if it exists. The program will be run in a new process. If pid_ptr is not NULL, it will be set to the pid of the new process. Returns -1 on failure and the exit code of the process on success (0 if the process was not finished).

typedef struct {
    char *path;
    char *wd;

    int argc;
    char **argv;
    char **envp;

    uint8_t sleep_mode;
} runtime_args_t;

int run_ifexist_full (
    runtime_args_t *args,
    int *pid_ptr
);
  • args: The arguments to run the program with.
    • path: The path to the program.
    • wd: The working directory.
    • argc: The number of arguments.
    • argv: The list of arguments.
    • envp: The list of environment variables.
    • sleep_mode: The sleep mode.
  • pid_ptr: A pointer to the pid of the new process.
  • Returns: The exit code of the process on success, -1 on failure.

Note

  • wd can be NULL to let the current PWD.
  • argv must start with the name of the program but must not necessarily end with a NULL pointer but envp must end with a NULL pointer!

Sleep mode is a flag interpreted like this:

  • 0: The process will be run in the background.
  • 1: The parent process will wait for the child process to finish.
  • 2: The child process will not be started.
  • 3: The program will be run in the current process (like exec).

Supported magical numbers:

Number Name Description
0x7F 'E' 'L' 'F' ELF file The program is an ELF file.
'#' '!' '/' Shebang standard shebang
'>' '!' '/' Fastbang profanOS fastbang

If the file format is not recognized, DEFRUN environment variable will be used to run the program. If DEFRUN is not set, the program will be run in the current process.

User Guide

Development Guide

Library Documentation

Clone this wiki locally