在我的程序中使用findfile

时间:2013-02-26 17:32:50

标签: c recursion header find

我正在尝试将findfile函数合并到我的代码中,我收到错误“Segmentation error(core dumped)。我明白这意味着什么,我只是在代码中找不到错误。如果有人可以指出它出去或引导我朝着正确的方向前进,我将不胜感激。

以下是代码:

#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);
    }
}
 }
 }

1 个答案:

答案 0 :(得分:0)

变化:

scanf(“%s”,&amp; input_dir_name);

为:

scanf(“%s”,input_dir_name);

BTW,头文件(* .h)通常不用于实现功能。它们通常仅用于声明函数,宏和变量。实现(定义)在 .c / .cpp文件中完成。