I'm trying to make a program
printing files that has the same inode numbers
printing the amount of hard links in the current directory.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
int main() {
DIR *dp;
dp = opendir(".");
struct dirent *dent1, *dent2;
while((dent1 = readdir(dp))) {
while((dent2 = readdir(dp))) {
struct stat buf;
if(stat(dent1->d_name, &buf)) {
perror("stat");
exit(1);
}
if((dent1->d_ino == dent2->d_ino) && (dent1->d_name != dent2->d_name) {
printf("---Same inode files---\n");
printf("%s\n", dent1->d_name);
printf("%s\n", dent2->d_name);
printf("Number of hard links = %o\n", (unsigned int)buf.st_nlink);
}
}
}
closedir(dp);
return 0;
}
This is my code I wrote.
I used two loops to compare each files inode numbers.
There was no error in the program. But it didn't work well as I expected.
There were 3 hard links in the current directory, but it only printed one of them.
I know my coding skills are not good, but I did my best and this is as far as I can get on my own.
Is there anything wrong with my program?
Do I have to add something more?
Is my code inefficient?
Related
I am not sure why I am getting a seg fault. I know it is somewhere in my pidspec function but I'm not sure why it is happening. The goal of this program is to have the process id passed in as first argument to program, from there, the pid is located in the proc folder and the contents of that file are displayed to console. Any help would be greatly appreciated. I haven't written any C for a year so I'm a bit rusty.
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
void pidspec(char *v){
DIR *myDirectory;
struct dirent *myFile;
char *proc = "/proc";
printf("Made it here");
myDirectory = opendir(proc);
if(myDirectory){
printf("Made it here");
if(strcmp(myFile->d_name, v) == 0){
myDirectory = opendir(v);
if(myDirectory){
while ((myFile = readdir(myDirectory)))
printf("%s\n", myFile->d_name);
}
}
}
return;
}
int main(int argc, char *argv[]){
printf("Made it here");
if(argc == 2){
printf("%s",argv[1]);
pidspec(argv[1]);
}
return 0;
}
On your first run, myFile is not initialised, that is not pointing to anything, and then you de-refernece it!
struct dirent *myFile;
...
if(strcmp(myFile->d_name, v) == 0)
So either you didn't mean to use myFile here, or ensure it's pointing at something first.
Whenever I run my program for every directory 8 is output even when I use ls-ls to check the actual size of each directory.
Im trying to print the st_blocks of all the directories the user enters
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char**argv)
{
struct stat path_stat;
int i;
int size=0;
for (i=1; i <argc; i++){
if (lstat(argv[i], &path_stat)) {
perror(argv[i]);
} else {
if (S_ISDIR(path_stat.st_mode)){
size=path_stat.st_blocks;
printf("%d %s \n",size,argv[i]);
}
}
}
return(0);
}
st_blocks is the number of 512 byte blocks allocated to the directory node. 8x512 = 4096 bytes = 1 block for a typical modern filesystem seems correct.
It will not tell you the total size of files within the folder or similar information, if that is what you are hoping to obtain.
For more information:
https://askubuntu.com/questions/186813/why-does-every-directory-have-a-size-4096-bytes-4-k
Using C on Linux, I'm writing a code that stores all the information about the files in a directory using function stat() and prints them on the Terminal
The algorithm is quite simple, I made a structure array of "files" and dynamically allocated them. The structure contains a char array (string) so I dynamically allocated it too.
The thing is .. the dynamic allocation works fine but if I'm inside the while loop I can access the other element inside the structure - which is a structure stat object - but if I access it after the loop finishes, it gives me "Segmentation Fault"!
Here's the code
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
struct file{
char* name;
struct stat fbuf;
};
int main(int argc, char **argv)
{
char* dir=NULL;
int k;
dir=(char *)malloc(strlen(argv[argc-1])+1);
dir=argv[argc-1];
strcpy(dir,argv[argc-1]);
DIR *curr_dir;
struct dirent *dir_inode;
int i,j=0;
char* sum=NULL;
struct file* files=NULL;
if ((curr_dir = opendir(dir)) == NULL) {
fprintf(stderr, "Can't Open %s\n", argv[1]);
exit(2);
}
while (((dir_inode = readdir(curr_dir))) != NULL) {
files=(struct file*) realloc(files,((j)+1)*(sizeof(char*)+sizeof(struct stat))); // Structure array reallocation
(files+(j))->name=(char *)(malloc(strlen(dir_inode->d_name)+1));//name allocation
for(i=0;i<strlen(dir_inode->d_name);i++)
(files+(j))->name[i]=dir_inode->d_name[i];//name storage
(files+(j))->name[i]='\0';
sum= (char *) malloc(strlen(dir)+strlen(dir_inode->d_name)+2);//To add file name to its directory
for(i=0;i<strlen(dir);i++)
sum[i]=dir[i];
sum[i]='/';
i++;
for(k=0;dir_inode->d_name[k]!='\0';k++)
sum[i+k]=dir_inode->d_name[k];
sum[i+k]='\0';//file name with directory in sum
if( stat(sum,&((files+j)->fbuf)) == -1){ // the function gets information from the file name and stores them in fbuf
printf("error stat\n");
exit(1);
}
free(sum);
if( S_ISDIR( ( (files+(j))->fbuf ).st_mode ) ){
printf("d");
}
else {
printf("-");
}
//Here the output appears fine
//The output depends on accessing fbuf in files array
printf("statOK\n");
(j)++; // index
}
printf("%d %d %d\n",files,j,files+1);
printf("%d\n",j);
printf("\n\n\n\n");
for(i=0;i<j;i++){
printf("%s\n",(files+i)->name);
printf("%d\n",files);
//Starting from here, same syntax but outside the loop it gives the error
if( S_ISDIR( ( (files+i)->fbuf ).st_mode ) ){
printf("d");
else {
printf("-");
}
}
free(files);
free(dir);
closedir(curr_dir);
exit(1);
}
The code isn't complete yet but all what I want is to access the fbuf outside the loop, then I can complete it
Any ideas?
Bad size assumption
This allocation is wrong:
files=(struct file*) realloc(files,((j)+1)*(sizeof(char*)+sizeof(struct stat)));
Here, you assumed that the size of struct file was the sum of the sizes of its two components. But in fact, you don't know how that structure is packed and aligned, so the size of struct file could be larger than what you thought. You should just be using sizeof(struct file) instead:
files=(struct file*) realloc(files,(j+1)*(sizeof(struct file)));
Is there anyway for a C program to view the files in a certain directory and interface with them? For example, lets say that I downloaded a file using wget via the system() function, and I want to see what the file's name was. Is there any way for me to accomplish this through only the standard C library or the POSIX library?
This looks for files in a directory that were modified less than 5 seconds ago - there is NO error checking.
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include <dirent.h>
void dirchk(const char *arg) // arg=name of directory
{
time_t when = time(NULL) -5; // 5 secs ago
struct stat st;
DIR *dirp=opendir(arg);
struct dirent *d=readdir(dirp);
while (d != NULL)
{
if ((d = readdir(dirp)) != NULL)
{
stat(d->d_name, &st);
if( when - st.st_mtime <=5 )
printf("%s\n", d->d_name);
}
}
closedir(dirp);
return;
}
int main()
{
dirchk(".");
return 0;
}
"." is the current working directory.
So I've been looking around for a while, and can't find an answer to my question...
I am attempting to create a program that, when a directory is inputted in a command line argument, will access each individual file in the directory and save the file name in a linked list. Then, the program will tokenize the contents of the file, and save each token to a linked list headed by the appropriate file name. The list will be constructed like this:
[ListHeader][file1.txt][file2.txt][file3.txt]...[filen.txt]
[token 1 ][token 1 ][token 1 ]...[token 1 ]
[token 2 ][token 2 ][token 2 ]...[token 2 ]
[token n ][token n ][token n ]...[token n ]
Now, I have finished the tokenizer/linked list function. I am having no trouble in the current working directory with opening files using the filestream fopen() command. What I am having trouble with, however, is opening a file in a new directory.
For example, let's say I input:
user#usermachine:~/Cprograms$ gcc -o d directorytokenizer.c
user#usermachine:~/Cprograms$ ./d test.txt
Where test.txt is in the same directory (in the above case Cprograms) as the program itself.
This example works fine.
However, if the input is
user#usermachine:~/Cprograms$ gcc -o d directorytokenizer.c
user#usermachine:~/Cprograms$ ./d testdirectory
where "testdirectory" is a directory contained in the directory Cprograms (testdirectory is a folder in Cprograms).
My program opens the directory fine, and it can output the filenames within the directory (I did this to debug, and it worked). However, when I try to pass the individual filenames into a function that filestreams them (fopen()), the filestream command can't find the file.
Here are the relevant portions of my code:
//Sortedlist.h-
#ifndef SORTED_LIST_H
#define SORTED_LIST_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
struct SLNodes
{
char *word;
struct SLNodes * next;
int occurencecounter;
};
typedef struct SLNodes* SLNode;
struct SortedList_
{
char * filename;
SLNode Listhead;
struct SortedList_ * nextfile;
};
typedef struct SortedList_* SortedList;
SortedList SLCreate();
void tokenstore(char *inputstr, SortedList NewList);
#endif
//indexer.c (truncated)-
//this is the mainfile
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include "listheader.h"
#include "tokenstore.c"
#include "diropen.c"
int main(int argc, char *argv[])
{
DIR *dip;
if (argc < 2)
usage(argc, argv[0]);
if ((dip = opendir(argv[1])) == NULL)
{
printf("Not a directory, now attempting to open file...\n");
SortedList NewList = CreateNewList(argv[1]);
NewList = tokenize(NewList, argv[1]);
SLNode curr = NewList->Listhead;
if (curr != NULL)
{
SLNode curr = NewList->Listhead;
printf("The following alphabetized and tokenized list of words is in the file: %s\n", NewList->filename);
while(curr != NULL)
{
printf("<listbegin>\n");
printf("The word <%s> appears <%d> times\n", curr->word, curr->occurencecounter);
printf("<endlist>\n\n");
curr = curr->next;
}
}
else
printf("File did not contain any words. Please make sure to input a file with words in it. Remember, words consist of upper case and lower case letters, and numbers... Nothing else.\n");
}
else
{
SortedList NewList = CreateNewList(argv[1]);
printf("You have input a directory. Now attempting to open and sort...\n");
openadir(argv[1], NewList);
}
}
//diropen.c (truncated)-
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
SortedList openadir(char *dirname, SortedList NewList)
{
DIR *inputdir;
struct dirent *dirstruct;
SortedList placeholder;
int i = 0;
/* DIR *opendir(const char *name);
*
* Open a directory stream to argv[1] and make sure
* it's a readable and valid (directory) */
if ((inputdir = opendir(dirname)) == NULL)
{
perror("opendir");
return;
}
printf("Directory stream is now open\n");
/* struct dirent *readdir(DIR *dir);
*
* Read in the files from argv[1] and print */
while ((dit = readdir(dip)) != NULL)
{
i++;
printf("\n%s\n", dit->d_name);
NewList->filename = dirstruct->d_name;
NewList = tokenize(NewList, dirstruct->d_name);
NewList->nextfile = malloc(sizeof(SortedList));
placeholder = NewList->nextfile;
placeholder->filename = NULL;
placeholder->Listhead = NULL;
placeholder->nextfile = NULL;
NewList = placeholder;
}
}
//tokenstore.c (truncated) -
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include "listheader.h"
SortedList tokenize(SortedList NewList, char *filename) //tokenize the input file
{
printf("filename is : %s\n", filename);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* The following function simply steps through the input file and records the total
* character count, in order to allocate a buffer of the correct size
* to contain the eventual tokenized string.
*
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
FILE *fc = fopen(filename, "r");
if(fc == NULL)
fatal(": Input file does not exist");
int hold;
int count=0;
do
{
hold = fgetc(fc);
count++;
}
while( hold != EOF );
fclose(fc);
}
Now, as I said earlier, inputting a filename that is in the same directory as the program works fine, it's when I try to pass in filenames to fopen from a different directory that the program throws a fatal: file not found. (fatal error function is not included in the source code I posted, but it works fine).
My question is, how do I pass filenames from another directory into fopen() in such a manner that the program will open the files?
Thank you for taking the time to read all this.
The d_name member of the struct dirent contains, as you have noticed, only the file name. It does not contain the directory name.
All you need to do is to concatenate the directory name, directory separator, and that d_name string to get a relative path that you can use with fopen.
Also note:
NewList->filename = dirstruct->d_name;
This is not good. You must copy that string if you want to retain it.
Your tokenize function is declared are returning a SortedList, but it doesn't return anything.
Please make sure you've turned on your compiler's warnings.
You could first open the directory with open and then access the files inside it with openat.