December 7th, 2013, 02:29 PM
-
Symbol lookup error: undefined symbol: localtime, version GLIBC_2.2.5
I'm trying to make a program that reproduces the command line ls and -l to see the files that are in a directory.
Arguments of function main are:
ls [-l] [directory]
where [...] symbolizes that the argument is opcional.
The output for every file of the folder when -l is one of the arguments should be:
size_in_bytes month day hours:minutes name
For example: 273635 Nov 16 09:34 file.ext
Otherwise the output is simply the name of each file.
This is the message I got when running the program after successful compilation:
Symbol lookup error: undefined symbol: localtime, version GLIBC_2.2.5
Code:
#include <stdio.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int l_activated = 0;
struct dirent *info;
struct stat *more_info;
struct tm *data;
char folder[100],file[100], time[50], cwd[1024];
DIR *diretory;
getcwd(cwd,sizeof cwd);
if (argv[2]==NULL)
strcpy(folder,cwd);
else if (strcmp (argv[2],"-l")==0)
{
l_activated = 1;
if (argv[3]== NULL)
strcpy(folder,cwd);
else
strcpy(folder,argv[3]);
}
else
strcpy(folder,argv[2]);
diretory = opendir(folder);
info = readdir(diretory);
while (info != NULL)
{
if (l_activated == 1)
{
strcpy(file,folder);
strcat(file,"/");
strcat(file,info->d_name);
stat(file , more_info );
data = localtime(&more_info->st_mtime);
strftime (time, sizeof (time), "%b %e %H:%M", data);
printf("\n%10zd %s %s", more_info->st_size, time, info->d_name);
}
else
printf("\n%s",info->d_name);
info = readdir(diretory);
}
printf("\n\n");
closedir(diretory);
return 0;
}
what does it means?
December 7th, 2013, 03:30 PM
-
December 7th, 2013, 05:41 PM
-
gcc on my Ubuntu (Kosmic Koala) throws a warning that more_info may be used uninitialized.
Code:
int main(int argc, char **argv)
{
int l_activated = 0;
struct dirent *info;
struct stat *more_info;
struct tm *data;
char folder[100],file[100], time[50], cwd[1024];
. . .
strcpy(file,folder);
strcat(file,"/");
strcat(file,info->d_name);
stat(file , more_info );
data = localtime(&more_info->st_mtime);
The way that you're calling stat, it's as if you expect stat to have created a static struct somewhere that it's giving you the pointer to. However, when I read the man page for stat I didn't see any such statement. And when I looked it up in a textbook on Linux systems programming, they declared an actual stat struct and passed its address to stat. Besides, if stat were returning a pointer to a struct through the parameter list, then it would have to have been declared as struct stat **, not just struct stat *, and you would have had to have passed the address of more_info so that the function could change that pointer.
Instead, you should probably write that as:
Code:
int main(int argc, char **argv)
{
int l_activated = 0;
struct dirent *info;
struct stat more_info;
struct tm *data;
char folder[100],file[100], time[50], cwd[1024];
. . .
strcpy(file,folder);
strcat(file,"/");
strcat(file,info->d_name);
stat(file , &more_info );
data = localtime(&more_info.st_mtime);
I haven't tested that.
Also, the man page says that not all the time fields are updated by some Linux file systems. Plus there are several things that could change the st_mtime field. You should probably review the man page of stat() yourself.