Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting list of files into array

Status
Not open for further replies.
May 3, 2002
633
0
0
US
My knowledge of C is limited and for the task I am trying to do I cannot use Korn shell. Given that, how would I get a list of files into an array and print them to the screen and give the user a choice of which one to choose to delete.

Example:

If there was a core file in /home /tmp /usr/WebSphere /sft/iplanet

I want to list the files and prompt them to delete them:

Do you want to delete /home/core (Y/N)?

And loop through until all files have been read. I can do this easily in Korn, but as I said my C is rough.

Thanks for any help.
 
This really seems like something that the shell has
better high level facilties for than a standalone C
program. An array is not an ideal data structure for
doing what you want in any case. You would have to
allocate a very large static array, or use dynamic
allocation for a char **array which requires a
resizing algorithm and some overhead. A linked list
is better. Proper error handling is an ordeal with C
for systems operations like these.

In any case there are no ansi C libs for directory
searches and the posix opendir() family may not be
usable for you, I don't know your platform. In
addition I assume you would want a recursive search
of the chosen directories?

Having said all this: perl,tcl and python are good
candidates for this job as they offer high level
interfaces and enough power and speed to do the
job.






 
Thanks for your input, but if you can bear with me, this is what I have, however, it is not deleting the file that I prompt for. I am on AIX by the way. Thanks.

#include <stdio.h>
FILE *inf;
char s[80];
int line;

void cleartoendofline(void);

void cleartoendofline(void)
{
char ch;
ch = getchar();
while( ch != '\n' )
ch = getchar();
}

main () {
setuid(0);
system(&quot;find /home /tmp /usr/WebSphere -type f -name core > /tmp/findcore.txt&quot;);
line=0;
inf = fopen (&quot;/tmp/findcore.txt&quot;, &quot;r&quot;);
if (inf == NULL) printf(&quot;Can't Open File\n&quot;);
else {
while (fgets(s, 79, inf) != NULL) {
line++;
printf(&quot;File %d: %s&quot;, line, s);
Confirm(s);
}
}
}

Confirm(char s) {
char ch;
int valid_choice;

printf(&quot;Do you want to remove this file? \n&quot;);
valid_choice = 0;
while( valid_choice == 0 ) {
printf(&quot;Continue (Y/N)? &quot;);
scanf(&quot; %c&quot;, &ch );
ch = toupper(ch);
if((ch == 'Y') || (ch == 'N'))
valid_choice = 1;
if ( valid_choice == 1 )
unlink(&quot;%s&quot;, s);
else
printf(&quot;\007Error: Invalid choice\n&quot;);
cleartoendofline();
}
}
 
There are problems with your confirm() function...
You could try something trivial like this..

Code:
int confirm(char *s) {
int pret;
char ch;
begin:
         printf(&quot;\nWould you like to delete: %s (y/n)?&quot;,s);
         scanf(&quot; %c&quot;, &ch);
           
        if (ch == 'n') {
               return 0;
        }
           
        if (ch == 'y') {   
           if ( (pret = unlink(s)) < 0) {
               perror(&quot;unlink() in confirm()&quot;);
               return -1;
           } else {
               return 1;
           }
        }
printf(&quot;Invalid choice!\n&quot;);
goto begin;
}
This gives you some error checks.


 
This is a round about way to achieve what you want.
Check out the header files <dirent.h> or <unistd.h>
in your AIX system. Either of them would have defined
functions to open and traverse directories. I think
the function name is &quot;readdir&quot; if you are having dirent.h.

Once you have opened a directory, you can traverse it.

Here is how a dirent sturcture is defined in SunOS.

struct dirent {
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[1];
};



I suggest you try 'man readdir' or 'man dirent' at a console
for getting proper information on traversing directories.

Regards

Anand Pillai
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Visit my homepage for python scripts
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top