Solved

this is a linux one

Posted on 2000-04-07
4
190 Views
Last Modified: 2010-04-15
i have to write a program that will look in a directory and count all the files that hte read permitions for the group and anyone. the result should be in a file (formatted text).
0
Comment
Question by:kplonk
  • 2
4 Comments
 
LVL 2

Expert Comment

by:munsie
ID: 2695222
if your looking for someone to write your school assignment, then you're looking in the wrong place.

for a quick pointer, read the man page for opendir() and stat().

dennis
0
 

Author Comment

by:kplonk
ID: 2695271
that is not what i wnat all i need to know is hoe to get the informaton oh the access writes to a file. thanks, also the dta type needed to hold this would also be nice.
0
 
LVL 2

Expert Comment

by:munsie
ID: 2695404
ok, in my original comment i said to look at the opendir() and stat() man pages.  These two calls (and related calls) have everything you need to do what you described.  The opendir() call allows you to find the name and other information about every file in a directory.  The stat() call will give you any other information you need.

dennis
0
 

Accepted Solution

by:
SkAtAn earned 10 total points
ID: 2698130
This one is from "beginning Linux Programming" .. I am sure you will be abel to do something with it.

/*  We start with the appropriate headers and then a function, printdir,
    which prints out the current directory.
    It will recurse for subdirectories, using the depth parameter is used for indentation.  */

#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void printdir(char *dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;

    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 ||
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

/*  Now we move onto the main function.  */


int main(int argc, char* argv[])
{
    char *topdir, pwd[2]=".";
    if (argc != 2)
        topdir=pwd;
    else
        topdir=argv[1];

    printf("Directory scan of %s\n",topdir);
    printdir(topdir,0);
    printf("done.\n");  

    exit(0);
}
         
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
posix semaphore deadlock 13 108
port mapped I/o query 10 139
UPD maximums on Red Hat 6 102
Digital Output does not have continuity to ground? 3 69
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now