curl-curl/docs/examples
Viktor Szakats 193cb00ce9
build: stop overriding standard memory allocation functions
Before this patch curl used the C preprocessor to override standard
memory allocation symbols: malloc, calloc, strdup, realloc, free.
The goal of these is to replace them with curl's debug wrappers in
`CURLDEBUG` builds, another was to replace them with the wrappers
calling user-defined allocators in libcurl. This solution needed a bunch
of workarounds to avoid breaking external headers: it relied on include
order to do the overriding last. For "unity" builds it needed to reset
overrides before external includes. Also in test apps, which are always
built as single source files. It also needed the `(symbol)` trick
to avoid overrides in some places. This would still not fix cases where
the standard symbols were macros. It was also fragile and difficult
to figure out which was the actual function behind an alloc or free call
in a specific piece of code. This in turn caused bugs where the wrong
allocator was accidentally called.

To avoid these problems, this patch replaces this solution with
`curlx_`-prefixed allocator macros, and mapping them _once_ to either
the libcurl wrappers, the debug wrappers or the standard ones, matching
the rest of the code in libtests.

This concludes the long journey to avoid redefining standard functions
in the curl codebase.

Note: I did not update `packages/OS400/*.c` sources. They did not
`#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning
the overrides were never applied to them. This may or may not have been
correct. For now I suppressed the direct use of standard allocators
via a local `.checksrc`. Probably they (except for `curlcl.c`) should be
updated to include `curl_setup.h` and use the `curlx_` macros.

This patch changes mappings in two places:
- `lib/curl_threads.c` in libtests: Before this patch it mapped to
  libcurl allocators. After, it maps to standard allocators, like
  the rest of libtests code.
- `units`: before this patch it mapped to standard allocators. After, it
  maps to libcurl allocators.

Also:
- drop all position-dependent `curl_memory.h` and `memdebug.h` includes,
  and delete the now unnecessary headers.
- rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other
  allocators.
- map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`).
  To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`.
- multibyte: map `curlx_convert_*()` to `_strdup()` on Windows
  (was: `strdup()`).
- src: do not reuse the `strdup` name for the local replacement.
- lib509: call `_strdup()` on Windows (was: `strdup()`).
- test1132: delete test obsoleted by this patch.
- CHECKSRC.md: update text for `SNPRINTF`.
- checksrc: ban standard allocator symbols.

Follow-up to b12da22db1 #18866
Follow-up to db98daab05 #18844
Follow-up to 4deea9396b #18814
Follow-up to 9678ff5b1b #18776
Follow-up to 10bac43b87 #18774
Follow-up to 20142f5d06 #18634
Follow-up to bf7375ecc5 #18503
Follow-up to 9863599d69 #18502
Follow-up to 3bb5e58c10 #17827

Closes #19626
2025-11-28 10:44:26 +01:00
..
.checksrc build: stop overriding standard memory allocation functions 2025-11-28 10:44:26 +01:00
.gitignore examples: add an example for logging failed transfers 2025-10-09 14:40:09 -04:00
10-at-a-time.c examples: consistent variable naming across examples 2025-10-31 16:44:57 +01:00
adddocsref.pl scripts: enable strict warnings in Perl where missing, fix fallouts 2025-07-27 22:35:18 +02:00
address-scope.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
altsvc.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
anyauthput.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
block_ip.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
cacertinmem.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
certinfo.c examples: consistent variable naming across examples 2025-10-31 16:44:57 +01:00
chkspeed.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
CMakeLists.txt build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
connect-to.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
cookie_interface.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
crawler.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
debug.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
default-scheme.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
ephiperfifo.c badwords: check indented lines in source code, fix fallouts 2025-11-15 13:25:02 +01:00
evhiperfifo.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
externalsocket.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
fileupload.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftp-delete.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
ftp-wildcard.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftpget.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftpgetinfo.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftpgetresp.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftpsget.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftpupload.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
ftpuploadfrommem.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
ftpuploadresume.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
getinfo.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
getinmemory.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
getredirect.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
getreferrer.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
ghiper.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
headerapi.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
hiperfifo.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
hsts-preload.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
htmltidy.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
htmltitle.cpp examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http2-download.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http2-pushinmemory.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http2-serverpush.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http2-upload.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http3-present.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http3.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http-options.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
http-post.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
httpcustomheader.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
httpput-postfields.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
httpput.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
https.c examples: fix minor typo 2025-11-25 08:58:03 +01:00
imap-append.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-authzid.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-copy.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-create.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-delete.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-examine.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-fetch.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-list.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-lsub.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-multi.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-noop.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-search.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-ssl.c examples: fix minor typo 2025-11-25 08:58:03 +01:00
imap-store.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
imap-tls.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
interface.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
ipv6.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
keepalive.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
localport.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
log_failed_transfers.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
Makefile.am cmake: support building some complicated examples, build them in CI 2025-10-07 19:09:02 +02:00
Makefile.example Makefile.example: make default options more likely to work 2025-10-20 13:52:58 +02:00
Makefile.inc examples: add an example for logging failed transfers 2025-10-09 14:40:09 -04:00
maxconnects.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-app.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-debugcallback.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-double.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-event.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-formadd.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-legacy.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-post.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-single.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
multi-uv.c examples/multi-uv: simplify passing uv struct 2025-11-26 13:19:36 +01:00
multithread.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
netrc.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
parseurl.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
persistent.c examples: improve global init, error checks and returning errors 2025-10-13 23:02:05 +02:00
pop3-authzid.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-dele.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-list.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-multi.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-noop.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-retr.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-ssl.c examples: fix minor typo 2025-11-25 08:58:03 +01:00
pop3-stat.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-tls.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-top.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
pop3-uidl.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
post-callback.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
postinmemory.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
postit2-formadd.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
postit2.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
progressfunc.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
protofeats.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
range.c examples: improve global init, error checks and returning errors 2025-10-13 23:02:05 +02:00
README.md GHA: silence proselint warnings and an error 2024-10-15 16:44:17 +02:00
resolve.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
rtsp-options.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
sendrecv.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
sepheaders.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
sessioninfo.c examples: consistent variable naming across examples 2025-10-31 16:44:57 +01:00
sftpget.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
sftpuploadresume.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
shared-connection-cache.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
simple.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
simplepost.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
simplessl.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
smooth-gtk-thread.c examples: make functions/data static where missing 2025-11-18 01:05:14 +01:00
smtp-authzid.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
smtp-expn.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
smtp-mail.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
smtp-mime.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
smtp-multi.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
smtp-ssl.c examples: fix minor typo 2025-11-25 08:58:03 +01:00
smtp-tls.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
smtp-vrfy.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00
sslbackend.c docs: use CURLSSLBACKEND_NONE 2023-09-21 14:15:03 +02:00
synctime.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
threaded-ssl.c badwords: check indented lines in source code, fix fallouts 2025-11-15 13:25:02 +01:00
unixsocket.c examples: improve global init, error checks and returning errors 2025-10-13 23:02:05 +02:00
url2file.c build: tidy-up MSVC CRT warning suppression macros 2025-11-18 00:49:26 +01:00
urlapi.c badwords: check indented lines in source code, fix fallouts 2025-11-15 13:25:02 +01:00
usercertinmem.c examples: consistent variable naming across examples 2025-10-31 16:44:57 +01:00
version-check.pl scripts: enable strict warnings in Perl where missing, fix fallouts 2025-07-27 22:35:18 +02:00
websocket-cb.c examples: consistent variable naming across examples 2025-10-31 16:44:57 +01:00
websocket-updown.c tidy-up: result code variable names in tests and examples 2025-11-14 01:47:12 +01:00
websocket.c examples/websocket: fix use of uninitialized rlen 2025-10-17 10:15:07 +02:00
xmlstream.c examples: tidy-up headers and includes 2025-11-18 02:05:07 +01:00

libcurl examples

This directory is for libcurl programming examples. They are meant to show some simple steps on how you can build your own application to take full advantage of libcurl.

If you end up with other small but still useful example sources, please mail them for submission in future packages and on the website.

Building

The Makefile.example is an example Makefile that could be used to build these examples. Just edit the file according to your system and requirements first.

Most examples should build fine using a command line like this:

`curl-config --cc --cflags --libs` -o example-my example.c

Some compilers do not like having the arguments in this order but instead want you do reorganize them like:

`curl-config --cc` -o example-my example.c `curl-config --cflags --libs`

Please do not use the curl.se site as a test target for your libcurl applications/experiments. Even if some of the examples use that site as a URL at some places, it does not mean that the URLs work or that we expect you to actually torture our website with your tests. Thanks.

Examples

Each example source code file is designed to be and work stand-alone and rather self-explanatory. The examples may at times lack the level of error checks you need in a real world, but that is then only for the sake of readability: to make the code smaller and easier to follow.