I am working on a c program which displays all the directories and files under each directory.
I want to know what kind of file each is. Like, is the file an ascii text file or english text or a c file.
if it is an executable, is it an binary file or a shell script.
I was told to look inside the file to achieve this.
Is there any unix utility to do this?what function can I use to achive this.Thank you for any kind of help .
you can use it from C , with system(" file <file>");
Let's say , you got some files :
ok.c exe ..
you make the code , with system("file <file>"); or you use file <file> command withing shell script :
you should get something like :
exe: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.0, dynamically linked (uses shared libs), not stripped
file.c : ex.c: ASCII C program text
Thank you! Ur suggetion was very much helpful.
But I just want the message like ascii text or english text.How can I do that?
Is the returned value a string or what?
Can u give me a more detailed example.
ps:Thanks.I think u r really smart.U reply to most of my questions.
Thanks anyway.
Thank you, everyone.
Pardon me for my dumb questions.
Can u please tell me whether the filename specified in system("file <file>") should be a constant or can it be a variable too?
Because, the file name is in a variable dirt and it changes for every iteration in the loop.
Thanks again.
Sorry for the stupid question.
Here is a sample piece of code.
void dirlist(char dir[PATH_MAX],int request)
DIR *dp=opendir(dir);
struct direct *entry;
extern int errno;
char dirt[PATH_MAX]={0x0};
struct stat buf;
int i=0;
extern int errno;
if(!dp)
{
printf(" Unable to Open %s\n",dir);
return;
}
for(entry=readdir(dp);entry!=NULL;entry=readdir(dp))
{
i++;
if(i==1 || i==2) continue;//skiping if it is . or ..(just for now)
printf("%30s\t",entry->d_name);
strcpy(dirt,dir);//copy the whole pathname
strcat(dirt,"/");
strcat(dirt,entry->d_name);
Hi !_30!
I am quite new to this.
U asked me to use ( STRNCPY(string,initial_string+x,y) ) on the output string but the system("file <file"); is printing it on the console like the printf statement.So what should I do?
I want only part of the message and not the whole thing.
Thank you.