How can I list the number of files/directories - c

I wrote this function that actually lists the contents of a given directory. However, I want to make it just tell me how many files and how many directories there are. I've tried things like this, but it doesn't really work:
if(dir->d_type == D_DIR)
directories++;
Here's my code. How can I modify it:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
int main (void) {
DIR *d = opendir (".");
struct dirent *dir;
if (d != NULL) {
int files, directories = 0;
while(dir=readdir(d))
puts(dir->d_name);//Prints the actual names of the entries
//Closing the directory
(void) closedir (d);
} else
perror ("Couldn't open the directory");
return 0;
}

I figured it. Here's my code for future references (if someone else needs that):
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
int main (void) {
DIR *d = opendir (".");
struct dirent *dir;
if (d != NULL) {
int files, directories = 0;
while((dir=readdir(d)) != NULL) {
if (dir->d_type == DT_REG)
files++;
if(dir->d_type == DT_DIR)
directories++;
}
//Closing the directory
(void) closedir (d);
printf("%i Files and %i directories\n\n", files, directories);
} else
perror ("No such directory");
return 0;
}

Related

Listing Elements with the same inodes in Groups

I have a program that scans the entire root directory and to get inodes, hardlinks counts and the file paths. the results is as follows:
However i would like to group all the hardlinks linked to a particular inode together. At the end i would display the Inode , Hardlink count, and the grouped paths.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <stdlib.h>
void scanDirectory(char *basePath){
DIR* dir = opendir(basePath);
struct dirent* dp;
struct stat* statbuf = (struct stat*)malloc(sizeof(struct stat));
if (!dir)
return;
while((dp = readdir(dir)) != NULL){
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
continue;
if (dp->d_type == DT_DIR) {
char path [1024];
strcpy (path,basePath);
strcat (path,dp->d_name);
strcat (path,"/");
scanDirectory(path);
}
else if(dp->d_type == DT_REG){
char file [1024];
strcpy (file,basePath);
strcat (file,dp->d_name);
if (stat(file, statbuf) == 0) {
if(statbuf->st_nlink >1){
printf("Inode:%ld | Hardlink: %d | %s \n",dp->d_ino, (unsigned int)statbuf->st_nlink, file);
}
}
}
}
free(statbuf);
closedir(dir);
}
int main(){
scanDirectory("/");
}

How to print out process name if i know the pid using c? [duplicate]

Hello!
I wanto to make simple c proggram what will work like ps -e. The only colums that should be shown are PID and CMD. Thats my code:
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <regex.h>
int main()
{
DIR *dir;
struct dirent *entry;
if ((dir = opendir("/proc")) == NULL)
perror("operation error");
else
{
printf("PID CMD\n");
while ((entry = readdir(dir)) != NULL)
printf(" %s\n", entry->d_name);
closedir(dir);
}
return 0;
}
My questins are:
1) How i can show only folders with numbers(i don't know how to implement regcomp())?
2)How to near PID write CMD (I can't glue(?) strings with path if is folder with number)?
This is an hint ... Try to develop your code starting from this! :)
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
int readData(char *dirname);
int readData(char *dirname)
{
FILE * file;
char buffer[1024]={0};
sprintf(buffer,"/proc/%s/stat",dirname);
file = fopen(buffer,"r");
if (!file)
return errno;
while(fgets(buffer,sizeof(buffer),file))
puts(buffer);
if (file)
fclose(file);
return errno;
}
int main(void)
{
DIR * dir;
struct dirent * entry;
if ( (dir = opendir("/proc")) == NULL )
perror("operation error");
while ((entry = readdir(dir))) {
if ( strlen(entry->d_name) == strspn(entry->d_name, "0123456789"))
if (readData(entry->d_name))
break;
}
if (dir)
closedir(dir);
return errno;
}

Read file /proc/{pid}/comm in c linux

I want read file /proc/{PID}/comm (the name of the proccess) and write near PID in my simple ps command
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
int main()
{
DIR *dir;
struct dirent *ent;
const char l[]="0123456789";
if ((dir = opendir("/proc")) == NULL)
perror("operation error");
else
{
printf(" PID CMD\n");
while ((ent = readdir(dir)) != NULL)
if(strspn(ent->d_name, l))
{
printf(" %s\n", ent->d_name);
}
closedir(dir);
}
return 0;
}
I readed lot of examples, but i don;t have idea how should it looks like in my code.
Have somebody any ideas

c: Ignore parent, child and DS_STORE files

I am trying to create a program that goes through a directory and prints "This is a directory" for all sub-directories and this is a "regular file" for all text files/photos etc.
The problem is, when I go to do so, the parent, child and DS_STORE file and directories are shown.
I know there have been similar questions on this forum, but some of them are either too advanced for me or focus on other languages.
Just to be clear, I'm trying to iterate through a directory while ignoring the parent, child and DS_Store files/folders.
Here's what I have so far:
all include/define statements
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <dirent.h>
#include<stdio.h>
#include <unistd.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
#define FILE_MODE S_IRWXU
DIR *directory;
struct dirent *entry;
Functions
bool isaDie(struct dirent *aFile)
{
char fileType = aFile->d_type;
switch(fileType)
{
case DT_DIR:
printf("this is a directory \n");
return true;
break;
case DT_REG:
printf("This is a regular file \n");
return false;
default: printf("Unknown file type \n");
} return false;
}
struct stat;
void goThoughFile( const char *aFilePath )
{
directory = opendir(aFilePath);
if(directory == NULL)
{
printf("Error");
exit(1);
}
else
{
while( (entry = readdir(directory)) != NULL)
{
printf("%s", entry->d_name);
isaDie(entry);
}
}
}
main
int main()
{
goThoughFile("somePathHere" );
return 0;
}

C programming: How to get directory name?

I'm writing a code for printing out the path from root to current directory or referred directory, using recursive function. but I can't get the directory name, only get ..
Problem happened among base case and call
dirent->name.
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
static void list_dir (const char * dir_name)
{
DIR * d;
struct dirent *e;
struct stat sb;
struct stat sb2;
long childIno;
long parentIno;
char parent[200];
stat(dir_name, &sb);
if (stat(dir_name, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
childIno = (long) sb.st_ino;
/* get parent dir name */
snprintf(parent, sizeof(parent), "%s/..", dir_name);
d = opendir(parent);
stat(parent, &sb2);
if (stat(parent, &sb2) == -1) {
perror("stat2");
printf("parent name: \n");
exit(EXIT_FAILURE);
}
parentIno = (long) sb2.st_ino;
if (d == NULL) {
printf("Cannot open dircetory '%s'\n", parent);
}
/*below code is really messed up*/
if (childIno == parentIno) {
while ((e = readdir(d)) != NULL) {
printf("base case %s\n", e->d_name);
break;
}
}else{
list_dir(parent);
}
/*code above here is really messed up*/
/* After going through all the entries, close the directory. */
closedir (d);
}
int main (int argc, char** argv)
{
list_dir (argv[1]);
return 0;
}
right result should be when typing command line
./pathto .
should print out path from root directory to my current directory
or if command line as this
./pathto file.txt
should print out path from root directory to file.txt
Doing this using <dirent.h> and stat is possible, but really tricky. POSIX offers a function for this called realpath. Omitting error checking:
#include <limits.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char buf[PATH_MAX];
puts(realpath(argv[1], buf));
return 0;
}

Resources