summaryrefslogtreecommitdiff
path: root/docs/examples/fopen.c
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/fopen.c')
-rw-r--r--docs/examples/fopen.c79
1 files changed, 40 insertions, 39 deletions
diff --git a/docs/examples/fopen.c b/docs/examples/fopen.c
index 1e0a09a0a..5a7e6ad5b 100644
--- a/docs/examples/fopen.c
+++ b/docs/examples/fopen.c
@@ -13,7 +13,7 @@
* See the main() function at the bottom that shows an app that retrieves from
* a specified url using fgets() and fread() and saves as two output files.
*
- * Copyright (c) 2003 Simtec Electronics
+ * Copyright (c) 2003, 2017 Simtec Electronics
*
* Re-implemented by Vincent Sanders <vince@kyllikki.org> with extensive
* reference to original curl example code
@@ -58,9 +58,9 @@
#include <curl/curl.h>
enum fcurl_type_e {
- CFTYPE_NONE=0,
- CFTYPE_FILE=1,
- CFTYPE_CURL=2
+ CFTYPE_NONE = 0,
+ CFTYPE_FILE = 1,
+ CFTYPE_CURL = 2
};
struct fcurl_data
@@ -102,19 +102,19 @@ static size_t write_callback(char *buffer,
URL_FILE *url = (URL_FILE *)userp;
size *= nitems;
- rembuff=url->buffer_len - url->buffer_pos; /* remaining space in buffer */
+ rembuff = url->buffer_len - url->buffer_pos; /* remaining space in buffer */
if(size > rembuff) {
/* not enough space in buffer */
- newbuff=realloc(url->buffer, url->buffer_len + (size - rembuff));
- if(newbuff==NULL) {
+ newbuff = realloc(url->buffer, url->buffer_len + (size - rembuff));
+ if(newbuff == NULL) {
fprintf(stderr, "callback buffer grow failed\n");
- size=rembuff;
+ size = rembuff;
}
else {
/* realloc succeeded increase buffer size*/
- url->buffer_len+=size - rembuff;
- url->buffer=newbuff;
+ url->buffer_len += size - rembuff;
+ url->buffer = newbuff;
}
}
@@ -211,12 +211,12 @@ static int fill_buffer(URL_FILE *file, size_t want)
static int use_buffer(URL_FILE *file, size_t want)
{
/* sort out buffer */
- if((file->buffer_pos - want) <=0) {
+ if((file->buffer_pos - want) <= 0) {
/* ditch buffer - write will recreate */
free(file->buffer);
- file->buffer=NULL;
- file->buffer_pos=0;
- file->buffer_len=0;
+ file->buffer = NULL;
+ file->buffer_pos = 0;
+ file->buffer_len = 0;
}
else {
/* move rest down make it available for later */
@@ -243,7 +243,8 @@ URL_FILE *url_fopen(const char *url, const char *operation)
memset(file, 0, sizeof(URL_FILE));
- if((file->handle.file=fopen(url, operation)))
+ file->handle.file = fopen(url, operation);
+ if(file->handle.file)
file->type = CFTYPE_FILE; /* marked as URL */
else {
@@ -282,11 +283,11 @@ URL_FILE *url_fopen(const char *url, const char *operation)
int url_fclose(URL_FILE *file)
{
- int ret=0;/* default is good return */
+ int ret = 0;/* default is good return */
switch(file->type) {
case CFTYPE_FILE:
- ret=fclose(file->handle.file); /* passthrough */
+ ret = fclose(file->handle.file); /* passthrough */
break;
case CFTYPE_CURL:
@@ -298,8 +299,8 @@ int url_fclose(URL_FILE *file)
break;
default: /* unknown or supported type - oh dear */
- ret=EOF;
- errno=EBADF;
+ ret = EOF;
+ errno = EBADF;
break;
}
@@ -311,11 +312,11 @@ int url_fclose(URL_FILE *file)
int url_feof(URL_FILE *file)
{
- int ret=0;
+ int ret = 0;
switch(file->type) {
case CFTYPE_FILE:
- ret=feof(file->handle.file);
+ ret = feof(file->handle.file);
break;
case CFTYPE_CURL:
@@ -324,8 +325,8 @@ int url_feof(URL_FILE *file)
break;
default: /* unknown or supported type - oh dear */
- ret=-1;
- errno=EBADF;
+ ret = -1;
+ errno = EBADF;
break;
}
return ret;
@@ -337,7 +338,7 @@ size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
switch(file->type) {
case CFTYPE_FILE:
- want=fread(ptr, size, nmemb, file->handle.file);
+ want = fread(ptr, size, nmemb, file->handle.file);
break;
case CFTYPE_CURL:
@@ -363,8 +364,8 @@ size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
break;
default: /* unknown or supported type - oh dear */
- want=0;
- errno=EBADF;
+ want = 0;
+ errno = EBADF;
break;
}
@@ -395,24 +396,24 @@ char *url_fgets(char *ptr, size_t size, URL_FILE *file)
/*buffer contains data */
/* look for newline or eof */
- for(loop=0;loop < want;loop++) {
+ for(loop = 0;loop < want;loop++) {
if(file->buffer[loop] == '\n') {
- want=loop+1;/* include newline */
+ want = loop+1;/* include newline */
break;
}
}
/* xfer data to caller */
memcpy(ptr, file->buffer, want);
- ptr[want]=0;/* always null terminate */
+ ptr[want] = 0;/* always null terminate */
use_buffer(file, want);
break;
default: /* unknown or supported type - oh dear */
- ptr=NULL;
- errno=EBADF;
+ ptr = NULL;
+ errno = EBADF;
break;
}
@@ -435,9 +436,9 @@ void url_rewind(URL_FILE *file)
/* ditch buffer - write will recreate - resets stream pos*/
free(file->buffer);
- file->buffer=NULL;
- file->buffer_pos=0;
- file->buffer_len=0;
+ file->buffer = NULL;
+ file->buffer_pos = 0;
+ file->buffer_len = 0;
break;
@@ -463,12 +464,12 @@ int main(int argc, char *argv[])
const char *url;
if(argc < 2)
- url="http://192.168.7.3/testfile";/* default to testurl */
+ url = "http://192.168.7.3/testfile";/* default to testurl */
else
- url=argv[1];/* use passed url */
+ url = argv[1];/* use passed url */
/* copy from url line by line with fgets */
- outf=fopen(FGETSFILE, "wb+");
+ outf = fopen(FGETSFILE, "wb+");
if(!outf) {
perror("couldn't open fgets output file\n");
return 1;
@@ -492,7 +493,7 @@ int main(int argc, char *argv[])
/* Copy from url with fread */
- outf=fopen(FREADFILE, "wb+");
+ outf = fopen(FREADFILE, "wb+");
if(!outf) {
perror("couldn't open fread output file\n");
return 1;
@@ -516,7 +517,7 @@ int main(int argc, char *argv[])
/* Test rewind */
- outf=fopen(REWINDFILE, "wb+");
+ outf = fopen(REWINDFILE, "wb+");
if(!outf) {
perror("couldn't open fread output file\n");
return 1;