Solved Why FreeBSD secret tricks?

Just added a visit to Ural mountains to my personal bucket list. But my flight itinerary will avoid SVO, precisely because of how Vladimir/Vovochka runs things around there.
 
Here's a secret trick although more platform independent.

Type this in your command prompt and it'll generate source code enabling the use of the cURL library:

curl http://example.com --libcurl example.c

It'll give you code that looks like this:


C:
/********* Sample code generated by the curl command line tool **********
* All curl_easy_setopt() options are documented at:
* https://curl.se/libcurl/c/curl_easy_setopt.html
************************************************************************/
#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(hnd, CURLOPT_URL, "http://example.com");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.76.0");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
  curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

  /* Here is a list of options the curl code used that cannot get generated
     as source easily. You may select to either not use them or implement
     them yourself.

  CURLOPT_WRITEDATA set to a objectpointer
  CURLOPT_INTERLEAVEDATA set to a objectpointer
  CURLOPT_WRITEFUNCTION set to a functionpointer
  CURLOPT_READDATA set to a objectpointer
  CURLOPT_READFUNCTION set to a functionpointer
  CURLOPT_SEEKDATA set to a objectpointer
  CURLOPT_SEEKFUNCTION set to a functionpointer
  CURLOPT_ERRORBUFFER set to a objectpointer
  CURLOPT_STDERR set to a objectpointer
  CURLOPT_HEADERFUNCTION set to a functionpointer
  CURLOPT_HEADERDATA set to a objectpointer

  */

  ret = curl_easy_perform(hnd);

  curl_easy_cleanup(hnd);
  hnd = NULL;

  return (int)ret;
}
/**** End of sample code ****/

You can modify it to output to a string instead of stdout like this:

This will fetch JSON data for Boston Weather and put it into a string.


C:
/********* Sample code generated by the curl command line tool **********
* All curl_easy_setopt() options are documented at:
* https://curl.se/libcurl/c/curl_easy_setopt.html
*
* compile dynamically: gcc -o example -lcurl example.c
* compile dynamically: clang `pkg-config --cflags libcurl` -o example example.c `pkg-config --libs libcurl`
* compile statically: gcc -o example example.c -lcurl -lssl -lcrypto -ldl -lm -lz -DCURL_STATICLIB
* compile statically: clang `pkg-config --cflags libcurl` -o example example.c `pkg-config --libs libcurl` -lssl -lcrypto -ldl -lm -lz -DCURL_STATICLIB
* ************************************************************************/
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    size_t size;
    char *memory;
}MemType;


size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata){
    //cURL repeatedly calls this function until it runs out of data from the datastream
    size_t realsize = size * nmemb;

    MemType *mem = (MemType *)userdata;
    char *tmp = realloc(mem->memory, mem->size + realsize + 1);

    if (tmp == NULL){
        printf("Not Enough Memory, realloc return NULL.\n");
        return 0;
    }

    mem->memory = tmp;
    memcpy(&(mem->memory[mem->size]), ptr, realsize); // Overwrite the last element @ this address with the first byte @ address ptr, the number of bytes = realsize.
    mem->size += realsize;
    mem->memory[mem->size] = 0; // The array size is one byte larger than mem->size, however, mem->size will give us the location of the last element [one char = one byte], strings need to terminate with NULL character, which isn't included in the *ptr stream.

    return realsize;
}

int main(int argc, char *argv[])
{
  CURLcode ret;
  CURL *hnd;
  MemType output;
  output.memory = malloc(1);
  output.size = 0;

  hnd = curl_easy_init();
  curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(hnd, CURLOPT_URL, "https://api.weather.gov/stations/KBOS/observations/latest");
  curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.76.0");
  curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
  curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
  curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_callback);
  curl_easy_setopt(hnd, CURLOPT_WRITEDATA, (void *)&output);

  /* Here is a list of options the curl code used that cannot get generated
     as source easily. You may select to either not use them or implement
     them yourself.

  CURLOPT_WRITEDATA set to a objectpointer
  CURLOPT_INTERLEAVEDATA set to a objectpointer
  CURLOPT_WRITEFUNCTION set to a functionpointer
  CURLOPT_READDATA set to a objectpointer
  CURLOPT_READFUNCTION set to a functionpointer
  CURLOPT_SEEKDATA set to a objectpointer
  CURLOPT_SEEKFUNCTION set to a functionpointer
  CURLOPT_ERRORBUFFER set to a objectpointer
  CURLOPT_STDERR set to a objectpointer
  CURLOPT_HEADERFUNCTION set to a functionpointer
  CURLOPT_HEADERDATA set to a objectpointer

  */

  ret = curl_easy_perform(hnd);

  printf("%s",output.memory);

  curl_easy_cleanup(hnd);
  hnd = NULL;

  return (int)ret;
  //return 0;
}
/**** End of sample code ****/

Here's some code that extracts secret weather data from a JSON string and prints it (this uses the json-glib library there's also a json-c library).

C:
//gcc `pkg-config --cflags json-glib-1.0` -o json-glib-example json-glib-example.c `pkg-config --libs json-glib-1.0`
#include <stdlib.h>
#include <stdio.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>

