Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bismillah
- #include <stdio.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <pwd.h>
- #include <grp.h>
- #include <time.h>
- #include <locale.h>
- #include <langinfo.h>
- #include <stdint.h>
- #include <error.h>
- #include <pthread.h>
- #define MAX_THREAD 10 /*maximum Thread sayisi*/
- #ifndef PATH_MAX
- #define PATH_MAX 255
- #endif
- #define MAX_FILE 1000
- void* recursion(void *path);
- int isdirectory(char *path) {
- struct stat statbuf;
- if (stat(path, &statbuf) == -1)
- return 0;
- else
- return S_ISDIR(statbuf.st_mode);
- }
- int dcounter = 0, fcounter = 0, isdir = 0;
- char directorypath[MAX_THREAD][PATH_MAX];
- char filespath[MAX_FILE][PATH_MAX];
- /* index of thread id array */
- int i = 0;
- pthread_t tid[MAX_THREAD];
- int main(int argc, char **argv) {
- /* variables*/
- /*current directory path*/
- char current[PATH_MAX];
- /*temprory file or directory name*/
- char *name;
- /*directory structor*/
- struct dirent *direntp;
- /*Directories pointer*/
- DIR *dirptr;
- /*buffersize*/
- int bufsize;
- /*a file variable*/
- int fd[2];
- /*file pointer*/
- FILE* fptr;
- void *deneme;
- int error;
- /*usage*/
- if (argc < 0) {
- fprintf(stderr, "Usage: %s \n", argv[0]);
- return 2;
- }
- /*current directory path*/
- if (getcwd(current, PATH_MAX) == NULL) {
- perror("Failed the get current working directory");
- exit(0);
- }
- /*create pipe*/
- if (pipe(fd) == -1) {
- perror("Failed to create the pipe");
- return 1;
- }
- //printf("tid = %d \n" , (int)tid);
- //deneme = (void *) current;
- //printf("Current working directory = %s \n", (char*) deneme);
- if (pthread_create(&tid[i], NULL, recursion, (void*) current)) {
- fprintf(stderr, "Cannot create thread\n");
- exit(1);
- }
- // threadin isini sonlandirmasi beklendi
- if (error = pthread_join(tid[i], NULL))
- fprintf(stderr, "Failed to join thread:\n%s\n", strerror(error));
- return 0;
- }
- void* recursion(void *mypath) {
- char temp[PATH_MAX];
- DIR *dirptr;
- /*directory structor*/
- struct dirent *direntp;
- /*temprory file or directory name*/
- char *name;
- char *path = (char *) mypath;
- int error;
- //printf("buraya geliyor mu\n");
- printf("tid = %ld \n", (long int) pthread_self());
- /*gelen directory acilir*/
- if ((dirptr = opendir(path)) == NULL)
- fprintf(stderr, "[%ld]Failed the open directory %s \n", (long) getpid(), path);
- strcpy(temp, path);
- while ((direntp = readdir(dirptr)) != NULL) {
- name = direntp->d_name;
- if ((strcmp(name, "..") == 0) || (strcmp(name, ".") == 0))
- continue;
- strcat(temp, "/");
- strcat(temp, name);
- printf("%s \n", temp);
- /*Directory path arrayi olusturulur*/
- if (isdirectory(temp)) {
- strcpy(directorypath[dcounter], temp);
- dcounter++;
- }
- /*File pathleri olusturulur*/
- else {
- strcpy(filespath[fcounter], temp);
- fcounter++;
- }
- strcpy(temp, path);
- } // end while
- while (dcounter > 0){
- dcounter--;
- i++;
- if (pthread_create(&tid[i], NULL, recursion, (void*) directorypath[dcounter])) {
- fprintf(stderr, "Cannot create thread\n");
- exit(1);
- }
- if (error = pthread_join(tid[i], NULL))
- fprintf(stderr, "Failed to join thread %s\n", strerror(error));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement