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;
}
Related
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;
}
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
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;
}
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;
}
I have problem with achieving int output of numbers of running processes with same PID.
Eg.
ps aux | grep program1
shows me 3 process, 2 of my main app (parent and child). I wanna know how to get it in C. my point is to get like number "2" because i have two processes with same name. As far as i know i cannot get terminal output to C variable so I really dont have idea how to get it.The problem is that I must get this information on progmam2 not program1.
Thanks!
Check this out, I think this is not advanced at all
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
void readProcessName(const char *const comm, char name[PATH_MAX])
{
int fd;
int size;
fd = open(comm, O_RDONLY);
if (fd == -1)
return;
if ((size = read(fd, name, PATH_MAX)) > 1)
name[size - 1] = '\0';
else
name[0] = '\0';
close(fd);
}
void findProcessByName(const char *const find)
{
DIR *dir;
struct dirent *entry;
dir = opendir("/proc");
if (dir == NULL)
return;
chdir("/proc");
while ((entry = readdir(dir)) != NULL)
{
struct stat st;
char comm[PATH_MAX];
const char *name;
char procname[PATH_MAX];
name = entry->d_name;
if (stat(name, &st) == -1)
continue;
if (S_ISDIR(st.st_mode) == 0)
continue;
/* this will skip .. too, and any hidden file? there are no hidden files I think */
if (name[0] == '.')
continue;
snprintf(comm, sizeof(comm), "%s/comm", name);
if (stat(comm, &st) == -1)
continue;
readProcessName(comm, procname);
if (strcmp(procname, find) == 0)
printf("%s pid: %s\n", procname, name);
}
closedir(dir);
}
int main(int argc, char **argv)
{
findProcessByName("process-name-here");
return 0;
}