ftw program wont compile.

ftw program wont compile.

Post by Kirk Fielde » Mon, 16 Oct 2000 04:00:00



Hello all (again),
When I try to compile the following program(ot.c) I get the messages below.
Have tried removing/adding const struct various places (and changed stb to
st) but cant seem to get it to compile. Can anyone fix it?
I will be grateful for any help as I have been stuck on this problem for a
long time (only got this far with help and trial and error) and am out of my
depth(but trying).
My main concern is if you give the command line a -R option ftw is invoked
and working.
Thanking you in advance,
Kirk.
eg $ot -R 500 temp
returns any files in temp that are >500bytes
$ot -sR 500 temp
returns any files in temp that are <500bytes
COMPILER ERRORS:
ot.c: In function `largers':
ot.c:89: warning: passing arg 2 of `stat' discards qualifiers from pointer
target type
ot.c: In function `largerls':
ot.c:101: warning: passing arg 2 of `stat' discards qualifiers from pointer
target type
ot.c:102: warning: passing arg 1 of `printf' makes pointer from integer
without a cast
ot.c: In function `largerl':
ot.c:112: warning: passing arg 2 of `stat' discards qualifiers from pointer
target type
ot.c: In function `largeri':
ot.c:122: warning: passing arg 2 of `stat' discards qualifiers from pointer
target type
Undefined                       first referenced
 symbol                             in file
largerlsR                           /var/tmp/ccvgbr7T.o
ld: fatal: Symbol referencing errors. No output written to trey
collect2: ld returned 1 exit status
PROGRAM SOURCE ot.c:
#include<sys/types.h>
#include<sys/stat.h>
#include<math.h>
#include <stdlib.h>
#include <stdio.h>
#include<dirent.h>
#include <ftw.h>
#include <unistd.h>
   int  l_opt = 0, s_opt = 0, R_opt = 0, size = 0 , k=0, i=0;
   int status_flag = 0;
   int size1 = 0;
   void largers(char *filenames[], int filenum);
   void largerls(char *filenames[], int filenum);
   int process(const char *, const struct stat *st, int);
   void largerl(char *filenames[], int filenum);
   void largeri(char *filenames[], int filenum);
   int main(int argc, char **argv)
   {
      int           optchar;
      int           integer;
      char          *filenames[10];
      int           filenum = 0;
      const char c = '.';
      extern char   *optarg;                          /* Is set to start of
argument value */
      extern int    optind;                           /* Is set to index in
argv[] of next argument */
      extern int    opterr;                           /* Tells getopt to
print error msg */
      extern int    optopt;

      if (argc < 2)
      {
         fprintf(stderr, "usage: larger [-opts] size [ file... ]\n");
         exit(2);
      }
      while (optind < argc) { /* Loop through all arguments to program */

      /* Get options, if any. Will stay in this loop as long as the
arguments
       are options (starting with "-") */
         while ((optchar = getopt(argc, argv, "lsR")) != EOF)

            switch (optchar) {
               case 'l':
                  l_opt = 10;
                  break;
               case 's':
                  s_opt = 21;
                  break;
               case 'R':
                  R_opt = 32;
                  break;
               default :
                  printf("Unrecognized option %c\n", optopt);
                  break;
            }
         }
      /* Argument was not an option, so get integer or filename */
         if (atoi(argv[optind]) > 0) {
            size = atoi(argv[optind]);
         }
         else

            filenames[filenum++] = argv[optind];
         }
         optind++;
      } /* as long as there are more arguments */
      size1 = atoi(argv[1]);
      if(size1)

         largeri(filenames, filenum);
         exit(1);
      }
      if(s_opt)

         if(l_opt)

            if(R_opt)

               largerlsR(filenames, filenum);
            }
            largerls(filenames, filenum);
         }
         else  largers(filenames, filenum);
      }
      else if(l_opt) {  largerl(filenames, filenum);}
      if(R_opt)

         if (!filenum) {
            ftw(*argv , process ,sysconf(_SC_OPEN_MAX) - 3 ); }
      }
   }
   void largers(char *filenames[],int filenum)
   {
      int f_size = 0;
      int k = 0;
      const struct stat st;
      for(k=0;k < filenum; k++)

         f_size = stat(filenames[k],&st);
         if (st.st_size < size){
            printf("%s\n", filenames[k]);
         }
      }
   }
   void largerls(char *filenames[],int filenum)

      int f_size = 0;
      int k = 0;
      const struct stat st;
      for(k=0;k < filenum; k++)

         f_size = stat(filenames[k],&st);
         if (st.st_size < size) printf(st.st_size, filenames[k]);
      }
   }

   void largerl(char *filenames[],int filenum)

      int f_size = 0;
      int k = 0;
      const struct stat st;
      for(k=0;k < filenum; k++)

         f_size = stat(filenames[k],&st);
         if (st.st_size > size) printf("%d %s\n", st.st_size, filenames[k]);
      }
   }
   void largeri(char *filenames[],int filenum)
   {
      int f_size = 0;
      int k = 0;
      const struct stat st;
      for(k=0;k < filenum; k++)

         f_size = stat(filenames[k],&st);
         if (st.st_size > size){
            printf("%s\n", filenames[k]);
         }
      }
   }
   int process(const char *path, const struct stat *st, int flag)
   {
      printf("%-24s", path);

      switch (flag) {  /*st->.. needs work*/
         case FTW_F:
            printf("file, mode %o\n", st->st_mode & 07777);
            break;
         case FTW_D:
            printf("directory, mode %o\n", st->st_mode & 07777);
            break;
         case FTW_DNR:
            printf("unreadable directory, mode %o\n", st->st_mode & 07777);
            break;
         case FTW_NS:
            printf("unknown; stat() failed\n");
            break;
      }
   }

 
 
 

ftw program wont compile.

Post by Fletcher Glen » Mon, 16 Oct 2000 04:00:00


At least one of your problems is that you declare:
const struct stat st;
The const is an indication that this struct is read-only, and it
cannot accept data.  I believe that you should drop the
"const" specifier.

Another problem is that you call largerlsR() but you do not
supply the function.  At least you do not supply it in this
listing.

--
Fletcher Glenn
to reply remove NOSPAM from my reply address

"Kirk Fielder" <kirkfiel...@hotmail.com> wrote in message

news:B5dG5.38936$O7.476419@ozemail.com.au...
> Hello all (again),
> When I try to compile the following program(ot.c) I get the messages
below.
> Have tried removing/adding const struct various places (and changed stb to
> st) but cant seem to get it to compile. Can anyone fix it?
> I will be grateful for any help as I have been stuck on this problem for a
> long time (only got this far with help and trial and error) and am out of
my
> depth(but trying).
> My main concern is if you give the command line a -R option ftw is invoked
> and working.
> Thanking you in advance,
> Kirk.
> eg $ot -R 500 temp
> returns any files in temp that are >500bytes
> $ot -sR 500 temp
> returns any files in temp that are <500bytes
> COMPILER ERRORS:
> ot.c: In function `largers':
> ot.c:89: warning: passing arg 2 of `stat' discards qualifiers from pointer
> target type
> ot.c: In function `largerls':
> ot.c:101: warning: passing arg 2 of `stat' discards qualifiers from
pointer
> target type
> ot.c:102: warning: passing arg 1 of `printf' makes pointer from integer
> without a cast
> ot.c: In function `largerl':
> ot.c:112: warning: passing arg 2 of `stat' discards qualifiers from
pointer
> target type
> ot.c: In function `largeri':
> ot.c:122: warning: passing arg 2 of `stat' discards qualifiers from
pointer
> target type
> Undefined                       first referenced
>  symbol                             in file
> largerlsR                           /var/tmp/ccvgbr7T.o
> ld: fatal: Symbol referencing errors. No output written to trey
> collect2: ld returned 1 exit status
> PROGRAM SOURCE ot.c:
> #include<sys/types.h>
> #include<sys/stat.h>
> #include<math.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include<dirent.h>
> #include <ftw.h>
> #include <unistd.h>
>    int  l_opt = 0, s_opt = 0, R_opt = 0, size = 0 , k=0, i=0;
>    int status_flag = 0;
>    int size1 = 0;
>    void largers(char *filenames[], int filenum);
>    void largerls(char *filenames[], int filenum);
>    int process(const char *, const struct stat *st, int);
>    void largerl(char *filenames[], int filenum);
>    void largeri(char *filenames[], int filenum);
>    int main(int argc, char **argv)
>    {
>       int           optchar;
>       int           integer;
>       char          *filenames[10];
>       int           filenum = 0;
>       const char c = '.';
>       extern char   *optarg;                          /* Is set to start
of
> argument value */
>       extern int    optind;                           /* Is set to index
in
> argv[] of next argument */
>       extern int    opterr;                           /* Tells getopt to
> print error msg */
>       extern int    optopt;

>       if (argc < 2)
>       {
>          fprintf(stderr, "usage: larger [-opts] size [ file... ]\n");
>          exit(2);
>       }
>       while (optind < argc) { /* Loop through all arguments to program */

>       /* Get options, if any. Will stay in this loop as long as the
> arguments
>        are options (starting with "-") */
>          while ((optchar = getopt(argc, argv, "lsR")) != EOF)

>             switch (optchar) {
>                case 'l':
>                   l_opt = 10;
>                   break;
>                case 's':
>                   s_opt = 21;
>                   break;
>                case 'R':
>                   R_opt = 32;
>                   break;
>                default :
>                   printf("Unrecognized option %c\n", optopt);
>                   break;
>             }
>          }
>       /* Argument was not an option, so get integer or filename */
>          if (atoi(argv[optind]) > 0) {
>             size = atoi(argv[optind]);
>          }
>          else

>             filenames[filenum++] = argv[optind];
>          }
>          optind++;
>       } /* as long as there are more arguments */
>       size1 = atoi(argv[1]);
>       if(size1)