#define JSONDATA "{    \"@context\": [        \"https://geojson.org/geojson-ld/geojson-context.jsonld\",        {            \"@version\": \"1.1\",            \"wx\": \"https://api.weather.gov/ontology#\",            \"s\": \"https://schema.org/\",            \"geo\": \"http://www.opengis.net/ont/geosparql#\",            \"unit\": \"http://codes.wmo.int/common/unit/\",            \"@vocab\": \"https://api.weather.gov/ontology#\",            \"geometry\": {                \"@id\": \"s:GeoCoordinates\",                \"@type\": \"geo:wktLiteral\"            },            \"city\": \"s:addressLocality\",            \"state\": \"s:addressRegion\",            \"distance\": {                \"@id\": \"s:Distance\",                \"@type\": \"s:QuantitativeValue\"            },            \"bearing\": {                \"@type\": \"s:QuantitativeValue\"            },            \"value\": {                \"@id\": \"s:value\"            },            \"unitCode\": {                \"@id\": \"s:unitCode\",                \"@type\": \"@id\"            },            \"forecastOffice\": {                \"@type\": \"@id\"            },            \"forecastGridData\": {                \"@type\": \"@id\"            },            \"publicZone\": {                \"@type\": \"@id\"            },            \"county\": {                \"@type\": \"@id\"            }        }    ],    \"id\": \"https://api.weather.gov/stations/KBOS/observations/2021-05-17T21:54:00+00:00\",    \"type\": \"Feature\",    \"geometry\": {        \"type\": \"Point\",        \"coordinates\": [            -71.030000000000001,            42.369999999999997        ]    },    \"properties\": {        \"@id\": \"https://api.weather.gov/stations/KBOS/observations/2021-05-17T21:54:00+00:00\",        \"@type\": \"wx:ObservationStation\",        \"elevation\": {            \"value\": 9,            \"unitCode\": \"unit:m\"        },        \"station\": \"https://api.weather.gov/stations/KBOS\",        \"timestamp\": \"2021-05-17T21:54:00+00:00\",        \"rawMessage\": \"KBOS 172154Z 09010KT 10SM FEW080 BKN200 18/10 A3023 RMK AO2 SLP236 T01780100\",        \"textDescription\": \"Mostly Cloudy\",        \"icon\": \"https://api.weather.gov/icons/land/day/bkn?size=medium\",        \"presentWeather\": [],        \"temperature\": {            \"value\": 17.800000000000001,            \"unitCode\": \"unit:degC\",            \"qualityControl\": \"qc:V\"        },        \"dewpoint\": {            \"value\": 10,            \"unitCode\": \"unit:degC\",            \"qualityControl\": \"qc:V\"        },        \"windDirection\": {            \"value\": 90,            \"unitCode\": \"unit:degree_(angle)\",            \"qualityControl\": \"qc:V\"        },        \"windSpeed\": {            \"value\": 18.359999999999999,            \"unitCode\": \"unit:km_h-1\",            \"qualityControl\": \"qc:V\"        },        \"windGust\": {            \"value\": null,            \"unitCode\": \"unit:km_h-1\",            \"qualityControl\": \"qc:Z\"        },        \"barometricPressure\": {            \"value\": 102370,            \"unitCode\": \"unit:Pa\",            \"qualityControl\": \"qc:V\"        },        \"seaLevelPressure\": {            \"value\": 102360,            \"unitCode\": \"unit:Pa\",            \"qualityControl\": \"qc:V\"        },        \"visibility\": {            \"value\": 16090,            \"unitCode\": \"unit:m\",            \"qualityControl\": \"qc:C\"        },        \"maxTemperatureLast24Hours\": {            \"value\": null,            \"unitCode\": \"unit:degC\",            \"qualityControl\": null        },        \"minTemperatureLast24Hours\": {            \"value\": null,            \"unitCode\": \"unit:degC\",            \"qualityControl\": null        },        \"precipitationLastHour\": {            \"value\": null,            \"unitCode\": \"unit:m\",            \"qualityControl\": \"qc:Z\"        },        \"precipitationLast3Hours\": {            \"value\": null,            \"unitCode\": \"unit:m\",            \"qualityControl\": \"qc:Z\"        },        \"precipitationLast6Hours\": {            \"value\": null,            \"unitCode\": \"unit:m\",            \"qualityControl\": \"qc:Z\"        },        \"relativeHumidity\": {            \"value\": 60.277058019152001,            \"unitCode\": \"unit:percent\",            \"qualityControl\": \"qc:V\"        },        \"windChill\": {            \"value\": null,            \"unitCode\": \"unit:degC\",            \"qualityControl\": \"qc:V\"        },        \"heatIndex\": {            \"value\": null,            \"unitCode\": \"unit:degC\",            \"qualityControl\": \"qc:V\"        },        \"cloudLayers\": [            {                \"base\": {                    \"value\": 2440,                    \"unitCode\": \"unit:m\"                },                \"amount\": \"FEW\"            },            {                \"base\": {                    \"value\": 6100,                    \"unitCode\": \"unit:m\"                },                \"amount\": \"BKN\"            }        ]    }}"

int main (int argc, char *argv[])
{
  char *str = JSONDATA;



  /* str contains the JSON from the example above */
  JsonParser *parser = json_parser_new ();
  json_parser_load_from_data (parser, str, -1, NULL);

  JsonReader *reader = json_reader_new (json_parser_get_root (parser));

  json_reader_read_member (reader, "properties");
  json_reader_read_member (reader, "textDescription");
  const char *desc = json_reader_get_string_value (reader);
  json_reader_end_member (reader);
  json_reader_read_member (reader, "temperature");
  json_reader_read_member (reader, "value");
  double temperature = json_reader_get_double_value (reader);
  json_reader_end_member (reader);
  json_reader_end_member (reader);
  json_reader_end_member (reader);

  g_object_unref (reader);
  g_object_unref (parser);

  printf("%lf\n%s\n", temperature,desc);
  return EXIT_SUCCESS;
}

One fetches JSON data the other converts JSON to useable information.... it's all there.
 
Back
Top