---
name: "erl_eterm"
section: "3erl"
platform: "linux"
package: "erlang-manpages"
version: "1:21.2.6+dfsg-1"
release: "Debian 10 (Buster)"
platforms: ["linux"]
url: "https://new.carta.tech/erl_eterm"
---

# erl\_eterm(3erl)

Functions for Erlang term construction.

## NAME

erl\_eterm - Functions for Erlang term construction.

## DESCRIPTION

This module provides functions for creating and manipulating Erlang terms.

An Erlang term is represented by a C structure of type *ETERM*. Applications should not reference any fields in this structure directly, as it can be changed in future releases to provide faster and more compact term storage. Instead, applications should use the macros and functions provided.

Each of the following macros takes a single *ETERM* pointer as an argument. The macros return a non-zero value if the test is true, otherwise *0*.

- ***ERL\_IS\_INTEGER(t)*:**

  True if *t* is an integer.

- ***ERL\_IS\_UNSIGNED\_INTEGER(t)*:**

  True if *t* is an integer.

- ***ERL\_IS\_FLOAT(t)*:**

  True if *t* is a floating point number.

- ***ERL\_IS\_ATOM(t)*:**

  True if *t* is an atom.

- ***ERL\_IS\_PID(t)*:**

  True if *t* is a pid (process identifier).

- ***ERL\_IS\_PORT(t)*:**

  True if *t* is a port.

- ***ERL\_IS\_REF(t)*:**

  True if *t* is a reference.

- ***ERL\_IS\_TUPLE(t)*:**

  True if *t* is a tuple.

- ***ERL\_IS\_BINARY(t)*:**

  True if *t* is a binary.

- ***ERL\_IS\_LIST(t)*:**

  True if *t* is a list with zero or more elements.

- ***ERL\_IS\_EMPTY\_LIST(t)*:**

  True if *t* is an empty list.

- ***ERL\_IS\_CONS(t)*:**

  True if *t* is a list with at least one element.

The following macros can be used for retrieving parts of Erlang terms. None of these do any type checking. Results are undefined if you pass an *ETERM\** containing the wrong type. For example, passing a tuple to *ERL\_ATOM\_PTR()* likely results in garbage.

- ***char \*ERL\_ATOM\_PTR(t)*:**

  - ***char \*ERL\_ATOM\_PTR\_UTF8(t)*:**

  A string representing atom *t*.

- ***int ERL\_ATOM\_SIZE(t)*:**

  - ***int ERL\_ATOM\_SIZE\_UTF8(t)*:**

  The length (in bytes) of atom *t*.

- ***void \*ERL\_BIN\_PTR(t)*:**

  A pointer to the contents of *t*.

- ***int ERL\_BIN\_SIZE(t)*:**

  The length (in bytes) of binary object *t*.

- ***int ERL\_INT\_VALUE(t)*:**

  The integer of *t*.

- ***unsigned int ERL\_INT\_UVALUE(t)*:**

  The unsigned integer value of *t*.

- ***double ERL\_FLOAT\_VALUE(t)*:**

  The floating point value of *t*.

- ***ETERM \*ERL\_PID\_NODE(t)*:**

  - ***ETERM \*ERL\_PID\_NODE\_UTF8(t)*:**

  The node in pid *t*.

- ***int ERL\_PID\_NUMBER(t)*:**

  The sequence number in pid *t*.

- ***int ERL\_PID\_SERIAL(t)*:**

  The serial number in pid *t*.

- ***int ERL\_PID\_CREATION(t)*:**

  The creation number in pid *t*.

- ***int ERL\_PORT\_NUMBER(t)*:**

  The sequence number in port *t*.

- ***int ERL\_PORT\_CREATION(t)*:**

  The creation number in port *t*.

- ***ETERM \*ERL\_PORT\_NODE(t)*:**

  - ***ETERM \*ERL\_PORT\_NODE\_UTF8(t)*:**

  The node in port *t*.

- ***int ERL\_REF\_NUMBER(t)*:**

  The first part of the reference number in ref *t*. Use only for compatibility.

- ***int ERL\_REF\_NUMBERS(t)*:**

  Pointer to the array of reference numbers in ref *t*.

- ***int ERL\_REF\_LEN(t)*:**

  The number of used reference numbers in ref *t*.

- ***int ERL\_REF\_CREATION(t)*:**

  The creation number in ref *t*.

- ***int ERL\_TUPLE\_SIZE(t)*:**

  The number of elements in tuple *t*.

- ***ETERM \*ERL\_CONS\_HEAD(t)*:**

  The head element of list *t*.

- ***ETERM \*ERL\_CONS\_TAIL(t)*:**

  A list representing the tail elements of list *t*.

