Rev 37 | Rev 53 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | ls | 1 | /* ============================================================================ |
| 2 | # Create |
||
| 43 | - | 3 | # name : Andre Rikkert de Koe date: Apr 2011 |
| 2 | ls | 4 | # descr : filefind |
| 5 | # This is a demo of programming in the C language. |
||
| 6 | # Its part of a project to implement a program to create a directory |
||
| 7 | # tree similar to the output of the Unix find program. |
||
| 8 | # EUID : Any user who has read access to all the files being searched for. |
||
| 9 | # run : interactive |
||
| 10 | # |
||
| 11 | # Changes |
||
| 12 | # name : date: |
||
| 13 | # descr : short description |
||
| 14 | # |
||
| 15 | ============================================================================ */ |
||
| 16 | |||
| 23 | - | 17 | #include <string.h> |
| 18 | |||
| 2 | ls | 19 | #include <stdio.h> |
| 20 | #include <libgen.h> |
||
| 21 | #include "checkfiles.h" |
||
| 22 | #include <unistd.h> // getcwd |
||
| 23 | |||
| 24 | |||
| 25 | #include <sys/types.h> |
||
| 26 | #include <sys/stat.h> |
||
| 27 | #include <dirent.h> |
||
| 28 | #include <unistd.h> |
||
| 29 | |||
| 30 | #include <errno.h> |
||
| 31 | |||
| 24 | - | 32 | #define PATH_MAX 255 |
| 2 | ls | 33 | |
| 34 | // ============================================================================ |
||
| 35 | // Functions |
||
| 36 | // ============================================================================ |
||
| 37 | |||
| 38 | void Usage(char *progname, char *mesg) |
||
| 39 | { |
||
| 40 | printf("Usage: %s directory\n" ,progname); |
||
| 41 | printf("%s\n", mesg); |
||
| 42 | } |
||
| 43 | |||
| 44 | // http://www.ibm.com/developerworks/aix/library/au-unix-readdir.html |
||
| 45 | |||
| 46 | void traverse(char *directory) |
||
| 47 | { |
||
| 48 | char curdir[PATH_MAX]; |
||
| 49 | char curdir2[PATH_MAX]; |
||
| 50 | |||
| 51 | getcwd(curdir, PATH_MAX); |
||
| 52 | chdir(directory); |
||
| 53 | getcwd(curdir2, PATH_MAX); |
||
| 54 | |||
| 55 | DIR *dir = NULL; |
||
| 56 | struct dirent entry; |
||
| 57 | struct dirent *entryPtr = NULL; |
||
| 58 | int retval = 0; |
||
| 59 | char pathName[PATH_MAX + 1]; |
||
| 60 | char pfile[PATH_MAX + 1]; |
||
| 61 | |||
| 62 | /* Open the given directory, if you can. */ |
||
| 63 | dir = opendir(curdir2); |
||
| 64 | if (dir == NULL) |
||
| 65 | { |
||
| 66 | printf( "Error opening %s: %s", directory, strerror( errno ) ); |
||
| 67 | } |
||
| 68 | retval = readdir_r( dir, &entry, &entryPtr ); |
||
| 69 | while( entryPtr != NULL ) |
||
| 70 | { |
||
| 71 | struct stat entryInfo; |
||
| 72 | |||
| 73 | if( ( ! strncmp( entry.d_name, ".", PATH_MAX ) == 0 ) && |
||
| 74 | ( ! strncmp( entry.d_name, "..", PATH_MAX ) == 0 ) ) |
||
| 75 | { |
||
| 76 | sprintf(pfile,"%s/%s", curdir2, entry.d_name); |
||
| 77 | if (isdir(entry.d_name) && ! issymlink(entry.d_name)) |
||
| 78 | { |
||
| 79 | printf("%s\n", pfile); |
||
| 80 | traverse(entry.d_name); |
||
| 81 | } |
||
| 82 | else |
||
| 83 | { |
||
| 84 | if ((isregular(entry.d_name)) || issymlink(entry.d_name)) |
||
| 85 | printf("%s\n", pfile); |
||
| 86 | else |
||
| 87 | printf("WARRNING: Could not process file %s\n", entry.d_name); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | retval = readdir_r( dir, &entry, &entryPtr ); |
||
| 91 | } |
||
| 92 | chdir(curdir); |
||
| 93 | } |
||
| 94 | |||
| 95 | void start_traverse(char *directory) |
||
| 96 | { |
||
| 97 | printf("%s\n", directory); |
||
| 98 | traverse(directory); |
||
| 99 | } |
||
| 100 | |||
| 101 | // ============================================================================ |
||
| 102 | // MAIN |
||
| 103 | // ============================================================================ |
||
| 104 | |||
| 23 | - | 105 | int main(int argc, char *argv[]) |
| 2 | ls | 106 | { |
| 107 | char *progname = argv[0]; |
||
| 108 | char *directory; |
||
| 109 | char mesg[80]; |
||
| 110 | |||
| 111 | progname = basename(progname); |
||
| 112 | if (argc == 2) |
||
| 113 | { |
||
| 114 | directory = argv[1]; |
||
| 115 | if (isdir(directory)) |
||
| 116 | { |
||
| 117 | start_traverse(directory); |
||
| 118 | } |
||
| 119 | else |
||
| 120 | { |
||
| 37 | - | 121 | sprintf(mesg, "ERROR: Not a directory : %s", directory); |
| 2 | ls | 122 | Usage(progname, mesg); |
| 123 | } |
||
| 124 | } |
||
| 125 | else |
||
| 126 | { |
||
| 37 | - | 127 | Usage(progname, "ERROR: 1 argument expected"); |
| 2 | ls | 128 | } |
| 129 | } |