Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
download.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
37
39#ifdef FUN_WITH_CURL
40 Value vpath = pop_value(vm);
41 Value vurl = pop_value(vm);
42 char *url = value_to_string_alloc(&vurl);
44 free_value(vurl);
46 if (!url || !path) {
47 if (url) free(url);
48 if (path) free(path);
49 push_value(vm, make_int(0));
50 break;
51 }
52 FILE *fp = fopen(path, "wb");
53 if (!fp) {
54 free(url);
55 free(path);
56 push_value(vm, make_int(0));
57 break;
58 }
59 CURL *h = curl_easy_init();
60 if (!h) {
61 fclose(fp);
62 free(url);
63 free(path);
64 push_value(vm, make_int(0));
65 break;
66 }
67 curl_easy_setopt(h, CURLOPT_URL, url);
68 curl_easy_setopt(h, CURLOPT_FOLLOWLOCATION, 1L);
69 curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, fun_curl_file_write_cb);
70 curl_easy_setopt(h, CURLOPT_WRITEDATA, fp);
71 CURLcode rc = curl_easy_perform(h);
72 curl_easy_cleanup(h);
73 fclose(fp);
74 free(url);
75 free(path);
76 if (rc != CURLE_OK) {
77 push_value(vm, make_int(0));
78 break;
79 }
80 push_value(vm, make_int(1));
81#else
82 Value a = pop_value(vm);
84 Value b = pop_value(vm);
87#endif
88 break;
89}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_CURL_DOWNLOAD
Definition bytecode.h:175
free_value(a)
push_value(vm, make_int(0))
int rc
free(vals)
fclose(f)
Tagged union representing a Fun value.
Definition value.h:68
void vpath
Definition stubs.c:23
vm fp
Definition throw.c:57
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value path
Definition write_file.c:33