## EXPORTS

**ETERM \*erl\_cons(head, tail)**\

Types:

ETERM \*head;\
 ETERM \*tail;\

Concatenates two Erlang terms, prepending *head* onto *tail* and thereby creating a *cons* cell. To make a proper list, *tail* is always to be a list or an empty list. Notice that *NULL* is not a valid list.

- \*

  *head* is the new term to be added.

- \*

  *tail* is the existing list to which *head* is concatenated.

The function returns a new list.

*ERL\_CONS\_HEAD(list)* and *ERL\_CONS\_TAIL(list)* can be used to retrieve the head and tail components from the list. *erl\_hd(list)* and *erl\_tl(list)* do the same thing, but check that the argument really is a list.

*Example:*

```
ETERM *list,*anAtom,*anInt;
anAtom = erl_mk_atom("madonna");
anInt  = erl_mk_int(21);
list   = erl_mk_empty_list();
list   = erl_cons(anAtom, list);
list   = erl_cons(anInt, list);
 ... /* do some work */
erl_free_compound(list);
```

**ETERM \*erl\_copy\_term(term)**\

Types:

ETERM \*term;\

Creates and returns a copy of the Erlang term *term*.

**ETERM \*erl\_element(position, tuple)**\

Types:

int position;\
 ETERM \*tuple;\

Extracts a specified element from an Erlang tuple.

- \*

  *position* specifies which element to retrieve from *tuple*. The elements are numbered starting from 1.

- \*

  *tuple* is an Erlang term containing at least *position* elements.

Returns a new Erlang term corresponding to the requested element, or *NULL* if *position* was greater than the arity of *tuple*.

**ETERM \*erl\_hd(list)**\

Types:

ETERM \*list;\

Extracts the first element from a list.

*list* is an Erlang term containing a list.

Returns an Erlang term corresponding to the head head element in the list, or a *NULL* pointer if *list* was not a list.

**void erl\_init(NULL, 0)**\

Types:

void \*NULL;\
 int 0;\

This function must be called before any of the others in the *Erl\_Interface* library to initialize the library functions. The arguments must be specified as *erl\_init(NULL,0)*.

**int erl\_iolist\_length(list)**\

Types:

ETERM \*list;\

Returns the length of an I/O list.

*list* is an Erlang term containing an I/O list.

Returns the length of *list*, or *-1* if *list* is not an I/O list.

For the definition of an I/O list, see *erl\_iolist\_to\_binary*.

**ETERM \*erl\_iolist\_to\_binary(term)**\

Types:

ETERM \*list;\

Converts an I/O list to a binary term.

*list* is an Erlang term containing a list.

Returns an Erlang binary term, or *NULL* if *list* was not an I/O list.

Informally, an I/O list is a deep list of characters and binaries that can be sent to an Erlang port. In BNF, an I/O list is formally defined as follows:

```
iolist ::= []
        |   Binary
        |   [iohead | iolist]
        ;
iohead ::= Binary
        |   Byte (integer in the range [0..255])
        |   iolist
        ;
```

**char \*erl\_iolist\_to\_string(list)**\

Types:

ETERM \*list;\

Converts an I/O list to a *NULL*-terminated C string.

*list* is an Erlang term containing an I/O list. The I/O list must not contain the integer 0, as C strings may not contain this value except as a terminating marker.

Returns a pointer to a dynamically allocated buffer containing a string. If *list* is not an I/O list, or if *list* contains the integer 0, *NULL* is returned. It is the caller's responsibility to free the allocated buffer with *erl\_free()*.

For the definition of an I/O list, see *erl\_iolist\_to\_binary*.

**int erl\_length(list)**\

Types:

ETERM \*list;\

Determines the length of a proper list.

*list* is an Erlang term containing a proper list. In a proper list, all tails except the last point to another list cell, and the last tail points to an empty list.

Returns *-1* if *list* is not a proper list.

**ETERM \*erl\_mk\_atom(string)**\

Types:

const char \*string;\

Creates an atom.

*string* is the sequence of characters that will be used to create the atom.

Returns an Erlang term containing an atom. Notice that it is the caller's responsibility to ensure that *string* contains a valid name for an atom.

*ERL\_ATOM\_PTR(atom)* and *ERL\_ATOM\_PTR\_UTF8(atom)* can be used to retrieve the atom name (as a *NULL*-terminated string). *ERL\_ATOM\_SIZE(atom)* and *ERL\_ATOM\_SIZE\_UTF8(atom)* return the length of the atom name.

**Note:**

The UTF-8 variants were introduced in Erlang/OTP R16 and the string returned by *ERL\_ATOM\_PTR(atom)* was not *NULL*-terminated on older releases.

