I've the following code written in C and it displays files (name and size) and directories (<dir> if it encounters directory which is not hidden or not link) but it's not able to pass testing arguments test. I haven't done this test myself (it's just given to me to test my codes), that's why I don't know the reason for failing this test.
Code:
int main(int argc, char* argv[])
{
DIR *pdir;
struct dirent *pent;
struct stat statbuf;
if(argc>1)
{
pdir=opendir(argv[1]);
} else {
pdir=opendir(".");
}
//chdir(pdir);
if (!pdir){
printf ("opendir() failure; terminating\n");
exit(1);
}
while ((pent=readdir(pdir))!=NULL)
{
stat(pent->d_name,&statbuf);
if(strcmp(".", pent->d_name) == 0 || strcmp("..", pent->d_name) == 0)
continue;
if(S_ISDIR(statbuf.st_mode))
printf("%s <dir>\n", pent->d_name);
if(!(S_ISDIR(statbuf.st_mode)))
printf("%s %d\n", pent->d_name, statbuf.st_size);
}
closedir(pdir);
return 0;
}
That's the test:
Code:
#!/bin/bash
LS_PY=./myls.py
LS_C=./myls
TMP1=~/lsout1
TMP2=~/lsout2
TMPERR=~/lserr
echo Testing files
$LS_PY > $TMP1
$LS_C > $TMP2
diff $TMP1 $TMP2 && echo OK || echo ERROR
echo Testing dirs
mkdir testdir
$LS_PY > $TMP1
$LS_C > $TMP2
diff $TMP1 $TMP2 && echo OK || echo ERROR
rmdir testdir
echo Testing arguments
$LS_PY / > $TMP1
$LS_C / > $TMP2
diff $TMP1 $TMP2 && echo OK || echo ERROR
echo Testing error-arguments - should see error below
$LS_C nonexistentdirectory > /dev/null && echo ERROR - RETURN ZERO || echo RETURN value OK
echo Testing error-arguments - should see error below
$LS_C tests.sh > /dev/null && echo ERROR - RETURN ZERO || echo RETURN value OK