Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

All your files have been destroyed (sorry). Paul.


devel / comp.lang.c / Re: bloody beginner question

SubjectAuthor
o Re: bloody beginner questionJohn Bode

1
Re: bloody beginner question

<s6ha50$9vf$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=16046&group=comp.lang.c#16046

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jfbode1...@gmail.com (John Bode)
Newsgroups: comp.lang.c
Subject: Re: bloody beginner question
Date: Fri, 30 Apr 2021 11:10:06 -0500
Organization: A noiseless patient Spider
Lines: 125
Message-ID: <s6ha50$9vf$1@dont-email.me>
References: <s5b1uq$eeq$1@gwaiyur.mb-net.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 30 Apr 2021 16:10:08 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="86eb9b83bd74069bf5dbcc5fb87b693e";
logging-data="10223"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198BhUJ7v6AglwV8VGEPLEf"
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0)
Gecko/20100101 Thunderbird/78.9.0
Cancel-Lock: sha1:MQtki0cyKLIkMvaFlJ5YfonRN98=
In-Reply-To: <s5b1uq$eeq$1@gwaiyur.mb-net.net>
Content-Language: en-US
 by: John Bode - Fri, 30 Apr 2021 16:10 UTC

On 4/15/21 10:57 PM, Jan Novak wrote:
> Hi,
>
> i need a small c program, what polls a static url every 60 seconds with
> 2 parameters and wrote the result to a log file.
>
> It should run under linux, compiled with gcc. Only thing i have is this:
>
> #include "stdio.h"
> #include "unistd.h"
> int
>
> main(){
>    unsigned int time_to_sleep = 60;
>    int i=1;
>    while(time_to_sleep){
>        sleep(time_to_sleep);
>        printf("step %d\n", i);
>        i++;
>    }
> }
>
> I understand the basics, but I dont want to be a C programmer. Can
> someone help?
>
> Jan

Several questions:

First, when you say "with 2 parameters", do you mean parameters
as part of the URL, like "http://some.url?p1=value&p2=another_value",
or are sent as a JSON/XML payload via POST like

{ "param1" : "value1", "param2" : "value2" }
?

Second, do those parameters stay the same for every call, or do
they change from call to call?

Does your program need to actually process any of the data, or does it
go straight into a log file for processing later?

Is your program meant to run as a daemon or background process, or
is it fired off from an interactive session?

If the parameters are constant *and* part of the URL *and* all you need
to do is append data to a file for later processing, then this should be
sufficient:

#include <stdio.h>
#include <unistd.h>

int main( void )
{
/**
* Command to execute curl on a URL and append the results to a
* log file. Assumes curl is available on your system.
*/
const char *command = "curl http://some.url?p1=val1&p2=val2 >>
log_file";
unsigned int time_to_sleep = 60;
int i=1;

/**
* Since time_to_sleep never changes, this is equivalent - do you
* really intend for your loop to run forever?
*/
for( ;; )
{
system( command );
sleep( time_to_sleep );

/**
* If your loop is really meant to run "forever",
* then i will eventually overflow, and the
* behavior of signed integer overflow
* is undefined. This also assumes you're
* running from an interactive session, since
* it assumes the presence of stdout.
*/
printf( "step: %d\n", i++ );
}
return EXIT_SUCCESS;
}

If the parameters can be different for each invocation of the program
(i.e., specified on the command line), then you'll need to do some
string processing when you build the command:

#include <stdio.h>
#include <unistd.h>

#define COMMAND_SIZE 64 // Needs to be large enough to store the
// full command string; 64 is large
// enough to store the example above with
// some padding, but you'll need to adjust
// for the real URL and parameter names and
// values.

int main( int argc, char **argv )
{
if ( argc < 3 )
{
fprintf( stderr, "USAGE: %s <param1> <param2>\n", argv[0] );
return EXIT_FAILURE;
}

char command[COMMAND_SIZE+1];
const char *fmt = "curl http://some.url?p1=%s&p2=%s >> log_file" ;
sprintf( command, fmt, argv[1], argv[2] );

/** rest of the program is the same as the example above */

}

If you need to do a POST instead of a GET (send the parameters as part
of a JSON/XML payload rather than in the URL itself) or you need to
process the data as it comes back, then this gets a *lot* more
complicated in a hurry, to the point where I echo Keith and others
in saying you really, really, *really* don't want to do it in C. You'll
need to use a third-party library since C doesn't natively support
network communications, much less Web programming. I've used libcurl
(https://curl.se/libcurl/) which is one of the less painful options,
but if you really don't want to do a lot of C programming, then you
will want to explore other options.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor