LinuxÒÁµéÔ°ÂÛ̳'s Archiver

realhac ·¢±íÓÚ 2007-3-2 10:00

Ë­ÓбéÀúĿ¼µÄ³ÌÐò

ÎÒÏë±àдһ¸ö³ÌÐò£¬Äܹ»±éÀúÖ¸¶¨µÄĿ¼£¬¸ÃĿ¼ÏÂÓÐ×ÓĿ¼£¬ÓÐÎļþ£¬ÎÒµÄÄ¿µÄÊÇÄܹ»¶¨Î»µ½Ã¿Ò»¸öÎļþ£¬¾ÍÏñ²¡¶¾É¨ÃèÄÇÑù¡£Ð»ÁË

dearvoid ·¢±íÓÚ 2007-3-2 12:15

°³ÒÔǰдµÄÒ»¸ö¼òµ¥ÊµÏÖ:[code]
+?$yG;s:i /*
'V qt&JF$\1l *  Author: dearvoid at gmail.comM&n6LT9X5Wd
*y"|OJ T
*  $Date: 2006-06-14 09:01:52 +0800 (Wed, 14 Jun 2006) $
J _Ug%g-sm3x{ ? *  $HeadURL: svn://svnserver/clark/void/trunk/c/ls-R.c $
eu?!x o Md7g i *  $Revision: 554 $/N#Tfx0D!h
*/h J3Ow$s @L

o4Q%@ `:RP+Z #ifdef HAVE_CONFIG_H9v4D0jf"o5I+t6H1Q
#  include "config.h"d~Y:D'l d,]5G
#endif
z`e$lcD+@H:t-K'R
J;|2_2F*Us1KP #include <stdio.h>
CTVdt$KT #include <stdbool.h>!j?(p(Heoz%v:B:a%o
#include <errno.h> L&wK8{3O}vQ
#include <string.h>
*od2k?3m)d$Z%jhGG #include <stdlib.h>
5|X f:Kh&U(Ti s #include <dirent.h>pSZM\lu+{^
#include <sys/stat.h>P n9Iz d+[4U[GFM.W
ab'H&A ^u$U
bool
O+IHk VT)]i`!O isDir(const char * path, const struct dirent * ent)
Q3CVk#e z {
f'a%Ez zw #ifdef HAVE_STRUCT_DIRENT_D_TYPE&b_0G [f8^-~:?
    /*
LYfK&PP      * On Mac OS X 10.3.x (PPC) the struct dirent does not
AR$c;q!f7sZ"p%PTn      * has the d_type field.*en"o;V-vEP
     */r0VA6fg5G'ezc+B.m
    return ent->d_type == DT_DIR;!D-e#LG&D;G
#else
+y_9c f@O0A}     char buf[1024]; // FIXME
3OA wFa h,?     struct stat fstat;3iTr:ov7V4N#?+YW|h
9?6Sz-GF4alM
    sprintf(buf, "%s/%s", path, ent->d_name);%kE-zuwM_
    if (stat(buf, &fstat) < 0) { Fw:RE~w&y
        perror("stat()");6j8nV*ux[i!rX
        exit(1);+U g1? z'F` e1O+N
    } else {D5H$O7n"{ITkgL'I
        return S_ISDIR(fstat.st_mode);6u}c4z6Z,gy
    }
P7oh5?Pq7|d#Z(h #endif
D;_9i CX2D yL"~ }ky8_`F[

4m KV?]Bb int
$a8QH5I\Kw'Y&r ls_R(char * path)-Y"V4L*ke
{3{\(an;| MQ
    int i;g0Y)F|1R-\bU@
    int nEntries;R KP1if@
    char buf[1024];     // FIXME
P s4A DU6EJQ)_Q     struct dirent ** list;a9Y{O{L)J6YI

VG:kD.y,l&g"d1G     nEntries = scandir(path, &list, NULL, NULL);
XX/loT     if (nEntries < 0) {
C TC? P)j/vd         printf("scandir('%s'): %s\n", path, strerror(errno)); INM&iZ'`~
        return 1;
J}z }2h(q!s     }
+n&`t9n1FD&[ R.{*R'FM"N9e7Z1r
    printf("%s:\n", path);2v*x2r;enB
    for (i = 0; i < nEntries; ++i) {7j6SU r2n2c$gs.`t
        printf("\t%s\n", list[i]->d_name);C4]o \`t6K
        if (!isDir(path, list[i]) || !strcmp(list[i]->d_name, ".")
ka;V&W%Kxh3W.uz/S             || !strcmp(list[i]->d_name, "..")) {
C0e W+t x             free(list[i]);
;?)vKKby             list[i] = NULL;m"R?:z,j"P#h
        }
R{dI;}j{[     }
;J-`1@[:I.nJ     printf("\n");G!u2k,Y*eC6Z
+S,uXm7c V
    for (i = 0; i < nEntries; ++i) {
K)w9}:]$]6a p.m         if (list[i]) {Hk&E^ CVBJ"e
            sprintf(buf, "%s/%s", path, list[i]->d_name);
p^h#b[N m             ls_R(buf);
@KSQ)~)d @         }
Yt%FOr \&N'GL[ ha     }7b8t1i|!N^+Y
c0@{L$z0@u.I:hPD
    for (i = 0; i < nEntries; ++i) {
u%J7IBM#g+D4d t         free(list[i]);
PK3x.}oce@     }
i A2Re$WF$Fe     free(list);9DiD1jI0q8D

[Z[!Z,j;Q.T5u     return 0;
G-AAITE7_ }
AA(e~^
)P u'|+kk'C&v int"^~;YO|$E5G!Z
main(int argc, char *argv[])
u$ZA1H"t$M`b {
NJ7]&g,Y     if (argc != 2) {
e3OvmSa         printf("Usage:\n");}tl}w
        printf("    %s /your/dir\n", argv[0]);^Ug4nnqZ bl
        exit(1); ^fJhCZ
    }
K&lCk3[;m&} Z
U M$@a[\3_-}     ls_R(argv[1]);7VOPKa7jXv

1ZY UQ8L r5T     return 0;a!F#F;\XWa]rb9T^
}[/code]

vcacm ·¢±íÓÚ 2007-5-2 11:20

²»´í¡£¡£¡£

MMMIX ·¢±íÓÚ 2007-5-2 16:53

[quote=realhac;570980]ÎÒÏë±àдһ¸ö³ÌÐò£¬Äܹ»±éÀúÖ¸¶¨µÄĿ¼£¬¸ÃĿ¼ÏÂÓÐ×ÓĿ¼£¬ÓÐÎļþ£¬ÎÒµÄÄ¿µÄÊÇÄܹ»¶¨Î»µ½Ã¿Ò»¸öÎļþ£¬¾ÍÏñ²¡¶¾É¨ÃèÄÇÑù¡£Ð»ÁË[/quote]BTRV|1c`8}
¿´Ò»Ï ftw(3).

wfwt ·¢±íÓÚ 2007-5-4 10:16

ftw(3)»á¸ú×Ù·ûºÅÁ´½Ó£¬ÓÃnftw(3)£¬BSDÏÂÊÇfts(3)

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.