下面的程序在solaris8、gcc编译通过的,如果一个目录是空的,输出为2。
引用:#include
#include
#include
int main(int argc , char **argv)
{
DIR *dirp;
int num=0;
dirp = opendir(argv[1]);
while (dirp) {
if ( readdir(dirp) != NULL)
++num;
else
break;
}
closedir(dirp);
printf("%d\n",num);
}
shell中判断目录为空
引用:#!/bin/ksh
# Check if a directory is empty or not
if [ $# = 0 ]
then
echo
echo "use this tool to check if directory is empty"
echo "for example: isEmpty dirName "
echo
exit 1
fi
case $(( 0 + $(find $1 2>&- |head -2|wc -l))) in
0) echo Permission denied ! ;;
1) echo Directory is empty ! ;;
*) echo Directory is not empty ! ;;
esac