W
Wolfgang Draxinger
Antoninus said:You need to supply a FILE* to ferror().
Sorry, that were my mistakes, and I hereby take responsibility for them. I
didn't test that code yet, yes lazy and a little bit careless. Anyway, I
did that now, here's there working and checked code, complete with testing
entry point. Compile with
cc -DTEST -o calluname calluname.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define BLOCK_LENGTH 100
char * get_operating_system() {
FILE *fil;
char *output = 0;
size_t output_length = 0;
size_t output_read = 0;
fil = popen("uname -snm", "r");
if( !fil ) {
perror("error calling uname: ");
return 0;
}
while( !feof(fil) ) {
if( output_read + BLOCK_LENGTH + 1 > output_length ) {
char * o_new =
realloc(output,
output_length += BLOCK_LENGTH);
if( !o_new ) {
perror("error allocating memory: ");
break;
}
output = o_new;
}
output_read += fread(output +
output_read,
BLOCK_LENGTH,
1,
fil);
if( ferror(fil) ) {
fprintf(stderr, "error reading from uname
stdout\n");
break;
}
}
if( output &&
output_length > 0 &&
output_read > 0 ) {
memset(output + output_read, 0,
output_length - output_read);
}
pclose(fil);
return output;
}
#ifdef TEST
int main(int argc, char *argv[])
{
char *OS = get_operating_system();
if( !OS )
return EXIT_FAILURE;
puts(OS);
free(OS);
return EXIT_SUCCESS;
}
#endif