>          largeri(filenames, filenum);
>          exit(1);
>       }
>       if(s_opt)

>          if(l_opt)

>             if(R_opt)

>                largerlsR(filenames, filenum);
>             }
>             largerls(filenames, filenum);
>          }
>          else  largers(filenames, filenum);
>       }
>       else if(l_opt) {  largerl(filenames, filenum);}
>       if(R_opt)

>          if (!filenum) {
>             ftw(*argv , process ,sysconf(_SC_OPEN_MAX) - 3 ); }
>       }
>    }
>    void largers(char *filenames[],int filenum)
>    {
>       int f_size = 0;
>       int k = 0;
>       const struct stat st;
>       for(k=0;k < filenum; k++)

>          f_size = stat(filenames[k],&st);
>          if (st.st_size < size){
>             printf("%s\n", filenames[k]);
>          }
>       }
>    }
>    void largerls(char *filenames[],int filenum)

>       int f_size = 0;
>       int k = 0;
>       const struct stat st;
>       for(k=0;k < filenum; k++)

>          f_size = stat(filenames[k],&st);
>          if (st.st_size < size) printf(st.st_size, filenames[k]);
>       }
>    }

>    void largerl(char *filenames[],int filenum)

>       int f_size = 0;
>       int k = 0;
>       const struct stat st;
>       for(k=0;k < filenum; k++)

