void numberOfDirectoryAndFile(const char *directory_path, int *idrec_counter, int *ifile_counter) {

    DIR *directory_pointer;
    struct dirent *dirent_pointer;
    char sub_directory_path[BUFFER];

    directory_pointer = opendir(directory_path);


    if (directory_pointer == NULL) {

        fprintf(stderr, "\nDirectory couldn't open..!\n");

    } else {

        while ((dirent_pointer = readdir(directory_pointer)) != NULL) {

            if (strcmp(dirent_pointer->d_name, ".") != 0 && strcmp(dirent_pointer->d_name, "..") != 0) {

                sprintf(sub_directory_path, "%s/%s", directory_path, dirent_pointer->d_name);

                if (isdirectory(sub_directory_path) != 0) {
                    ++(*idrec_counter);
                    numberOfDirectoryAndFile(sub_directory_path, idrec_counter, ifile_counter);
                } else if (dirent_pointer->d_name[strlen(dirent_pointer->d_name) - 1] != '~') {
                    ++(*ifile_counter);
                }

            }
        }

        closedir(directory_pointer);
    }

}
static COUNTS *
chrcount(char *path)
{
    char temp[1024];
    char leaf[1024];
    size_t used = 0;
    size_t need = 2;
    COUNTS *result = typeCalloc(need, COUNTS);

    if (isdirectory(path)) {
	FILE *pp = popen("ls -1 -a", "r");
	if (pp != 0) {
	    while (fgets(leaf, sizeof(leaf), pp) != 0) {
		trim(leaf);
		sprintf(temp, "%s/%s", path, leaf);
		if ((used + 1) >= need) {
		    need = (used + 1) * 2;
		    result = realloc(result, sizeof(COUNTS) * need);
		}
		if ((result[used].count = do_count(temp)) >= 0) {
		    result[used].name = strmalloc(leaf);
		    used++;
		    result[used].name = 0;
		}
	    }
	    pclose(pp);
	}
    } else {
	result[0].name = strmalloc(path);
	result[0].count = do_count(path);
    }
    return result;
}
Exemple #3
0
static char		*return_arguments(char *res, char *str, glob_t list, char *c)
{
	if (ft_strcmp(res, ft_strjoin(str, "*")) == 0)
		return (NULL);
	if (isdirectory(res))
		res = ft_strjoin(res, "/");
	else
		res = ft_strjoin(res, " ");
	globfree(&list);
	return (ft_strjoin(ft_strsub(c, 0, (ft_strlen(c) - ft_strlen(str))), res));
}
Exemple #4
0
static int
grep_or_recurse(char *filename, BOOL dir_recurse, BOOL show_filenames,
  BOOL only_one_at_top)
{
int rc = 1;
int sep;
FILE *in;

/* If the file is a directory and we are recursing, scan each file within it.
The scanning code is localized so it can be made system-specific. */

if ((sep = isdirectory(filename)) != 0 && dir_recurse)
  {
  char buffer[1024];
  char *nextfile;
  directory_type *dir = opendirectory(filename);

  if (dir == NULL)
    {
    fprintf(stderr, "pcregrep: Failed to open directory %s: %s\n", filename,
      strerror(errno));
    return 2;
    }

  while ((nextfile = readdirectory(dir)) != NULL)
    {
    int frc;
    sprintf(buffer, "%.512s%c%.128s", filename, sep, nextfile);
    frc = grep_or_recurse(buffer, dir_recurse, TRUE, FALSE);
    if (frc == 0 && rc == 1) rc = 0;
    }

  closedirectory(dir);
  return rc;
  }

/* If the file is not a directory, or we are not recursing, scan it. If this is
the first and only argument at top level, we don't show the file name (unless
we are only showing the file name). Otherwise, control is via the
show_filenames variable. */

in = fopen(filename, "r");
if (in == NULL)
  {
  fprintf(stderr, "pcregrep: Failed to open %s: %s\n", filename, strerror(errno));
  return 2;
  }

rc = pcregrep(in, (filenames_only || (show_filenames && !only_one_at_top))?
  filename : NULL);
fclose(in);
return rc;
}
void create_fifos(const char *directory_path, char charArr[BUFFER][BUFFER]) {

    DIR *directory_pointer;
    struct dirent *dirent_pointer;
    char sub_directory_path[BUFFER];

    char fifo_name[BUFFER];
    int i = 0;

    directory_pointer = opendir(directory_path);


    if (directory_pointer == NULL) {

        fprintf(stderr, "\nDirectory couldn't open..!\n");

    } else {

        while ((dirent_pointer = readdir(directory_pointer)) != NULL) {

            if (strcmp(dirent_pointer->d_name, ".") != 0 && strcmp(dirent_pointer->d_name, "..") != 0) {

                sprintf(sub_directory_path, "%s/%s", directory_path, dirent_pointer->d_name);

                if (isdirectory(sub_directory_path) != 0) {

                    sprintf(fifo_name, "%d-%s", getpid(), dirent_pointer->d_name);
                    strcpy(charArr[i++], fifo_name);

                    if (mkfifo(fifo_name, 0666) == -1) {
                        perror("Failed to create FIFO...");
                        exit(EXIT_FAILURE);
                    }

                    sprintf(fifo_name_array[fifo_name_counter++], "%s", fifo_name);

                }
            }
        }

        closedir(directory_pointer);
    }

}
int grepfromDir(char *directory_path, const char *grep_name) {

    int temp_grep = 0;
    int total_grep_dir = 0;
    int totalGrep = 0;

    int idirec_counter = 0; /*Number of directory*/
    int ifile_counter = 0; /*Number of file*/

    char fifo_name[BUFFER];
    char fifo_name_arr[BUFFER][BUFFER];

    DIR *directory_pointer;
    struct dirent *dirent_pointer;
    char sub_directory_path[BUFFER];

    int fifowr; /*File descriptor to read from fifo*/
    int fiford; /*File descriptor to read write fifo*/

    int i = 0;
    int temp = 0;
    int thread_counter = 0; /*Thread process counter*/

    int status; /*Status fork process*/

    char direc_name[BUFFER];
    const char s[2] = "/";
    char *token;

    pid_t child;

    int pipe_array[2];

    pthread_t thread_array[BUFFER]; /*Thread array*/
    struct GrepData grep_data_array[BUFFER]; /*GrepData structure array to send necessary grep information thread process*/


    /*Finds directory and file number in directory path*/
    numberOfDirectoryAndFile(directory_path, &idirec_counter, &ifile_counter);

    /*Creates fifos*/
    create_fifos(directory_path, fifo_name_arr);


    /*Creates pipe*/
    if (ifile_counter > 0) {
        if (pipe(pipe_array) == -1) {
            perror("Pipe failed...");
            exit(EXIT_FAILURE);
        }
        /*Writes temporary zero value*/
        write(pipe_array[1], &temp, sizeof (int));

    }


    /*Open directory*/
    directory_pointer = opendir(directory_path);

    if (directory_pointer == NULL) {

        fprintf(stderr, "\nDirectory couldn't open..!\n");

    } else {

        while ((dirent_pointer = readdir(directory_pointer)) != NULL) {

            /*Skip for current directory and parent directory*/
            if (strcmp(dirent_pointer->d_name, ".") != 0 && strcmp(dirent_pointer->d_name, "..") != 0 && dirent_pointer->d_name[strlen(dirent_pointer->d_name) - 1] != '~') {

                /*Sub directory name ( sub_directory_path )*/
                sprintf(sub_directory_path, "%s/%s", directory_path, dirent_pointer->d_name);

                if (isdirectory(sub_directory_path) != 0) { /*Directory*/

                    /*Fork process for directory*/
                    child = fork();

                    if (child == 0) {

                        grepfromDir(sub_directory_path, grep_name);

                        exit(EXIT_SUCCESS);

                    }

                } else if (isdirectory(sub_directory_path) == 0) { /*File*/

                    sprintf(grep_data_array[thread_counter].grep_name, "%s", grep_name);

                    grep_data_array[thread_counter].pipe_arr[0] = pipe_array[0];
                    grep_data_array[thread_counter].pipe_arr[1] = pipe_array[1];

                    sprintf(grep_data_array[thread_counter].file_path, "%s", sub_directory_path);


                    /*Thread process for file*/
                    if (pthread_create(&thread_array[thread_counter], NULL, grep_from_file_th, &grep_data_array[thread_counter])) {
                        fprintf(stderr, "Error in pthread_create!\n");
                        exit(EXIT_FAILURE);
                    }

                    ++thread_counter;

                }

            }

        }

        /*Close directories*/
        closedir(directory_pointer);
    }


    /*Reads grep numbers in fifo*/
    for (i = 0; i < idirec_counter; ++i) {

        sprintf(fifo_name, "%s", fifo_name_arr[i]);

        if ((fiford = open(fifo_name, O_RDONLY)) != -1) {
            read(fiford, &temp_grep, sizeof (int));
            close(fiford);

            /*Unlink fifo*/
            unlink(fifo_name);

            total_grep_dir += temp_grep;

        }
    }


    /*Waits all thread process*/
    for (i = 0; i < thread_counter; ++i) {
        if (pthread_join(thread_array[i], NULL) != 0) {
            fprintf(stderr, "Some error in wait thread process!");
            exit(EXIT_FAILURE);
        }
    }


    /*Waits all child*/
    while (wait(&status) != -1) {

        if (status != EXIT_SUCCESS) {
            fprintf(stderr, "Error in fork procces!----%d\n", status);
        }

    }


    /*Reads grep number from pipe*/
    if (ifile_counter > 0) {
        read(pipe_array[0], &temp_grep, sizeof (int));

    }


    /* Total dir grep */
    totalGrep = temp_grep + total_grep_dir;


    /* Wirtes to fifos if process is not root procces */
    if (getpid() != main_pid_flag) {

        token = strtok(directory_path, s);

        while (token != NULL) {
            strcpy(direc_name, token);

            token = strtok(NULL, s);

        }

        sprintf(fifo_name, "%d-%s", getppid(), direc_name);
        fifowr = open(fifo_name, O_WRONLY);
        write(fifowr, &totalGrep, sizeof (int));
        close(fifowr);

    }

    return totalGrep;
}
Exemple #7
0
static int
grep_or_recurse(char *pathname, BOOL dir_recurse, BOOL show_filenames,
  BOOL only_one_at_top)
{
int rc = 1;
int sep;
FILE *in;
char *printname;

/* If the file name is "-" we scan stdin */

if (strcmp(pathname, "-") == 0)
  {
  return pcregrep(stdin,
    (filenames_only || filenames_nomatch_only ||
    (show_filenames && !only_one_at_top))?
      stdin_name : NULL);
  }

/* If the file is a directory and we are recursing, scan each file within it,
subject to any include or exclude patterns that were set. The scanning code is
localized so it can be made system-specific. */

if ((sep = isdirectory(pathname)) != 0 && dir_recurse)
  {
  char buffer[1024];
  char *nextfile;
  directory_type *dir = opendirectory(pathname);

  if (dir == NULL)
    {
    if (!silent)
      fprintf(stderr, "pcregrep: Failed to open directory %s: %s\n", pathname,
        strerror(errno));
    return 2;
    }

  while ((nextfile = readdirectory(dir)) != NULL)
    {
    int frc, blen;
    sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile);
    blen = strlen(buffer);

    if (exclude_compiled != NULL &&
        pcre_exec(exclude_compiled, NULL, buffer, blen, 0, 0, NULL, 0) >= 0)
      continue;

    if (include_compiled != NULL &&
        pcre_exec(include_compiled, NULL, buffer, blen, 0, 0, NULL, 0) < 0)
      continue;

    frc = grep_or_recurse(buffer, dir_recurse, TRUE, FALSE);
    if (frc > 1) rc = frc;
     else if (frc == 0 && rc == 1) rc = 0;
    }

  closedirectory(dir);
  return rc;
  }

/* If the file is not a directory, or we are not recursing, scan it. If this is
the first and only argument at top level, we don't show the file name (unless
we are only showing the file name). Otherwise, control is via the
show_filenames variable. */

in = fopen(pathname, "r");
if (in == NULL)
  {
  if (!silent)
    fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pathname,
      strerror(errno));
  return 2;
  }

printname =  (filenames_only || filenames_nomatch_only ||
  (show_filenames && !only_one_at_top))? pathname : NULL;

rc = pcregrep(in, printname);

fclose(in);
return rc;
}