mirror of
https://github.com/curl/curl.git
synced 2026-08-25 07:43:32 +03:00
tidy-up: use more static, sizeof(), char[], double-const
- make `const` data `static`, where missing and possible. - replace `strlen()` on literal or const strings with `sizeof()`. While the latter is optimized by popular C compiler, e.g. MSVC only does it with `/O2`. - replace magic numbers with `sizeof()`, where missing. - introduce `CURL_CSTRLEN()` macro for `sizeof(char[]) - 1`. - use `CURL_CSTRLEN()` macro. - move `const` before integer types, where missing. - replace `char *var` with `var[]`, where missing and possible. - use double const, where missing. `static const char *` -> `static const char * const`. - lib1514: constify pointers. - unit3205: drop redundant cast, avoid another one. - unit1666: map `OID()` macro to identical `STRCONST()`. Closes #22406
This commit is contained in:
parent
573a6ec16b
commit
e1450d8fda
117 changed files with 311 additions and 287 deletions
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
#include <curl/curl.h>
|
||||
|
||||
static const char *urls[] = {
|
||||
static const char * const urls[] = {
|
||||
"https://01.example/",
|
||||
"https://02.example/",
|
||||
"https://03.example/",
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ static int max_total = 20000;
|
|||
static int max_requests = 500;
|
||||
static size_t max_link_per_page = 5;
|
||||
static int follow_relative_links = 0;
|
||||
static const char *start_page = "https://www.reuters.com/";
|
||||
static const char start_page[] = "https://www.reuters.com/";
|
||||
|
||||
static int pending_interrupt = 0;
|
||||
static void sighandler(int dummy)
|
||||
|
|
|
|||
|
|
@ -303,7 +303,9 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
|
|||
{
|
||||
struct GlobalInfo *g = (struct GlobalInfo *)cbp;
|
||||
struct SockInfo *fdp = (struct SockInfo *)sockp;
|
||||
const char *whatstr[] = { "none", "IN", "OUT", "INOUT", "REMOVE" };
|
||||
static const char * const whatstr[] = {
|
||||
"none", "IN", "OUT", "INOUT", "REMOVE"
|
||||
};
|
||||
|
||||
fprintf(MSG_OUT, "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
|
||||
if(what == CURL_POLL_REMOVE) {
|
||||
|
|
@ -407,7 +409,7 @@ static void fifo_cb(struct GlobalInfo *g, int revents)
|
|||
static int init_fifo(struct GlobalInfo *g)
|
||||
{
|
||||
struct stat st;
|
||||
static const char *fifo = "hiper.fifo";
|
||||
static const char fifo[] = "hiper.fifo";
|
||||
curl_socket_t sockfd;
|
||||
struct epoll_event epev;
|
||||
|
||||
|
|
|
|||
|
|
@ -267,7 +267,9 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
|
|||
{
|
||||
struct GlobalInfo *g = (struct GlobalInfo *)cbp;
|
||||
struct SockInfo *fdp = (struct SockInfo *)sockp;
|
||||
const char *whatstr[] = { "none", "IN", "OUT", "INOUT", "REMOVE" };
|
||||
static const char * const whatstr[] = {
|
||||
"none", "IN", "OUT", "INOUT", "REMOVE"
|
||||
};
|
||||
|
||||
printf("%s e %p s %d what %d cbp %p sockp %p\n",
|
||||
__PRETTY_FUNCTION__, e, s, what, cbp, sockp);
|
||||
|
|
@ -378,7 +380,7 @@ static void fifo_cb(EV_P_ struct ev_io *w, int revents)
|
|||
static int init_fifo(struct GlobalInfo *g)
|
||||
{
|
||||
struct stat st;
|
||||
static const char *fifo = "hiper.fifo";
|
||||
static const char fifo[] = "hiper.fifo";
|
||||
curl_socket_t sockfd;
|
||||
|
||||
fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo);
|
||||
|
|
|
|||
|
|
@ -258,7 +258,9 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
|
|||
{
|
||||
struct GlobalInfo *g = (struct GlobalInfo *)cbp;
|
||||
struct SockInfo *fdp = (struct SockInfo *)sockp;
|
||||
static const char *whatstr[] = { "none", "IN", "OUT", "INOUT", "REMOVE" };
|
||||
static const char * const whatstr[] = {
|
||||
"none", "IN", "OUT", "INOUT", "REMOVE"
|
||||
};
|
||||
|
||||
MSG_OUT("socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
|
||||
if(what == CURL_POLL_REMOVE) {
|
||||
|
|
@ -395,8 +397,8 @@ static gboolean fifo_cb(GIOChannel *ch, GIOCondition condition, gpointer data)
|
|||
|
||||
int init_fifo(void)
|
||||
{
|
||||
static const char fifo[] = "hiper.fifo";
|
||||
struct stat st;
|
||||
const char *fifo = "hiper.fifo";
|
||||
int socket;
|
||||
|
||||
if(!lstat(fifo, &st)) {
|
||||
|
|
|
|||
|
|
@ -270,7 +270,9 @@ static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
|
|||
{
|
||||
struct GlobalInfo *g = (struct GlobalInfo *)cbp;
|
||||
struct SockInfo *fdp = (struct SockInfo *)sockp;
|
||||
const char *whatstr[] = { "none", "IN", "OUT", "INOUT", "REMOVE" };
|
||||
static const char * const whatstr[] = {
|
||||
"none", "IN", "OUT", "INOUT", "REMOVE"
|
||||
};
|
||||
|
||||
fprintf(MSG_OUT, "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]);
|
||||
if(what == CURL_POLL_REMOVE) {
|
||||
|
|
@ -376,7 +378,7 @@ static void fifo_cb(int fd, short event, void *arg)
|
|||
}
|
||||
|
||||
/* Create a named pipe and tell libevent to monitor it */
|
||||
static const char *fifo = "hiper.fifo";
|
||||
static const char fifo[] = "hiper.fifo";
|
||||
static int init_fifo(struct GlobalInfo *g)
|
||||
{
|
||||
struct stat st;
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@
|
|||
#define TO "<addressee@example.net>"
|
||||
#define CC "<info@example.org>"
|
||||
|
||||
static const char *payload_text =
|
||||
static const char payload_text[] =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO "\r\n"
|
||||
"From: " FROM "(Example User)\r\n"
|
||||
|
|
@ -111,7 +111,7 @@ int main(void)
|
|||
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
|
||||
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
||||
|
||||
filesize = strlen(payload_text);
|
||||
filesize = sizeof(payload_text) - 1;
|
||||
if(filesize <= LONG_MAX)
|
||||
infilesize = (long)filesize;
|
||||
curl_easy_setopt(curl, CURLOPT_INFILESIZE, infilesize);
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ int main(void)
|
|||
|
||||
curl = curl_easy_init();
|
||||
if(curl) {
|
||||
const char *urls[] = {
|
||||
static const char * const urls[] = {
|
||||
"https://example.com/",
|
||||
"https://curl.se/",
|
||||
"https://www.example/",
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ int main(void)
|
|||
CURL *curl;
|
||||
CURLcode result;
|
||||
struct MemoryStruct chunk;
|
||||
static const char *postthis = "Field=1&Field=2&Field=3";
|
||||
static const char postthis[] = "Field=1&Field=2&Field=3";
|
||||
|
||||
result = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if(result != CURLE_OK)
|
||||
|
|
@ -87,7 +87,7 @@ int main(void)
|
|||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
|
||||
|
||||
/* if we do not provide POSTFIELDSIZE, libcurl calls strlen() by itself */
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)sizeof(postthis) - 1);
|
||||
|
||||
/* Perform the request, result gets the return code */
|
||||
result = curl_easy_perform(curl);
|
||||
|
|
|
|||
|
|
@ -77,8 +77,8 @@ int main(void)
|
|||
{
|
||||
CURL *curl;
|
||||
/* Minimalistic http request */
|
||||
const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
|
||||
size_t request_len = strlen(request);
|
||||
static const char request[] = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
|
||||
static const size_t request_len = sizeof(request) - 1;
|
||||
|
||||
CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if(result != CURLE_OK)
|
||||
|
|
|
|||
|
|
@ -53,9 +53,9 @@ int main(void)
|
|||
/* init the curl session */
|
||||
curl = curl_easy_init();
|
||||
if(curl) {
|
||||
static const char *headerfilename = "head.out";
|
||||
static const char headerfilename[] = "head.out";
|
||||
FILE *headerfile;
|
||||
static const char *bodyfilename = "body.out";
|
||||
static const char bodyfilename[] = "body.out";
|
||||
FILE *bodyfile;
|
||||
|
||||
/* set URL to get */
|
||||
|
|
|
|||
|
|
@ -134,8 +134,8 @@ int main(void)
|
|||
|
||||
curl = curl_easy_init();
|
||||
if(curl) {
|
||||
const char *remote = "sftp://user:pass@example.com/path/filename";
|
||||
const char *filename = "filename";
|
||||
static const char remote[] = "sftp://user:pass@example.com/path/filename";
|
||||
static const char filename[] = "filename";
|
||||
|
||||
if(!sftpResumeUpload(curl, remote, filename)) {
|
||||
printf("resumed upload using curl %s failed\n", curl_version());
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@
|
|||
|
||||
int main(void)
|
||||
{
|
||||
static const char *postthis = "moo mooo moo moo";
|
||||
static const char postthis[] = "moo mooo moo moo";
|
||||
|
||||
CURL *curl;
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ int main(void)
|
|||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis);
|
||||
|
||||
/* if we do not provide POSTFIELDSIZE, libcurl calls strlen() by itself */
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis));
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)sizeof(postthis) - 1);
|
||||
|
||||
/* Perform the request, result gets the return code */
|
||||
result = curl_easy_perform(curl);
|
||||
|
|
|
|||
|
|
@ -55,9 +55,9 @@ int main(void)
|
|||
FILE *headerfile;
|
||||
const char *pPassphrase = NULL;
|
||||
|
||||
static const char *pCertFile = "testcert.pem";
|
||||
static const char *pCACertFile = "cacert.pem";
|
||||
static const char *pHeaderFile = "dumpit";
|
||||
static const char pCertFile[] = "testcert.pem";
|
||||
static const char pCACertFile[] = "cacert.pem";
|
||||
static const char pHeaderFile[] = "dumpit";
|
||||
|
||||
const char *pKeyName;
|
||||
const char *pKeyType;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@
|
|||
#define SENDER_MAIL "Kurt " SENDER_ADDR
|
||||
#define TO_MAIL "A Receiver " TO_ADDR
|
||||
|
||||
static const char *payload_text =
|
||||
static const char payload_text[] =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO_MAIL "\r\n"
|
||||
"From: " FROM_MAIL "\r\n"
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@
|
|||
#define TO_MAIL "A Receiver " TO_ADDR
|
||||
#define CC_MAIL "John CC Smith " CC_ADDR
|
||||
|
||||
static const char *payload_text =
|
||||
static const char payload_text[] =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO_MAIL "\r\n"
|
||||
"From: " FROM_MAIL "\r\n"
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@
|
|||
#define TO "<addressee@example.net>"
|
||||
#define CC "<info@example.org>"
|
||||
|
||||
static const char *headers_text[] = {
|
||||
static const char * const headers_text[] = {
|
||||
"Date: Tue, 22 Aug 2017 14:08:43 +0100",
|
||||
"To: " TO,
|
||||
"From: " FROM " (Example User)",
|
||||
|
|
@ -82,7 +82,7 @@ int main(void)
|
|||
curl_mime *mime;
|
||||
curl_mime *alt;
|
||||
curl_mimepart *part;
|
||||
const char **cpp;
|
||||
const char * const *cpp;
|
||||
|
||||
/* This is the URL for your mailserver */
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com");
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@
|
|||
#define TO_MAIL "<recipient@example.com>"
|
||||
#define CC_MAIL "<info@example.com>"
|
||||
|
||||
static const char *payload_text =
|
||||
static const char payload_text[] =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO_MAIL "\r\n"
|
||||
"From: " FROM_MAIL "\r\n"
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@
|
|||
#define TO_MAIL "<recipient@example.com>"
|
||||
#define CC_MAIL "<info@example.com>"
|
||||
|
||||
static const char *payload_text =
|
||||
static const char payload_text[] =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO_MAIL "\r\n"
|
||||
"From: " FROM_MAIL "\r\n"
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@
|
|||
#define TO_MAIL "<recipient@example.com>"
|
||||
#define CC_MAIL "<info@example.com>"
|
||||
|
||||
static const char *payload_text =
|
||||
static const char payload_text[] =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO_MAIL "\r\n"
|
||||
"From: " FROM_MAIL "\r\n"
|
||||
|
|
|
|||
|
|
@ -99,10 +99,10 @@ static int AutoSyncTime;
|
|||
static SYSTEMTIME SYSTime;
|
||||
static SYSTEMTIME LOCALTime;
|
||||
|
||||
static const char *DayStr[] = {
|
||||
static const char * const DayStr[] = {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
||||
};
|
||||
static const char *MthStr[] = {
|
||||
static const char * const MthStr[] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *stream)
|
|||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
static const char *pagefilename = "page.out";
|
||||
static const char pagefilename[] = "page.out";
|
||||
|
||||
CURLcode result;
|
||||
CURL *curl;
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ int main(int argc, const char *argv[])
|
|||
{
|
||||
CURL *curl;
|
||||
struct read_ctx rctx;
|
||||
const char *payload = "Hello, friend!";
|
||||
static const char payload[] = "Hello, friend!";
|
||||
|
||||
CURLcode result = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if(result != CURLE_OK)
|
||||
|
|
@ -108,7 +108,7 @@ int main(int argc, const char *argv[])
|
|||
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_cb);
|
||||
/* tell curl that we want to send the payload */
|
||||
rctx.curl = curl;
|
||||
rctx.blen = strlen(payload);
|
||||
rctx.blen = sizeof(payload) - 1;
|
||||
memcpy(rctx.buf, payload, rctx.blen);
|
||||
curl_easy_setopt(curl, CURLOPT_READDATA, &rctx);
|
||||
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue