Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bloody beginner question

63 views
Skip to first unread message

Jan Novak

unread,
Apr 15, 2021, 11:57:26 PM4/15/21
to
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

Siri Cruise

unread,
Apr 16, 2021, 1:12:18 AM4/16/21
to
In article <s5b1uq$eeq$1...@gwaiyur.mb-net.net>,
Jan Novak <rep...@gmail.com> 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.

Don't use C?

If I wanted to do something like this I would likely use a shell
script.

while continue-condition; do
curl "http://...." | awk 'extract-information' >> log-file
sleep 60
done

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
Discordia: not just a religion but also a parody. This post / \
I am an Andrea Doria sockpuppet. insults Islam. Mohammed

Keith Thompson

unread,
Apr 16, 2021, 1:43:27 AM4/16/21
to
First, I'm going to give you some annoying nitpicks that aren't directly
related to the problem you're trying to solve (which I agree with Siri
would probably be better done in a scripting language).

Use <>, not "", for the header names.

Use "int main(void)" rather than "int main()". Both forms are pretty
much valid, but "int main(void) is more explicit. (Use "int main()" if
you're writing C++). And separating the type from the function name is
an odd styhle.

It doesn't make much sense to use time_to_sleep as the while loop
condition, since it will always be true (non-zero). And incidentally,
if you're never change its value, it would make sense to define it
"const".

With those changes:

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

int main(void) {
const unsigned int time_to_sleep = 60;
int i = 1;
while (time_to_sleep) {
sleep(time_to_sleep);
printf("step %d\n", i);
i++;
}
}

Polling a URL is not something you can do in pure standard C without
extensions. (But the same is true of the sleep() function.) The
simplest approach would be to use the standard system() function
(declared in <stdlib.h>) to invoke some external command, perhaps curl
or wget. You'd have to use C's string functions to build the command,
including any arguments, to pass to system(). And again, that's
something that's more easily done in some scripting language.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Jan Novak

unread,
Apr 16, 2021, 2:15:46 AM4/16/21
to
Am 16.04.21 um 07:43 schrieb Keith Thompson:

> First, I'm going to give you some annoying nitpicks that aren't directly
> related to the problem you're trying to solve (which I agree with Siri
> would probably be better done in a scripting language).

thx!

> Polling a URL is not something you can do in pure standard C without
> extensions. (But the same is true of the sleep() function.) The
> simplest approach would be to use the standard system() function
> (declared in <stdlib.h>) to invoke some external command, perhaps curl
> or wget.

Thats an option, i never had thought about. And this is an option, where
i know how to use. Very thanks about this idea.

> You'd have to use C's string functions to build the command,
> including any arguments, to pass to system(). And again, that's
> something that's more easily done in some scripting language.

Yes, i know. But I can't and don't want to run a shell script (bash or
other), to execute wget or curl.

How can i work with the output of the system call in C?

Jan


Øyvind Røtvold

unread,
Apr 16, 2021, 4:13:01 AM4/16/21
to
Jan Novak <rep...@gmail.com> writes:

[ ... ]
>
> How can i work with the output of the system call in C?

You should probably use the popen(3) function.

https://linux.die.net/man/3/popen

--
.. Ųyvind - soon to appear in a kill file near you.
.. Ignorance can be cured; stupidity is forever.

Malcolm McLean

unread,
Apr 16, 2021, 5:53:18 AM4/16/21
to
On Friday, 16 April 2021 at 06:43:27 UTC+1, Keith Thompson wrote:
>
> Polling a URL is not something you can do in pure standard C without
> extensions. (But the same is true of the sleep() function.) The
> simplest approach would be to use the standard system() function
> (declared in <stdlib.h>) to invoke some external command, perhaps curl
> or wget. You'd have to use C's string functions to build the command,
> including any arguments, to pass to system(). And again, that's
> something that's more easily done in some scripting language.
>
If a non-C programmer wants a C program, then the most likely reason
is that he wants it to be standalone executable without any dependencies.
So "system" isn't a good solution.

There will be a lib curl (C url) on the system. The OP needs to include the
header and learn to use the calls. I've used it in the past, but I can't remember
the details.

Bart

unread,
Apr 16, 2021, 6:49:08 AM4/16/21
to
On 16/04/2021 10:53, Malcolm McLean wrote:
> On Friday, 16 April 2021 at 06:43:27 UTC+1, Keith Thompson wrote:
>>
>> Polling a URL is not something you can do in pure standard C without
>> extensions. (But the same is true of the sleep() function.) The
>> simplest approach would be to use the standard system() function
>> (declared in <stdlib.h>) to invoke some external command, perhaps curl
>> or wget. You'd have to use C's string functions to build the command,
>> including any arguments, to pass to system(). And again, that's
>> something that's more easily done in some scripting language.
>>
> If a non-C programmer wants a C program, then the most likely reason
> is that he wants it to be standalone executable without any dependencies.
> So "system" isn't a good solution.

'system' is a standard C function; I wouldn't call a C library a
dependency, more a necessity.


> There will be a lib curl (C url) on the system. The OP needs to include the
> header and learn to use the calls. I've used it in the past, but I can't remember
> the details.
>

And if such a library is available, then I'd guess so would commands
like 'curl', assuming this is Linux.

Malcolm McLean

unread,
Apr 16, 2021, 7:04:47 AM4/16/21
to
On Friday, 16 April 2021 at 11:49:08 UTC+1, Bart wrote:
> On 16/04/2021 10:53, Malcolm McLean wrote:
> > On Friday, 16 April 2021 at 06:43:27 UTC+1, Keith Thompson wrote:
> >>
> >> Polling a URL is not something you can do in pure standard C without
> >> extensions. (But the same is true of the sleep() function.) The
> >> simplest approach would be to use the standard system() function
> >> (declared in <stdlib.h>) to invoke some external command, perhaps curl
> >> or wget. You'd have to use C's string functions to build the command,
> >> including any arguments, to pass to system(). And again, that's
> >> something that's more easily done in some scripting language.
> >>
> > If a non-C programmer wants a C program, then the most likely reason
> > is that he wants it to be standalone executable without any dependencies.
> > So "system" isn't a good solution.
> 'system' is a standard C function; I wouldn't call a C library a
> dependency, more a necessity.
>
system is a standard function, but it depends on the program being attached
to a "system" that supports the passed command.
>
> > There will be a lib curl (C url) on the system. The OP needs to include the
> > header and learn to use the calls. I've used it in the past, but I can't remember
> > the details.
> >
> And if such a library is available, then I'd guess so would commands
> like 'curl', assuming this is Linux.
>
One would think so. I just tried typing "curl" into the DOS box on the machine
I'm using that the moment, and to my surprise it came back with "try curl --help".

But you've got to get the output. And if you're not careful, unwanted terminal windows
might start popping up. Then the user could always play games with his paths,
making the program fail in unexpected ways.

Calling a library is usually a cleaner, better solution than calling an external
process.



Scott Lurndal

unread,
Apr 16, 2021, 11:22:14 AM4/16/21
to
Sure. Write it in Java, should be only a few line - java has
all the hard work already built-in.

Keith Thompson

unread,
Apr 16, 2021, 2:11:39 PM4/16/21
to
Why not? Knowing your requirements and the reason for them makes it
easier for us to make workable suggestions.

> How can i work with the output of the system call in C?

The system() function is actually fairly primitive. It takes a string
representing a command; on Unix-like systems, it's passed to /bin/sh.
There's no way for the calling program to (directly) get the command's
output. The value returned by system() gives you some information.
You *could* do something like:

system("curl https://www.example.com/foo > output.txt");

and the open "output.txt", but that's probably a sign you should be
using some other approach.

popen() (which is POSIX-specific, not standard C) is a possibility.

Your problem desciption strongly suggests (to me) that a shell script,
or something in some other scripting language, is the best solution.
I'd like to know more about the constraints that imply that it isn't.

Eli the Bearded

unread,
Apr 16, 2021, 2:29:35 PM4/16/21
to
In comp.lang.c, Malcolm McLean <malcolm.ar...@gmail.com> wrote:
> On Friday, 16 April 2021 at 06:43:27 UTC+1, Keith Thompson wrote:
>> Polling a URL is not something you can do in pure standard C without
>> extensions. (But the same is true of the sleep() function.) The
>> simplest approach would be to use the standard system() function

I am doubtful that system("curl") is going to be simpler than libcurl
once you get into dealing with errors and processing output.

> If a non-C programmer wants a C program, then the most likely reason
> is that he wants it to be standalone executable without any dependencies.
> So "system" isn't a good solution.

Agreed. To minimize dependencies, code it in Go.

https://golang-examples.tumblr.com/post/41865096384/simple-http-client

Elijah
------
would probably use sh script for a task like that and accept dependencies

Chris M. Thomasson

unread,
Apr 16, 2021, 4:00:16 PM4/16/21
to
Might as well use sockets and code up a http client from scratch in C. ;^)

Keith gave you some much easier options.

John Bode

unread,
Apr 30, 2021, 12:10:19 PM4/30/21
to
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.
0 new messages