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)));
Related
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?
I'm trying to find all groups to which user belongs in my UNIX system, and that for each user.Implementation has to be in C. Here is my code:
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
static void error_fatal(char* msg)
{ perror(msg); exit(EXIT_FAILURE); }
int main(int argc, char** argv) {
struct group* grp;
struct passwd* pwd;
char *name;
int i = 0;
setpwent();
while((pwd = getpwent()) != NULL){
if( ( name = (char*) malloc( (strlen(pwd->pw_name)+1)*sizeof(char))) == NULL ) error_fatal("malloc");
strcpy(name, pwd->pw_name);
printf("%s:\n", name);
setgrent();
while( (grp = getgrent()) != NULL ) {
for( i=0; i < (sizeof(grp->gr_mem)/sizeof(grp->gr_mem[0])); i++ ){
if( /*strlen(&grp->gr_mem[i][0]) == strlen(name) && */ !strcmp(grp->gr_mem[i], name) )
printf("%s\n", name);
} }
endgrent();
free(name);
}
endpwent();
return 0;
}
But I get segmentation fault after "root:" output.
I'm pretty sure the problem is in accessing list of members in the fourth field of /etc/group file (see man 5 group for details).
So, basically my problem would be to find out how many members each group has, so my counter(i in program, the last for loop) would have nice upper boundary.
Your problem is here:
for( i=0; i < (sizeof(grp->gr_mem)/sizeof(grp->gr_mem[0])); i++ ){
struct group is defined as:
struct group {
char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* NULL-terminated array of pointers
to names of group members */
};
You're assuming gr_mem is an array but it is not. It is a pointer pointing to the first element of an array. So sizeof(grp->gr_mem)/sizeof(grp->gr_mem[0]) gives you the size of a pointer, probably 8 on your system. So if a user has less than 8 groups, you'll end up reading past the end of the array gr_mem points to the start of.
Because the array pointed to by gr_mem is NULL terminated, finding that terminator tells you when the loop is done:
for( i=0; grp->gr_mem[i]; i++ ){
Running your code I detected your issue is not verifying grp->gr_mem[i] == NULL before using it in strcmp call:
if (grp->gr_mem[i] == NULL)
continue;
Adding those lines before strcmp call worked for me.
Also, don't forget to free the memory you are using. I don't know if this is your complete code, but here you should considering using free(name) within your while loop.
I'm very new to C and having trouble returned a pointer to a two dimensional array.
The purpose of the code is to go into a folder called rules. From this folder it finds all the file paths for all the files in the rule folder. I want to populate the a two dimensional array with the complete file paths. As of now my code is capable of populating a two dimensional array with the the filepaths (this is done in the listFiles Method). I would like to use this two dimensional array within the main method, to do some further stuff. But I am having issues trying to get it to return, without causing compiling issues.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#include <string.h>
void listFiles(char *path);
int main() {
// Directory path to list files
char path[100];
char buff[FILENAME_MAX];
GetCurrentDir(buff, FILENAME_MAX);
// printf("%s\n",buff);
char toRuleFolder[100] = "/rules";
strcat(buff, toRuleFolder);
// printf("%s\n",buff);
listFiles(buff);
return 0;
}
void listFiles(char *path) {
char pathToUse[100];
struct dirent *dp;
char *arrayOfArraysOfChars[30][50]; // array to hold multiple single
// arrays of characters
DIR *dir = opendir(path);
printf(" %s\n", path);
return;
char *token;
const char s[2] = "-";
int counter = 0;
char pathToSave[100];
while ((dp = readdir(dir)) != NULL) {
token = strtok(dp->d_name, "");
while (token != NULL) {
if (strcmp(token, ".") != 0) {
if (strcmp(token, "..") != 0) {
strcpy(pathToSave, "");
strcpy(pathToSave, path);
strcat(pathToSave, "/");
strcat(pathToSave, token);
strcpy(arrayOfArraysOfChars[counter], pathToSave);
counter += 1;
}
}
token = strtok(NULL, s);
}
}
printf("%s\n", "sdasdasdssad");
printf("%s\n", arrayOfArraysOfChars[0]);
printf("%s\n", arrayOfArraysOfChars[1]);
printf("%s\n", arrayOfArraysOfChars[2]);
printf("%s\n", arrayOfArraysOfChars[3]);
closedir(dir);
}
arrayOfArraysOfChars is populated with the information I need. But I would like to be able to access this array from the main function. How would I do this?
I am assuming your compiler is warning you about returning local variables.
char *arrayOfArraysOfChars[30][50]
The memory used by this variable will be reused for the next stack frame when the function is finished, overwriting it and making it useless.
a) Pass it in as a parameter
b) Make it static (yuk)
c) Dynamically allocate it and return the pointer
Also, consider using a linked list of allocated strings rather than an array as I guess you don't know for sure how many files you're going to find or how long their names are.
This function is supposed to get a parameter as the pointer of a file and put all file into the struct anagram, then write it to another file. Right now the data only contains a.word, but it suppose to containst a.sorted too? I have check the a.sorted using printf
and it printf out the correct data, but why its not writing to the data file?
It still cant get the a.sorted even if i increase the count of the frwite
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "anagrams.h"
#define SIZE 80
//struct
struct anagram {
char word[SIZE];
char sorted[SIZE];
};
void buildDB ( const char *const dbFilename ){
FILE *dict, *anagramsFile;
struct anagram a;
//check if dict and anagram.data are open
errno=0;
dict= fopen(dbFilename, "r");
if(errno!=0) {
perror(dbFilename);
exit(1);
}
errno=0;
anagramsFile = fopen(anagramDB,"wb");
char word[SIZE];
char *pos;
int i=0;
while(fgets(word, SIZE, dict) !=NULL){
//get ripe of the '\n'
pos=strchr(word, '\n');
*pos = '\0';
strncpy(a.word,word,sizeof(word));
//lowercase word
int j=0;
while (word[j])
{
tolower(word[j]);
j++;
}
/* sort array using qsort functions */
qsort(word,strlen(word), 1, charCompare);
strncpy(a.sorted,word,sizeof(word));
//printf(a);
fwrite(&a,1,strlen(word)+1,anagramsFile);
i++;
}
fclose(dict);
fclose(anagramsFile);
}
it suppose to contains data with a.sorted for example "10th 01ht"
data:
fwrite(&a,1,strlen(word)+1,anagramsFile); should have been fwrite(a.sorted,1,strlen(a.sorted)+1,anagramsFile); I assume the declaration of sorted as char sorted[SOME_LEN];
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.