>          f_size = stat(filenames[k],&st);
>          if (st.st_size > size) printf("%d %s\n", st.st_size,
filenames[k]);
>       }
>    }
>    void largeri(char *filenames[],int filenum)
>    {
>       int f_size = 0;
>       int k = 0;
>       const struct stat st;
>       for(k=0;k < filenum; k++)

>          f_size = stat(filenames[k],&st);
>          if (st.st_size > size){
>             printf("%s\n", filenames[k]);
>          }
>       }
>    }
>    int process(const char *path, const struct stat *st, int flag)
>    {
>       printf("%-24s", path);

>       switch (flag) {  /*st->.. needs work*/
>          case FTW_F:
>             printf("file, mode %o\n", st->st_mode & 07777);
>             break;
>          case FTW_D:
>             printf("directory, mode %o\n", st->st_mode & 07777);
>             break;
>          case FTW_DNR:
>             printf("unreadable directory, mode %o\n", st->st_mode &
07777);
>             break;
>          case FTW_NS:
>             printf("unknown; stat() failed\n");
>             break;
>       }
>    }


 
 
 

1. using ftw in this program?

Hi all,
Is it possible(and how) to use/pass the filenames from this function
"compare" and somehow use them to compare their size using ftw comparing
each filename (and its size) with all the files in a given directory
searching recursively?
-- output the file name found in the directory search only if it matches
pre-selected files and is less than "size" . Similar to the way the example
program works below which I obtained from the web. What do I need to do to
get it to work? Have tried a number of this like changing st_mode to
stb.st_size but with no luck.
Thanking you in advance,
Kirk.
COMPARE: eg input: $comp 512 file1 file2
--returns the files(file1 and file2)only that are > 512bytes
 void compare(char *filenames[],int filenum)   {
      int f_size = 0;
      int k = 0;
      struct stat stb;
      for(k=0;k < filenum; k++)

         f_size = stat(filenames[k],&stb);
   if (stb.st_size < size) printf("%d %s\n", stb.st_size, filenames[k]);
             }
               }
NEED TO SOMEHOW WORK with the program below which works like this--
$ftw temp
Directory /temp:
/tmp   directory, mode 777
.
/tmp/.x11-unix/xo file, mode 0
/tmp/psdata  file, mode 664
.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <ftw.h>
int process(const char *, const struct stat *, int);
int main(int argc, char **argv)
{
    while (--argc) {
        printf("Directory %s:\n", *++argv);
        ftw(*argv, process, sysconf(_SC_OPEN_MAX) - 3);
        putchar('\n');
    }
    exit(0);
int
process(const char *path, const struct stat *st, int flag)
{
    printf("%-24s", path);
    switch (flag) {
    case FTW_F:
        printf("file, mode %o\n", st->st_mode & 07777);
        break;
    case FTW_D:
        printf("directory, mode %o\n", st->st_mode & 07777);
        break;
    case FTW_DNR:
        printf("unreadable directory, mode %o\n", st->st_mode & 07777);
        break;
    case FTW_NS:
        printf("unknown; stat() failed\n");
        break;
    }
    return(0);

2. $10 for remote x setup

3. ftw(): File Tree Walk library function failes to compile on SCO OS5 w/ gnu.

4. robot x4.90: Scientific graph plotting/data analysis

5. old termcap program wont run on openserver

6. install network card

7. Simple X program wont build

8. Why socket can loose its name?

9. FvwmButtons wont start programs in certain locales

10. CC compiled .so does not work with g++ compiled main program

11. mail program wont create mbox as *save* file ...

12. compile C programs with UNIX system calls (= Unix Programs??)

13. Programs wont start