**ETERM \*erl\_mk\_binary(bptr, size)**\

Types:

char \*bptr;\
 int size;\

Produces an Erlang binary object from a buffer containing a sequence of bytes.

- \*

  *bptr* is a pointer to a buffer containing data to be converted.

- \*

  *size* indicates the length of *bptr*.

Returns an Erlang binary object.

*ERL\_BIN\_PTR(bin)* retrieves a pointer to the binary data. *ERL\_BIN\_SIZE(bin)* retrieves the size.

**ETERM \*erl\_mk\_empty\_list()**\

Creates and returns an empty Erlang list. Notice that *NULL* is not used to represent an empty list; Use this function instead.

**ETERM \*erl\_mk\_estring(string, len)**\

Types:

char \*string;\
 int len;\

Creates a list from a sequence of bytes.

- \*

  *string* is a buffer containing a sequence of bytes. The buffer does not need to be *NULL*-terminated.

- \*

  *len* is the length of *string*.

Returns an Erlang list object corresponding to the character sequence in *string*.

**ETERM \*erl\_mk\_float(f)**\

Types:

double f;\

Creates an Erlang float.

*f* is a value to be converted to an Erlang float.

Returns an Erlang float object with the value specified in *f* or *NULL* if *f* is not finite.

*ERL\_FLOAT\_VALUE(t)* can be used to retrieve the value from an Erlang float.

**ETERM \*erl\_mk\_int(n)**\

Types:

int n;\

Creates an Erlang integer.

*n* is a value to be converted to an Erlang integer.

Returns an Erlang integer object with the value specified in *n*.

*ERL\_INT\_VALUE(t)* can be used to retrieve the value from an Erlang integer.

**ETERM \*erl\_mk\_list(array, arrsize)**\

Types:

ETERM \*\*array;\
 int arrsize;\

Creates an Erlang list from an array of Erlang terms, such that each element in the list corresponds to one element in the array.

- \*

  *array* is an array of Erlang terms.

- \*

  *arrsize* is the number of elements in *array*.

The function creates an Erlang list object, whose length *arrsize* and whose elements are taken from the terms in *array*.

**ETERM \*erl\_mk\_long\_ref(node, n1, n2, n3, creation)**\

Types:

const char \*node;\
 unsigned int n1, n2, n3;\
 unsigned int creation;\

Creates an Erlang reference, with 82 bits.

- \*

  *node* is the name of the C-node.

- \*

  *n1*, *n2*, and *n3* can be seen as one big number *n1\*2^64+n2\*2^32+n3*, which is to be chosen uniquely for each reference created for a given C-node.

- \*

  *creation* is an arbitrary number.

Notice that *n3* and *creation* are limited in precision, so only the low 18 and 2 bits of these numbers are used.

Returns an Erlang reference object.

*ERL\_REF\_NODE(ref)*, *ERL\_REF\_NUMBERS(ref)*, *ERL\_REF\_LEN(ref)*, and *ERL\_REF\_CREATION(ref)* can be used to retrieve the values used to create the reference.

**ETERM \*erl\_mk\_pid(node, number, serial, creation)**\

Types:

const char \*node;\
 unsigned int number;\
 unsigned int serial;\
 unsigned int creation;\

Creates an Erlang process identifier (pid). The resulting pid can be used by Erlang processes wishing to communicate with the C-node.

- \*

  *node* is the name of the C-node.

- \*

  *number*, *serial*, and *creation* are arbitrary numbers. Notice that these are limited in precision, so only the low 15, 3, and 2 bits of these numbers are used.

Returns an Erlang pid object.

*ERL\_PID\_NODE(pid)*, *ERL\_PID\_NUMBER(pid)*, *ERL\_PID\_SERIAL(pid)*, and *ERL\_PID\_CREATION(pid)* can be used to retrieve the four values used to create the pid.

**ETERM \*erl\_mk\_port(node, number, creation)**\

Types:

const char \*node;\
 unsigned int number;\
 unsigned int creation;\

Creates an Erlang port identifier.

- \*

  *node* is the name of the C-node.

- \*

  *number* and *creation* are arbitrary numbers. Notice that these are limited in precision, so only the low 18 and 2 bits of these numbers are used.

Returns an Erlang port object.

*ERL\_PORT\_NODE(port)*, *ERL\_PORT\_NUMBER(port)*, and *ERL\_PORT\_CREATION* can be used to retrieve the three values used to create the port.

**ETERM \*erl\_mk\_ref(node, number, creation)**\

Types:

const char \*node;\
 unsigned int number;\
 unsigned int creation;\

