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 tofseek(3)orlseek(3), please note that the data type for offset is not the same as defined forcurl_off_ton 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
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms