---
name: "sd_journal_previous_skip"
section: "3"
platform: "linux"
package: "libsystemd-dev"
version: "257.13-1~deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/sd_journal_next"
---

# sd\_journal\_previous\_skip(3)

Advance or set back the read pointer in the journal

Part of [sd\_journal\_next(3)](/sd_journal_next/3.md).

## NAME

sd\_journal\_next, sd\_journal\_previous, sd\_journal\_step\_one, sd\_journal\_next\_skip, sd\_journal\_previous\_skip, SD\_JOURNAL\_FOREACH, SD\_JOURNAL\_FOREACH\_BACKWARDS - Advance or set back the read pointer in the journal

## SYNOPSIS

```
#include <systemd/sd-journal.h>
```

**int sd\_journal\_next(sd\_journal \****j***);**

**int sd\_journal\_previous(sd\_journal \****j***);**

**int sd\_journal\_step\_one(sd\_journal \****j***, int** *advanced***);**

**int sd\_journal\_next\_skip(sd\_journal \****j***, uint64\_t** *skip***);**

**int sd\_journal\_previous\_skip(sd\_journal \****j***, uint64\_t** *skip***);**

**SD\_JOURNAL\_FOREACH(sd\_journal \****j***);**

**SD\_JOURNAL\_FOREACH\_BACKWARDS(sd\_journal \****j***);**

## DESCRIPTION

**sd\_journal\_next()** advances the read pointer into the journal by one entry. The only argument taken is a journal context object as allocated via [**sd\_journal\_open**(3)](/sd_journal_open/3.md). After successful invocation the entry may be read with functions such as [**sd\_journal\_get\_data**(3)](/sd_journal_get_data/3.md).

Similarly, **sd\_journal\_previous()** sets the read pointer back one entry.

**sd\_journal\_step\_one()** also moves the read pointer. If the current location is the head of the journal, e.g. when this is called following **sd\_journal\_seek\_head()**, then this is equivalent to **sd\_journal\_next()**, and the argument *advanced* will be ignored. Similarly, if the current location is the tail of the journal, e.g. when this is called following **sd\_journal\_seek\_tail()**, then this is equivalent to **sd\_journal\_previous()**, and *advanced* will be ignored. Otherwise, this is equivalent to **sd\_journal\_next()** when *advanced* is non-zero, and **sd\_journal\_previous()** when *advanced* is zero.

**sd\_journal\_next\_skip()** and **sd\_journal\_previous\_skip()** advance/set back the read pointer by multiple entries at once, as specified in the *skip* parameter. The *skip* parameter must be less than or equal to 2147483647 (2³¹-1).

The journal is strictly ordered by reception time, and hence advancing to the next entry guarantees that the entry then pointing to is later in time than then previous one, or has the same timestamp.

Note that [**sd\_journal\_get\_data**(3)](/sd_journal_get_data/3.md) and related calls will fail unless **sd\_journal\_next()** has been invoked at least once in order to position the read pointer on a journal entry.

Note that the **SD\_JOURNAL\_FOREACH()** macro may be used as a wrapper around [**sd\_journal\_seek\_head**(3)](/sd_journal_seek_head/3.md) and **sd\_journal\_next()** in order to make iterating through the journal easier. See below for an example. Similarly, **SD\_JOURNAL\_FOREACH\_BACKWARDS()** may be used for iterating the journal in reverse order.

## RETURN VALUE

The four calls return the number of entries advanced/set back on success or a negative errno-style error code. When the end or beginning of the journal is reached, a number smaller than requested is returned. More specifically, if **sd\_journal\_next()** or **sd\_journal\_previous()** reach the end/beginning of the journal they will return 0, instead of 1 when they are successful. This should be considered an EOF marker.

## NOTES

All functions listed here are thread-agnostic and only a single thread may operate on a given object at any given time. Different threads may access the same object at different times. Multiple independent objects may be used from different threads in parallel.

Functions described here are available as a shared library, which can be compiled against and linked to with the **libsystemd** [**pkg-config**(1)](/pkg-config/1.md) file.

## EXAMPLES

Iterating through the journal:

```
/* SPDX-License-Identifier: MIT-0 */
#include <errno.h>
#include <stdio.h>
#include <systemd/sd-journal.h>
int main(int argc, char *argv[]) {
  int r;
  sd_journal *j;
  r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
  if (r < 0) {
    fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
    return 1;
  }
  SD_JOURNAL_FOREACH(j) {
    const char *d;
    size_t l;
    r = sd_journal_get_data(j, "MESSAGE", (const void **)&d, &l);
    if (r < 0) {
      fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
      continue;
    }
    printf("%.*s\n", (int) l, d);
  }
  sd_journal_close(j);
  return 0;
}
```

## HISTORY

**sd\_journal\_next()**, **sd\_journal\_previous()**, **sd\_journal\_next\_skip()**, **sd\_journal\_previous\_skip()**, **SD\_JOURNAL\_FOREACH()**, and **SD\_JOURNAL\_FOREACH\_BACKWARDS()** were added in version 187.

**sd\_journal\_step\_one()** was added in version 254.

## SEE ALSO

[**systemd**(1)](/systemd/1.md), [**sd-journal**(3)](/sd-journal/3.md), [**sd\_journal\_open**(3)](/sd_journal_open/3.md), [**sd\_journal\_get\_data**(3)](/sd_journal_get_data/3.md), [**sd\_journal\_get\_realtime\_usec**(3)](/sd_journal_get_realtime_usec/3.md), [**sd\_journal\_get\_cursor**(3)](/sd_journal_get_cursor/3.md)
