I have a little problem with my source code. gcc speak to me: undefined reference to `round' but I don't know why because i'm using stdio.h, stdlib.h, math.h... :-( Can You help me with solve this problem?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <math.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#define VERYBIG 200
int dir_size(const char* dirname)
{
int size = 0;
char path[VERYBIG];
struct stat tmp;
DIR* cat = opendir(dirname);
struct dirent* entry;
while ((entry = readdir(cat)))
{
if (!((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)))
{
path[0] = 0;
strcat(path, dirname);
strcat(path, "/");
strcat(path, entry->d_name);
if (lstat(path, &tmp) == -1)
perror("lstat");
if (S_ISDIR(tmp.st_mode))
{
size += round(tmp.st_blocks / 2);
size += dir_size(path);
}
else
{
size += round(tmp.st_blocks / 2);
}
}
}
return size;
}
void rozmiar(int blocks, int h)
{
if (h == 0)
printf("%d\t", blocks);
else
{
double newbl;
if (blocks < 1024)
printf("%dK\t", blocks);
else if (blocks < (1024 * 1024))
{
newbl = blocks / 1024;
printf("%0.1fM\t", round(newbl));
}
else
{
newbl = blocks / (1024 * 1024);
printf("%0.1fB\t", round(newbl));
}
}
}
void do_du(char dirname[], int s, int h)
{
DIR* kat;
struct dirent* wpis;
int sum = 0, dsize = 0;
if ((kat = opendir(dirname)) == NULL)
fprintf(stderr, "du2: cannot open %s\n", dirname);
struct stat info;
char path[VERYBIG];
while ((wpis = readdir(kat)) != NULL)
{
if (!((strcmp(wpis->d_name, ".") == 0) || (strcmp(wpis->d_name, "..") == 0)))
{
path[0] = 0;
strcat(path, dirname);
strcat(path, "/");
strcat(path, wpis->d_name);
if (lstat(path, &info) == -1)
perror("lstat");
if (S_ISDIR(info.st_mode))
{
dsize = dir_size(path) + round(info.st_blocks / 2);
sum += dsize;
if (s == 0)
{
rozmiar(dsize, h);
printf("%s\n", wpis->d_name);
}
}
else
{
sum += round(info.st_blocks / 2);
if (s == 0)
{
rozmiar(round(info.st_blocks / 2), h);
printf("%s\n", wpis->d_name);
}
}
}
}
if (stat(dirname, &info) == -1)
perror("stat");
sum += round(info.st_blocks / 2);
rozmiar(sum, h);
printf("%s\n", dirname);
}
int main(int ac, char* av[])
{
char optstring[] = "sh";
int opcja, human = 0, suma = 0;
while ((opcja = getopt(ac, av, optstring)) != -1)
{
switch (opcja)
{
case 's':
suma = 1;
break;
case 'h':
human = 1;
break;
case '?':
printf("Usage: du2 [-s] [-h] [KATALOG]\n\t-s\twyswietla wielkosc "
"katalogu\n\t-h\twysiwetla wielkosci plikow w kilo-/mega-/gigabajtach\n");
exit(1);
}
}
if (human != 0 || suma != 0)
{
av++;
ac--;
}
if (ac == 1)
do_du(".", suma, human);
else
do_du(*++av, suma, human);
return 0;
}
It must be a linker error.
Try compiling the program like this
gcc program.c -lm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With