CURLOPT_SEEKFUNCTION(3)

πŸ’… CURLOPT_SEEKFUNCTION(3) β€” the "user callback for seeking in input stream" one πŸŽ€

okay bestie, so this is a page from the Library Functions Manual and it's literally all about handing libcurl your very own seek callback. it's giving "let me skip around in my input stream" energy and honestly? iconic. ✨


🌸 SYNOPSIS 🌸

#include <curl/curl.h>

/* These are the return codes for the seek callbacks */
#define CURL_SEEKFUNC_OK       0
#define CURL_SEEKFUNC_FAIL     1 /* fail the entire transfer */
#define CURL_SEEKFUNC_CANTSEEK 2 /* tell libcurl seeking cannot be done, so
                                    libcurl might try other means instead */

int seek_callback(void *clientp, curl_off_t offset, int origin);

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SEEKFUNCTION, seek_callback);

like, that's the whole vibe right there. the three #defines are your return codes, keep them in your notes, you will need them 🫢


✨ DESCRIPTION ✨

so you pass in a pointer to your callback function, and it has to match the prototype up there β€” int seek_callback(void *clientp, curl_off_t offset, int origin), no improvising πŸ’…

what libcurl does with it: it calls your function to seek to a certain position in the input stream. and the reason you'd want that? it can fast forward a file in a resumed upload β€” instead of reading all the uploaded bytes with the normal read function/callback like some kind of clown. it's also called to rewind a stream when data has already been sent to the server and needs to be sent again 😭 that's the messy situation and you're the one who fixes it.

when does it happen, you ask? this may happen when doing an HTTP PUT or POST with a multi-pass authentication method, or when an existing HTTP connection is reused too late and the server closes the connection. rude of the server but okay.

your function shall work like fseek(3) or lseek(3), and it gets SEEK_SET, SEEK_CUR or SEEK_END as the argument for origin β€” although lowkey libcurl currently only passes SEEK_SET. noted. πŸ“

clientp is the pointer you set with CURLOPT_SEEKDATA(3). that's your stuff, your data, your moment.

and the callback function must return one of these three, no cap:

  • CURL_SEEKFUNC_OK β€” it worked, slay πŸŽ‰
  • CURL_SEEKFUNC_FAIL β€” this causes the upload operation to fail, periodt πŸ’€
  • CURL_SEEKFUNC_CANTSEEK β€” this one says "okay the seek failed, BUT libcurl is free to work around the problem if possible". the latter can sometimes be done by instead reading from the input or similar. so it's the graceful-exit option, we love her 🌈
🚨 real talk: if you forward the input arguments directly to fseek(3) or lseek(3), please note that the data type for offset is not the same as defined for curl_off_t on many systems. do not just yeet the values across, check your types bestie 🫢

πŸ¦‹ DEFAULT πŸ¦‹

NULL

(so nothing, nobody, no callback β€” just the void πŸ™ˆ)


🌼 PROTOCOLS 🌼

this functionality affects ftp, http and sftp


πŸ’– EXAMPLE πŸ’–

here's a whole little main character example, copy it, it's yours:

#include <unistd.h> /* for lseek */

struct data {
  int our_fd;
};
static int seek_cb(void *clientp, curl_off_t offset, int origin)
{
  struct data *d = (struct data *)clientp;
  lseek(d->our_fd, offset, origin);
  return CURL_SEEKFUNC_OK;
}

int main(void)
{
  struct data seek_data;
  CURL *curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, seek_cb);
    curl_easy_setopt(curl, CURLOPT_SEEKDATA, &seek_data);
  }
}

see how seek_cb just returns CURL_SEEKFUNC_OK? confident. decisive. we stan 😌✨


πŸ‘‘ AVAILABILITY πŸ‘‘

Added in curl 7.18.0 β€” vintage but still thriving, iykyk 🧚


πŸ“ RETURN VALUE πŸ“

curl_easy_setopt(3) returns a CURLcode indicating success or error.

CURLE_OK (that's 0) means everything was OK. anything non-zero means an error occurred β€” see libcurl-errors(3) for the tea on that πŸ’…


⭐ SEE ALSO ⭐

like the whole friend group. go read them too fr fr 🌸


and that's the page bestie!! this one's brought to you by libcurl, dated 2026-10-12, and yes that's the same CURLOPT_SEEKFUNCTION(3) you came in with β€” she's just a little cuter now πŸ’œβœ¨πŸ¦‹

πŸ“¦ where this page comes from