Skip to content

Commit 3a6563d

Browse files
committed
examples: adhere to curl code style
All plain C examples now (mostly) adhere to the curl code style. While they are only examples, they had diverted so much and contained all sorts of different mixed code styles by now. Having them use a unified style helps users and readability. Also, as they get copy-and-pasted widely by users, making sure they're clean and nice is a good idea. 573 checksrc warnings were addressed.
1 parent 936d8f0 commit 3a6563d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+822
-767
lines changed

docs/examples/10-at-a-time.c

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* | (__| |_| | _ <| |___
66
* \___|\___/|_| \_\_____|
77
*
8-
* Copyright (C) 1998 - 2015, Daniel Stenberg, <[email protected]>, et al.
8+
* Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
99
*
1010
* This software is licensed as described in the file COPYING, which
1111
* you should have received as part of this distribution. The terms
@@ -127,50 +127,51 @@ int main(void)
127127
uses */
128128
curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX);
129129

130-
for (C = 0; C < MAX; ++C) {
130+
for(C = 0; C < MAX; ++C) {
131131
init(cm, C);
132132
}
133133

134-
while (U) {
134+
while(U) {
135135
curl_multi_perform(cm, &U);
136136

137-
if (U) {
137+
if(U) {
138138
FD_ZERO(&R);
139139
FD_ZERO(&W);
140140
FD_ZERO(&E);
141141

142-
if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
142+
if(curl_multi_fdset(cm, &R, &W, &E, &M)) {
143143
fprintf(stderr, "E: curl_multi_fdset\n");
144144
return EXIT_FAILURE;
145145
}
146146

147-
if (curl_multi_timeout(cm, &L)) {
147+
if(curl_multi_timeout(cm, &L)) {
148148
fprintf(stderr, "E: curl_multi_timeout\n");
149149
return EXIT_FAILURE;
150150
}
151-
if (L == -1)
151+
if(L == -1)
152152
L = 100;
153153

154-
if (M == -1) {
154+
if(M == -1) {
155155
#ifdef WIN32
156156
Sleep(L);
157157
#else
158158
sleep(L / 1000);
159159
#endif
160-
} else {
160+
}
161+
else {
161162
T.tv_sec = L/1000;
162163
T.tv_usec = (L%1000)*1000;
163164

164-
if (0 > select(M+1, &R, &W, &E, &T)) {
165+
if(0 > select(M+1, &R, &W, &E, &T)) {
165166
fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",
166167
M+1, L, errno, strerror(errno));
167168
return EXIT_FAILURE;
168169
}
169170
}
170171
}
171172

172-
while ((msg = curl_multi_info_read(cm, &Q))) {
173-
if (msg->msg == CURLMSG_DONE) {
173+
while((msg = curl_multi_info_read(cm, &Q))) {
174+
if(msg->msg == CURLMSG_DONE) {
174175
char *url;
175176
CURL *e = msg->easy_handle;
176177
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
@@ -182,7 +183,7 @@ int main(void)
182183
else {
183184
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
184185
}
185-
if (C < CNT) {
186+
if(C < CNT) {
186187
init(cm, C++);
187188
U++; /* just to prevent it from remaining at 0 if there are more
188189
URLs to get */

docs/examples/anyauthput.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* | (__| |_| | _ <| |___
66
* \___|\___/|_| \_\_____|
77
*
8-
* Copyright (C) 1998 - 2015, Daniel Stenberg, <[email protected]>, et al.
8+
* Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
99
*
1010
* This software is licensed as described in the file COPYING, which
1111
* you should have received as part of this distribution. The terms
@@ -119,7 +119,7 @@ int main(int argc, char **argv)
119119
{
120120
CURL *curl;
121121
CURLcode res;
122-
intptr_t hd ;
122+
intptr_t hd;
123123
struct stat file_info;
124124

125125
char *file;
@@ -132,7 +132,7 @@ int main(int argc, char **argv)
132132
url = argv[2];
133133

134134
/* get the file size of the local file */
135-
hd = open(file, O_RDONLY) ;
135+
hd = open(file, O_RDONLY);
136136
fstat(hd, &file_info);
137137

138138
/* In windows, this will init the winsock stuff */
@@ -154,11 +154,11 @@ int main(int argc, char **argv)
154154
curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd);
155155

156156
/* enable "uploading" (which means PUT when doing HTTP) */
157-
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ;
157+
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
158158

159159
/* specify target URL, and note that this URL should also include a file
160160
name, not only a directory (as you can do with GTP uploads) */
161-
curl_easy_setopt(curl,CURLOPT_URL, url);
161+
curl_easy_setopt(curl, CURLOPT_URL, url);
162162

163163
/* and give the size of the upload, this supports large file sizes
164164
on systems that have general support for it */

docs/examples/cacertinmem.c

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* | (__| |_| | _ <| |___
66
* \___|\___/|_| \_\_____|
77
*
8-
* Copyright (C) 1998 - 2015, Daniel Stenberg, <[email protected]>, et al.
8+
* Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
99
*
1010
* This software is licensed as described in the file COPYING, which
1111
* you should have received as part of this distribution. The terms
@@ -30,8 +30,8 @@
3030

3131
size_t writefunction( void *ptr, size_t size, size_t nmemb, void *stream)
3232
{
33-
fwrite(ptr,size,nmemb,stream);
34-
return(nmemb*size);
33+
fwrite(ptr, size, nmemb, stream);
34+
return (nmemb*size);
3535
}
3636

3737
static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm)
@@ -87,22 +87,22 @@ static CURLcode sslctx_function(CURL * curl, void * sslctx, void * parm)
8787
* structure that SSL can use
8888
*/
8989
PEM_read_bio_X509(bio, &cert, 0, NULL);
90-
if (cert == NULL)
90+
if(cert == NULL)
9191
printf("PEM_read_bio_X509 failed...\n");
9292

9393
/* get a pointer to the X509 certificate store (which may be empty!) */
9494
store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
9595

9696
/* add our certificate to this store */
97-
if (X509_STORE_add_cert(store, cert)==0)
97+
if(X509_STORE_add_cert(store, cert)==0)
9898
printf("error adding certificate\n");
9999

100100
/* decrease reference counts */
101101
X509_free(cert);
102102
BIO_free(bio);
103103

104104
/* all set to go */
105-
return CURLE_OK ;
105+
return CURLE_OK;
106106
}
107107

108108
int main(void)
@@ -112,22 +112,22 @@ int main(void)
112112

113113
rv=curl_global_init(CURL_GLOBAL_ALL);
114114
ch=curl_easy_init();
115-
rv=curl_easy_setopt(ch,CURLOPT_VERBOSE, 0L);
116-
rv=curl_easy_setopt(ch,CURLOPT_HEADER, 0L);
117-
rv=curl_easy_setopt(ch,CURLOPT_NOPROGRESS, 1L);
118-
rv=curl_easy_setopt(ch,CURLOPT_NOSIGNAL, 1L);
119-
rv=curl_easy_setopt(ch,CURLOPT_WRITEFUNCTION, *writefunction);
120-
rv=curl_easy_setopt(ch,CURLOPT_WRITEDATA, stdout);
121-
rv=curl_easy_setopt(ch,CURLOPT_HEADERFUNCTION, *writefunction);
122-
rv=curl_easy_setopt(ch,CURLOPT_HEADERDATA, stderr);
123-
rv=curl_easy_setopt(ch,CURLOPT_SSLCERTTYPE,"PEM");
124-
rv=curl_easy_setopt(ch,CURLOPT_SSL_VERIFYPEER,1L);
115+
rv=curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
116+
rv=curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
117+
rv=curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
118+
rv=curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
119+
rv=curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, *writefunction);
120+
rv=curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
121+
rv=curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, *writefunction);
122+
rv=curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
123+
rv=curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
124+
rv=curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
125125
rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
126126

127127
/* first try: retrieve page without cacerts' certificate -> will fail
128128
*/
129129
rv=curl_easy_perform(ch);
130-
if (rv==CURLE_OK)
130+
if(rv==CURLE_OK)
131131
printf("*** transfer succeeded ***\n");
132132
else
133133
printf("*** transfer failed ***\n");
@@ -136,9 +136,9 @@ int main(void)
136136
* load the certificate by installing a function doing the nescessary
137137
* "modifications" to the SSL CONTEXT just before link init
138138
*/
139-
rv=curl_easy_setopt(ch,CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
139+
rv=curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
140140
rv=curl_easy_perform(ch);
141-
if (rv==CURLE_OK)
141+
if(rv==CURLE_OK)
142142
printf("*** transfer succeeded ***\n");
143143
else
144144
printf("*** transfer failed ***\n");

docs/examples/chkspeed.c

+49-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* | (__| |_| | _ <| |___
66
* \___|\___/|_| \_\_____|
77
*
8-
* Copyright (C) 1998 - 2015, Daniel Stenberg, <[email protected]>, et al.
8+
* Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, et al.
99
*
1010
* This software is licensed as described in the file COPYING, which
1111
* you should have received as part of this distribution. The terms
@@ -68,63 +68,78 @@ int main(int argc, char *argv[])
6868
const char *url = URL_1M;
6969
char *appname = argv[0];
7070

71-
if (argc > 1) {
71+
if(argc > 1) {
7272
/* parse input parameters */
73-
for (argc--, argv++; *argv; argc--, argv++) {
74-
if (strncasecmp(*argv, "-", 1) == 0) {
75-
if (strncasecmp(*argv, "-H", 2) == 0) {
73+
for(argc--, argv++; *argv; argc--, argv++) {
74+
if(strncasecmp(*argv, "-", 1) == 0) {
75+
if(strncasecmp(*argv, "-H", 2) == 0) {
7676
fprintf(stderr,
7777
"\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
7878
appname);
7979
exit(1);
80-
} else if (strncasecmp(*argv, "-V", 2) == 0) {
80+
}
81+
else if(strncasecmp(*argv, "-V", 2) == 0) {
8182
fprintf(stderr, "\r%s %s - %s\n",
8283
appname, CHKSPEED_VERSION, curl_version());
8384
exit(1);
84-
} else if (strncasecmp(*argv, "-A", 2) == 0) {
85+
}
86+
else if(strncasecmp(*argv, "-A", 2) == 0) {
8587
prtall = 1;
86-
} else if (strncasecmp(*argv, "-X", 2) == 0) {
88+
}
89+
else if(strncasecmp(*argv, "-X", 2) == 0) {
8790
prtsep = 1;
88-
} else if (strncasecmp(*argv, "-T", 2) == 0) {
91+
}
92+
else if(strncasecmp(*argv, "-T", 2) == 0) {
8993
prttime = 1;
90-
} else if (strncasecmp(*argv, "-M=", 3) == 0) {
94+
}
95+
else if(strncasecmp(*argv, "-M=", 3) == 0) {
9196
long m = strtol((*argv)+3, NULL, 10);
9297
switch(m) {
93-
case 1: url = URL_1M;
94-
break;
95-
case 2: url = URL_2M;
96-
break;
97-
case 5: url = URL_5M;
98-
break;
99-
case 10: url = URL_10M;
100-
break;
101-
case 20: url = URL_20M;
102-
break;
103-
case 50: url = URL_50M;
104-
break;
105-
case 100: url = URL_100M;
106-
break;
107-
default: fprintf(stderr, "\r%s: invalid parameter %s\n",
108-
appname, *argv + 3);
109-
exit(1);
98+
case 1:
99+
url = URL_1M;
100+
break;
101+
case 2:
102+
url = URL_2M;
103+
break;
104+
case 5:
105+
url = URL_5M;
106+
break;
107+
case 10:
108+
url = URL_10M;
109+
break;
110+
case 20:
111+
url = URL_20M;
112+
break;
113+
case 50:
114+
url = URL_50M;
115+
break;
116+
case 100:
117+
url = URL_100M;
118+
break;
119+
default:
120+
fprintf(stderr, "\r%s: invalid parameter %s\n",
121+
appname, *argv + 3);
122+
exit(1);
110123
}
111-
} else {
124+
}
125+
else {
112126
fprintf(stderr, "\r%s: invalid or unknown option %s\n",
113127
appname, *argv);
114128
exit(1);
115129
}
116-
} else {
130+
}
131+
else {
117132
url = *argv;
118133
}
119134
}
120135
}
121136

122137
/* print separator line */
123-
if (prtsep) {
138+
if(prtsep) {
124139
printf("-------------------------------------------------\n");
125140
}
126141
/* print localtime */
127-
if (prttime) {
142+
if(prttime) {
128143
time_t t = time(NULL);
129144
printf("Localtime: %s", ctime(&t));
130145
}
@@ -167,7 +182,7 @@ int main(int argc, char *argv[])
167182
if((CURLE_OK == res) && (val>0))
168183
printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
169184

170-
if (prtall) {
185+
if(prtall) {
171186
/* check for name resolution time */
172187
res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME, &val);
173188
if((CURLE_OK == res) && (val>0))
@@ -178,8 +193,8 @@ int main(int argc, char *argv[])
178193
if((CURLE_OK == res) && (val>0))
179194
printf("Connect time: %0.3f sec.\n", val);
180195
}
181-
182-
} else {
196+
}
197+
else {
183198
fprintf(stderr, "Error while fetching '%s' : %s\n",
184199
url, curl_easy_strerror(res));
185200
}

0 commit comments

Comments
 (0)