Keven Diuliy
Keven Diuliy

Reputation: 131

C program to list hidden and read only files

I'm creating a simple C program that list directory contents. Does anyone know how to list only hidden? The folllowing code pulls every file from the directory and works perfect, but I only need the hidden files. Thanks.

Upvotes: 1

Views: 5091

Answers (4)

anilbey
anilbey

Reputation: 1977

To check whether a file is readonly or not, you can use sys/stat.h (reference : http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/stat.h.html)

All you need to do is to perform & (binary and) operation on st_mode.

struct stat st; 

if (stat(fileName, &st) == 0)
    cout << " user write permission: " << (st.st_mode & 00200 ) ;

If the output is 0, then user has no permission to write (readonly). Otherwise the file is not readonly.

Other permission bits:

       S_IRWXU     00700   mask for file owner permissions
       S_IRUSR     00400   owner has read permission
       S_IWUSR     00200   owner has write permission
       S_IXUSR     00100   owner has execute permission
       S_IRWXG     00070   mask for group permissions
       S_IRGRP     00040   group has read permission
       S_IWGRP     00020   group has write permission
       S_IXGRP     00010   group has execute permission
       S_IRWXO     00007   mask for permissions for others
                           (not in group)
       S_IROTH     00004   others have read permission
       S_IWOTH     00002   others have write permission
       S_IXOTH     00001   others have execute permission

Upvotes: 1

rashok
rashok

Reputation: 13474

Below code snippet will help you to check whether the file is hidden or not in windows.

int is_hiddenfile(char* file_name)
{
    FILE * batch_file = NULL;
    FILE * output_file = NULL;
    int count = 0;
    batch_file = fopen ("D:\\bat.bat", "w");
    fputs ("dir /ah ", batch_file);
    fputs (file_name, batch_file);
    fputs (" 2>D:\\out.txt", batch_file);
    fclose (batch_file);
    ShellExecuteA (NULL, "open", "D:\\bat.bat", NULL, NULL, SW_SHOWNORMAL);
    output_file = fopen("D:\\out.txt", "r");
    while(feof(output_file) == 0)
    {
        fgetc(output_file);
        count++;
    }
    if (count <= 1)
        return 1;
    else
        return 0;
}

To check whether a file is readonly or not in windows, /ar needs to be used instead of /ah in dir command.

int is_readonlyfile(char* file_name)
{
    ... //same as is_hiddenfile function
    fputs ("dir /ar ", batch_file);
    ... //same as is_hiddenfile function
}

Upvotes: -2

md5
md5

Reputation: 23727

On GNU/Linux, a hidden file begin with a dot.

#include <string.h>

int is_hidden(const char *name)
{
  return name[0] == '.' &&
         strcmp(name, ".") != 0 &&
         strcmp(name, "..") != 0);
}

To check if a file is read-only, it could be a good idea to use the stat function.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int is_readonly(const char *name)
{
  struct stat buf;

  stat(name, &buf);

  return buf->st_mode & /* ... */;
}

Upvotes: 6

cnicutar
cnicutar

Reputation: 182704

  • To decide if a file is "hidden" check if its name begins with a .
  • To decide if a file is read-only do a stat(2) and check permissions (st_mode)

Alternatively, for the second point you could use access(2) if you're careful. If you want to open(2) that file you shouldn't trust whatever access(2) returned. Generally access(2) is to be avoided.

Upvotes: 3

Related Questions