L
los harcre
My first c program.
Platform is linux 2.6.* ext3, 32bit arch.
My bkg: perl.
This is a cli program. It's given directory paths, and prints to screen
how many regular files are there.
My problem is that it seems to count properly for the cwd, but then.. It
says 0 for everywhere else. Maybe the entry_counter var is global .. ???
/*
* print to screen how many entries are in a directory
*/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int lscount ( char dir_path[], int typeflag )
{
DIR *dp;
int entry_count;
struct dirent *ep;
/*
switch ( typeflag )
{
case 0:
break;
case 1:
break;
case 2:
break;
default:
printf("no such type %d\n", typeflag );
exit(1);
}
*/
entry_count = 0;
dp = opendir( dir_path );
if ( dp == NULL )
{
(void) closedir(dp);
perror("didnt work");
exit(EXIT_FAILURE);
}
while ((ep = readdir(dp)) != NULL)
{
struct stat es;
stat( ep->d_name , &es ); /* man stat.h */
switch ( typeflag )
{
case 0:
/* all files */
entry_count++;
break;
case 1:
/* files */
if ( S_ISREG( es.st_mode ) )
entry_count++;
break;
case 2:
/* subdirs only */
if ( S_ISDIR( es.st_mode ) )
entry_count++;
break;
}
}
(void) closedir(dp);
return entry_count;
}
int main ( int argc, char *argv[] )
{
int i;
for ( i = 1; i < argc ; i++ )
{
printf("%6d %s\n", lscount(argv, 1) , argv );
}
exit(0);
}
My results:
[leo@nicotine lsutils]$ gcc -Wall lscount.c
[leo@nicotine lsutils]$ ./a.out ./
7 ./
[leo@nicotine lsutils]$ ./a.out ~/
0 /home/leo/
[leo@nicotine lsutils]$ touch ./test
[leo@nicotine lsutils]$ touch ~/testanother
[leo@nicotine lsutils]$ ./a.out ./ ~/
8 ./
0 /home/leo/
[leo@nicotine lsutils]$
Platform is linux 2.6.* ext3, 32bit arch.
My bkg: perl.
This is a cli program. It's given directory paths, and prints to screen
how many regular files are there.
My problem is that it seems to count properly for the cwd, but then.. It
says 0 for everywhere else. Maybe the entry_counter var is global .. ???
/*
* print to screen how many entries are in a directory
*/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int lscount ( char dir_path[], int typeflag )
{
DIR *dp;
int entry_count;
struct dirent *ep;
/*
switch ( typeflag )
{
case 0:
break;
case 1:
break;
case 2:
break;
default:
printf("no such type %d\n", typeflag );
exit(1);
}
*/
entry_count = 0;
dp = opendir( dir_path );
if ( dp == NULL )
{
(void) closedir(dp);
perror("didnt work");
exit(EXIT_FAILURE);
}
while ((ep = readdir(dp)) != NULL)
{
struct stat es;
stat( ep->d_name , &es ); /* man stat.h */
switch ( typeflag )
{
case 0:
/* all files */
entry_count++;
break;
case 1:
/* files */
if ( S_ISREG( es.st_mode ) )
entry_count++;
break;
case 2:
/* subdirs only */
if ( S_ISDIR( es.st_mode ) )
entry_count++;
break;
}
}
(void) closedir(dp);
return entry_count;
}
int main ( int argc, char *argv[] )
{
int i;
for ( i = 1; i < argc ; i++ )
{
printf("%6d %s\n", lscount(argv, 1) , argv );
}
exit(0);
}
My results:
[leo@nicotine lsutils]$ gcc -Wall lscount.c
[leo@nicotine lsutils]$ ./a.out ./
7 ./
[leo@nicotine lsutils]$ ./a.out ~/
0 /home/leo/
[leo@nicotine lsutils]$ touch ./test
[leo@nicotine lsutils]$ touch ~/testanother
[leo@nicotine lsutils]$ ./a.out ./ ~/
8 ./
0 /home/leo/
[leo@nicotine lsutils]$