[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: C stuff
Wooo, it's not as easy as in perl; essentially it is: open a directory
(realy a file), read the first entry into a structure, read the structure.
Here's a catch: reading the dir only gives one entry, for the next entry,
read it again (goes to next automatically). Another catch: the list is
not alpha-numeric sorted (the order in which files are added (ever
noticed this in DOS?)).
I included a short project at the bottom I did a while back-- it reads a
directory (the current) and outputs it to an HTML file; but you'll get the idea. It
doesn't show hidden files (it could very easily), nor files without read
permission (This was part of a webserver for a CS class).
Hope you can make it out-- if you knock out the html stuff, it'll clear
it up quite a bit.
Mike
On Sun, 29 Mar 1998, Bill Dunn wrote:
>
>
> What would be the equivalent in ANSI C or C++ to the following PERL line:
>
> $input = `ls -l /home/httpd/html`;
>
> I've tried:
>
> input[1000] = system("ls -l /home/httpd/html");
>
> but it actually sends the output to the screen instead of the assigning
> the directory listing to the variable. And I've tried several data types
> for input to no avail.
>
> Basically, all I need to do is get a list of the files in a directory
> and assign them to an array so I can use them one at a time.
>
> Thanks.
>
>
> Bill Dunn
>
> --
> To unsubscribe, send email to majordomo@silug.org with
> "unsubscribe silug-discuss" in the body.
>
>
----- starts here -----
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/param.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
void main()
{
struct dirent *dirent_struct_ptr; /* struct for readdir */
struct group *grp_struct_ptr; /*struct for getgrgid */
struct passwd *pwd_struct_ptr; /*struct for getpwuid */
struct stat *stat_struct_ptr; /* struct for stat */
struct tm *time_struct_ptr; /*struct for localtime */
FILE * fp;
DIR * dp; /* for opendir */
char *path_ptr, *time_string_ptr; /* for getcwd */
time_t cal_time;
uid_t uids, gids;
uid_t i=15;
int j=0;
uid_t getuid(void);
char temp[MAXPATHLEN+1];
char slash[]="/";
/* S_IREAD S_IWRITE S_IEXEC */
tzset();
uids=getuid();
if ((stat_struct_ptr=(struct stat *) malloc(sizeof(stat))) != NULL)
if ((path_ptr=getcwd(NULL,MAXPATHLEN+1)) != NULL)
if ((dp=opendir(path_ptr)) != NULL)
{
printf("\n<html>\n<head>\n<title>Mike Connor's
phase2.c</title>\n</head>\n");
printf("<body>");
printf("<table>");
printf("<tr><td>file name </td><td> size </td><td> owners name
</td><td> grou
p's name </td><td> last accessed </td><td> last modified </td></tr>");
while((dirent_struct_ptr=readdir(dp)) != NULL)
{
if (((*dirent_struct_ptr).d_ino !=0) && (*(*dirent_struct_ptr).d_name
!= '.')
)
{
strcpy(temp, path_ptr);
strcat(temp, slash);
strcat(temp, (*dirent_struct_ptr).d_name);
if ((stat(temp, stat_struct_ptr) != -1))
if ((*(*dirent_struct_ptr).d_name) != '.')
{
printf("<tr><td>");
if ( (((*stat_struct_ptr).st_mode & S_IREAD) && (uids ==
(*stat_struct_pt
r).st_uid)) || ((*stat_struct_ptr).st_mode & S_IREAD>>3) ||
((*stat_struct_ptr).
st_mode & S_IREAD>>6))
printf("<a href=\"%s\"> %s </a> ",
(*dirent_struct_ptr).d_name, (*dir
ent_struct_ptr).d_name);
else
printf(" %s ", (*dirent_struct_ptr).d_name);
printf("</td><td>");
printf(" %u ", (*stat_struct_ptr).st_size);
printf("</td><td>");
pwd_struct_ptr=getpwuid((*stat_struct_ptr).st_uid);
printf(" %s ", (*pwd_struct_ptr).pw_name);
free(pwd_struct_ptr);
printf("</td><td>");
grp_struct_ptr=getgrgid((*stat_struct_ptr).st_gid);
printf(" %s ", (*grp_struct_ptr).gr_name);
free(grp_struct_ptr);
printf("</td><td>");
time_struct_ptr=localtime(&(*stat_struct_ptr).st_atime);
printf(" %s ", asctime(time_struct_ptr));
printf("</td><td>");
time_struct_ptr=localtime(&(*stat_struct_ptr).st_mtime);
printf(" %s ", asctime(time_struct_ptr));
free(time_struct_ptr);
printf("</td></tr>");
}
}
} /*while end*/
printf("</table>");
printf("</body></html>\n");
} /* if ends */
}/*prog end*/
--
To unsubscribe, send email to majordomo@silug.org with
"unsubscribe silug-discuss" in the body.
- References:
- C stuff
- From: Bill Dunn <bill.dunn@vci.net>