Creates an old Erlang reference, with only 18 bits - use *erl\_mk\_long\_ref* instead.

- \*

  *node* is the name of the C-node.

- \*

  *number* is to be chosen uniquely for each reference created for a given C-node.

- \*

  *creation* is an arbitrary number.

Notice that *number* and *creation* are limited in precision, so only the low 18 and 2 bits of these numbers are used.

Returns an Erlang reference object.

*ERL\_REF\_NODE(ref)*, *ERL\_REF\_NUMBER(ref)*, and *ERL\_REF\_CREATION(ref)* can be used to retrieve the three values used to create the reference.

**ETERM \*erl\_mk\_string(string)**\

Types:

char \*string;\

Creates a list from a *NULL*-terminated string.

*string* is a *NULL*-terminated sequence of characters (that is, a C string) from which the list will be created.

Returns an Erlang list.

**ETERM \*erl\_mk\_tuple(array, arrsize)**\

Types:

ETERM \*\*array;\
 int arrsize;\

Creates an Erlang tuple from an array of Erlang terms.

- \*

  *array* is an array of Erlang terms.

- \*

  *arrsize* is the number of elements in *array*.

The function creates an Erlang tuple, whose arity is *size* and whose elements are taken from the terms in *array*.

To retrieve the size of a tuple, either use function *erl\_size* (which checks the type of the checked term and works for a binary as well as for a tuple) or *ERL\_TUPLE\_SIZE(tuple)* returns the arity of a tuple. *erl\_size()* does the same thing, but it checks that the argument is a tuple. *erl\_element(index,tuple)* returns the element corresponding to a given position in the tuple.

**ETERM \*erl\_mk\_uint(n)**\

Types:

unsigned int n;\

Creates an Erlang unsigned integer.

*n* is a value to be converted to an Erlang unsigned integer.

Returns an Erlang unsigned integer object with the value specified in *n*.

*ERL\_INT\_UVALUE(t)* can be used to retrieve the value from an Erlang unsigned integer.

**ETERM \*erl\_mk\_var(name)**\

Types:

char \*name;\

Creates an unbound Erlang variable. The variable can later be bound through pattern matching or assignment.

*name* specifies a name for the variable.

Returns an Erlang variable object with the name *name*.

**int erl\_print\_term(stream, term)**\

Types:

FILE \*stream;\
 ETERM \*term;\

Prints the specified Erlang term to the specified output stream.

- \*

  *stream* indicates where the function is to send its output.

- \*

  *term* is the Erlang term to print.

Returns the number of characters written on success, otherwise a negative value.

**void erl\_set\_compat\_rel(release\_number)**\

Types:

unsigned release\_number;\

By default, the *Erl\_Interface* library is only guaranteed to be compatible with other Erlang/OTP components from the same release as the *Erl\_Interface* library itself. For example, *Erl\_Interface* from Erlang/OTP R10 is not compatible with an Erlang emulator from Erlang/OTP R9 by default.

A call to *erl\_set\_compat\_rel(release\_number)* sets the *Erl\_Interface* library in compatibility mode of release *release\_number*. Valid range of *release\_number* is \[7, current release\]. This makes it possible to communicate with Erlang/OTP components from earlier releases.

**Note:**

If this function is called, it may only be called once directly after the call to function **erl\_init()**.

**Warning:**

You may run into trouble if this feature is used carelessly. Always ensure that all communicating components are either from the same Erlang/OTP release, or from release X and release Y where all components from release Y are in compatibility mode of release X.

**int erl\_size(term)**\

Types:

ETERM \*term;\

Returns either the arity of an Erlang tuple or the number of bytes in an Erlang binary object.

*term* is an Erlang tuple or an Erlang binary object.

Returns the size of *term* as described above, or *-1* if *term* is not one of the two supported types.

**ETERM \*erl\_tl(list)**\

Types:

ETERM \*list;\

Extracts the tail from a list.

*list* is an Erlang term containing a list.

Returns an Erlang list corresponding to the original list minus the first element, or *NULL* pointer if *list* was not a list.

**ETERM \*erl\_var\_content(term, name)**\

Types:

ETERM \*term;\
 char \*name;\

Returns the contents of the specified variable in an Erlang term.

- \*

  *term* is an Erlang term. In order for this function to succeed, *term* must either be an Erlang variable with the specified name, or it must be an Erlang list or tuple containing a variable with the specified name. Other Erlang types cannot contain variables.

- \*

  *name* is the name of an Erlang variable.

Returns the Erlang object corresponding to the value of *name* in *term*. If no variable with the name *name* is found in *term*, or if *term* is not a valid Erlang term, *NULL* is returned.
