hehe,写完了,请大家指教吧。
我是在freebsd上面做的。
/* fixdir.c -- delete the junk files under one board directory which do
not exist in .DIR file
version 1.0 author yeshao Mar.12 2002 */
/* usage: fixdir source_dir_name dest_dir_name
note:
1.these dir names should not append the '/'
2.fixdir only cp the files in .DIR file to the dest_dir
you should delete the old files and then mv the files in dest_dir back.
*/
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"sys/file.h"
int main(int argc,char *argv[])
{
FILE *fp;
char line[256],cmd[100],source_dir[100];
if(argc<3)
{
printf("usage: fixdir source_dir dest_dir\n");
exit(0);
}
sprintf(source_dir,"%s/.DIR",argv[1]);
fp=fopen(source_dir,"r");
flock(fileno(fp),LOCK_EX);
while(fread(line,256,1,fp))
{
sprintf(cmd,"cp %s/%14s %s/%14s",argv[1],line,argv[2],line);
system(cmd);
}
flock(fileno(fp),LOCK_UN);
fclose(fp);
}
【 在 yanglc.bbs@bbs.pku.edu.cn (谁都不守候) 的大作中提到: 】
: 写完后公开出来吧,利国利民,功德无量,呵呵。
--
修改:yeshao FROM 166.111.172.104
FROM 166.111.172.104
【 在 yeshao@smth.org (fight through my way) 的大作中提到: 】
: hehe,写完了,请大家指教吧。
: flock(fileno(fp),LOCK_EX);
: while(fread(line,256,1,fp))
~~~~~~~~~~~~~~~~~ 直接这样子读不行吧?
应该是一个fileheader结构:
struct fileheader { /* This structure is used to hold data in */
char filename[STRLEN]; /* the DIR files */
char owner[STRLEN];
char title[STRLEN];
unsigned level;
unsigned char accessed[ 12 ]; /* struct size = 256 bytes */
} ;
所以应该是
struct fileheader line;
while(fread(line, sizeof(line), 1, fp)) {
直接用 rename 把 line.filename 移动到新目录,用system开销太大
}
: {
: sprintf(cmd,"cp %s/%14s %s/%14s",argv[1],line,argv[2],line);
: system(cmd);
: }
: flock(fileno(fp),LOCK_UN);
: fclose(fp);
: }
: 【 在 yanglc.bbs@bbs.pku.edu.cn (谁都不守候) 的大作中提到: 】
: : 写完后公开出来吧,利国利民,功德无量,呵呵。
--
FROM AfterDark