findfile 関数をコードに取り込もうとしていますが、「セグメンテーション エラー (コア ダンプ)」というエラーが表示されます。これが何を意味するのかは理解していますが、コードでエラーを見つけることができません。正しい方向に私は、私はそれをいただければ幸いです。
コードは次のとおりです。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "find.h" // utilize findfile function
#define NAMESIZE 256
#define TOKENSIZE 100
int main(int argc, char *argv[])
{
int choice = -1;
char *input_dir_name, *dirpath, *chptr;
struct stat statbuf;
input_dir_name = (char *) malloc(NAMESIZE * sizeof(char));
dirpath = (char *) malloc(NAMESIZE * sizeof(char));
printf("\n");
printf("SELECT THE FUNCTION YOU WANT TO EXECUTE:\n");
printf("1. Find the 3 largest files in a directory\n");
printf("2. List all zero length files in a directory\n");
printf("3. Find all files with permission 777 in a directory\n");
printf("4. Create a backup of a directory\n");
printf("\n");
printf("ENTER YOUR CHOICE: ");
scanf("%d", &choice);
printf("Enter a directory name in the current directory: ");
scanf("%s", &input_dir_name);
/**********************************************************/
printf(input_dir_name);//Segmentation Fault (core dumped)
/*Form a full path to the directory and check if it exists*/
/**********************************************************/
if(choice == 1){
printf("\nEXECUTING \"1. Find the 3 largest files in a directory\"\n");
/********************************************************/
/**************Function to perform choice 1**************/
/********************************************************/
}
else if(choice == 2){
printf("\nEXECUTING \"2. List all zero length files in a directory\"\n");
/********************************************************/
/**************Function to perform choice 2**************/
/********************************************************/
}
else if(choice == 3){
printf("\nEXECUTING \"3. Find all files with permission 777 in a directory\"\n");
/********************************************************/
/**************Function to perform choice 3**************/
/********************************************************/
}
else if(choice == 4){
printf("\nEXECUTING \"4. Create a backup of a directory\"\n");
/********************************************************/
/**************Function to perform choice 4**************/
/********************************************************/
}
else{
printf("Invalid choice\n");
exit(1);
}
free(input_dir_name);
free(dirpath);
return 0;
}
================================================== ==================================== ここに私のfindfile関数を含むfind.hファイルがあります:
#include <stdio.h> // Standard input/output routines
#include <stdlib.h> // Standard library routines
#include <dirent.h> // readdir(), etc.
#include <sys/stat.h> // stat(), etc.
#include <string.h> // strstr(), etc.
#include <unistd.h> // getcwd(), etc.
#define MAX_DIR_PATH 2048 // Maximal full path length we support
/*===========================================================================
*
* Function: findfile recusively traverses the current directory, searching
* for files with a given string in their name. Hence strstr().
* Input: String to match.
* Output: Any file found, printed to the screen with a full path.
*
============================================================================*/
void findfile (char *pattern)
{
DIR *dir; // Pointer to the scanned directory
struct dirent *entry; // Pointer to one directory entry
char cwd[MAX_DIR_PATH + 1]; // Current working directory
struct stat dir_stat; // Used by stat()
// First, save the path of current working directory
if (!getcwd (cwd, MAX_DIR_PATH + 1))
{
perror ("getcwd:");
return;
}
// Open the directory to read
dir = opendir (".");
if (!dir)
{
fprintf (stderr, "Cannot read directory '%s': ", cwd);
perror ("");
return;
}
/*=======================================================
*
* Scan the directory, traversing each sub-directory, and
* matching the pattern for each file / directory name.
*
========================================================*/
while ((entry = readdir (dir)))
{
// Check if the pattern matches
if (entry->d_name && strstr (entry->d_name, pattern))
{
printf ("%s/%s\n", cwd, entry->d_name);
}
// Check if the given entry is a directory
if (stat (entry->d_name, &dir_stat) == -1)
{
perror ("stat:");
continue;
}
// Skip the "." and ".." entries, to avoid loops
if (strcmp (entry->d_name, ".") == 0)
continue;
if (strcmp (entry->d_name, "..") == 0)
continue;
/* Is this a directory? */
if (S_ISDIR (dir_stat.st_mode))
{
/* Change into the new directory */
if (chdir (entry->d_name) == -1)
{
fprintf (stderr, "Cannot chdir into '%s': ", entry->d_name);
perror ("");
continue;
}
/* check this directory */
findfile (pattern);
/* Finally, restore the original working directory. */
if (chdir ("..") == -1)
{
fprintf (stderr, "Cannot chdir back to '%s': ", cwd);
perror ("");
exit (1);
}
}
}
}