Solved

this is a linux one

Posted on 2000-04-07
4
193 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

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

943 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

10 Experts available now in Live!

Get 1:1 Help Now