F
fybar
Hi, I am in the progress of writing a program that will read in the
contents of a directory and then do some stuff to some of the files. So
far I am getting the contents of the directory but I get a strange
result from one file. Here is my program, feel free to critique:
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
void main(int argc, char *argv[])
{
int i;
int j;
int array_size;
DIR *dirp;
struct dirent *direntp;
char filename[NAME_MAX];
/* print usage */
switch(argc) {
case 1:
printf("Usage: %s path\n\n", argv[0]);
exit(1);
break;
/* print out dir contents */
default :
for ( i=1; i < argc; i++){
dirp = opendir( argv );
printf("This is the dir %s.\n\n", argv);
while ( (direntp = readdir( dirp )) != NULL )
{
printf( "1. strlen from raw {%d}\n", strlen(direntp->d_name) );
printf( "2. raw from pointer {%s}\n",direntp->d_name );
strncpy(filename,direntp->d_name,strlen(direntp->d_name));
array_size=strlen(filename);
printf("This is the string length %d\n", array_size);
if ( strncmp( filename, "r",1 )==0 ){
printf( "3. Modified string {%s}\n",filename );
}
for ( j=1; j <= array_size; j++ ) {
filename[j]=0;
}
}
}
break;
}
closedir( dirp );
exit(0);
}
I call the program:
myprog /usr/data/r*
I get data like this:
This is the dir /usr/data/rui19.
1. strlen from raw {1}
2. raw from pointer {.}
This is the string length 6
1. strlen from raw {2}
2. raw from pointer {..}
This is the string length 2
1. strlen from raw {14}
2. raw from pointer {rui19.ils.0001}
This is the string length 14
3. Modified string {rui19.ils.0001}
However one file gives me this:
1. strlen from raw {18}
2. raw from pointer {rui22.ils.0052.bak}
This is the string length 22
3. Modified string {rui22.ils.0052.baM-@M-^?^C}
What is that on the end of the file name? This file was created from a
winder's version of edt, vms guy just won't let go, and saved over a
Samba share to a Tru64 box. I don't know if some characters were
concatenated to the end of the filename, but an ls shows the filename
properly. Is there something in the filename or is my program to blame?
I have not tried removing or renaming the file as I don't want to lose
this behaviour until I undestand it. The odd part is that if I print
out the name directly from the dirent struct I don't see a problem, I
only see it after the strncpy. This is the only file that does this.
Also, I created the array_size variable because this wouldn't work.
What is wrong with this statement?
for ( j=1; j <= strlen(filename); j++
Thanks!
fyb
contents of a directory and then do some stuff to some of the files. So
far I am getting the contents of the directory but I get a strange
result from one file. Here is my program, feel free to critique:
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
void main(int argc, char *argv[])
{
int i;
int j;
int array_size;
DIR *dirp;
struct dirent *direntp;
char filename[NAME_MAX];
/* print usage */
switch(argc) {
case 1:
printf("Usage: %s path\n\n", argv[0]);
exit(1);
break;
/* print out dir contents */
default :
for ( i=1; i < argc; i++){
dirp = opendir( argv );
printf("This is the dir %s.\n\n", argv);
while ( (direntp = readdir( dirp )) != NULL )
{
printf( "1. strlen from raw {%d}\n", strlen(direntp->d_name) );
printf( "2. raw from pointer {%s}\n",direntp->d_name );
strncpy(filename,direntp->d_name,strlen(direntp->d_name));
array_size=strlen(filename);
printf("This is the string length %d\n", array_size);
if ( strncmp( filename, "r",1 )==0 ){
printf( "3. Modified string {%s}\n",filename );
}
for ( j=1; j <= array_size; j++ ) {
filename[j]=0;
}
}
}
break;
}
closedir( dirp );
exit(0);
}
I call the program:
myprog /usr/data/r*
I get data like this:
This is the dir /usr/data/rui19.
1. strlen from raw {1}
2. raw from pointer {.}
This is the string length 6
1. strlen from raw {2}
2. raw from pointer {..}
This is the string length 2
1. strlen from raw {14}
2. raw from pointer {rui19.ils.0001}
This is the string length 14
3. Modified string {rui19.ils.0001}
However one file gives me this:
1. strlen from raw {18}
2. raw from pointer {rui22.ils.0052.bak}
This is the string length 22
3. Modified string {rui22.ils.0052.baM-@M-^?^C}
What is that on the end of the file name? This file was created from a
winder's version of edt, vms guy just won't let go, and saved over a
Samba share to a Tru64 box. I don't know if some characters were
concatenated to the end of the filename, but an ls shows the filename
properly. Is there something in the filename or is my program to blame?
I have not tried removing or renaming the file as I don't want to lose
this behaviour until I undestand it. The odd part is that if I print
out the name directly from the dirent struct I don't see a problem, I
only see it after the strncpy. This is the only file that does this.
Also, I created the array_size variable because this wouldn't work.
What is wrong with this statement?
for ( j=1; j <= strlen(filename); j++
Thanks!
fyb