file type

Hi Everyone!

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 .

Hey dude ,

You can just use file <file> command dude .

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

:wink:

Hi !_30!

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.

General questions about the command 'file' are best answered by looking at 'man file'. Yes, it prints human-readable strings.

Like , it was saied , all you can find it's in man file

You can see there a lot of option's .. No the result is not a string , but I can say to you that is inspired from :

/usr/share/misc/file/magic.mime

If you want just a part of that message to be shown , you can just copy a part of it with ( STRNCPY(string,initial_string+x,y) ) from it ..

:smiley:

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.

The system function doesn't care whether it's given a constant or not. Beyond that I'm not sure what you're asking.

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);

So, here can I use :

int i=system\("file dirt"\);

Is it possible to do that?

Appreciate all ur halp.
Thank you. :slight_smile:

Oh, I see, you want variable substitution in a constant string. C doesn't do that.

You have to build a new string out of other strings like so:

{
  char buf[512];
  sprintf(buf,"file %s",dirt);
  system(buf);
}

Thanks a lot, Corona688.It solved my problem. :smiley: :slight_smile: :cool: :wink: :smiley: :slight_smile:

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.

{
  char buf[512];
  FILE *p=popen("file somefile","r");
  fgets(buf,512,p);
  fprintf(stderr, "Process returned %d\n",pclose(p));

  printf("Output was %s\n",buf);
}

Thank you!