[Mew-dist 11689] Re: m-002.tar.gz

SAKAI Kiyotaka ksakai at example.com
1999年 12月 7日 (火) 18:55:52 JST


>> In article <199912070729.QAA01494 at example.com>, Kazu Yamamoto (山本和彦) <kazu at example.com> writes:

> m-002.tar.gz を ftp.mew.org:/pub/Mew/alpha に置きました。
> だいぶ問題点が分かって来ました。

> - direnet の d_type
> 	stat しないとだめ?
> - scandir()
> 	自前で作らないとだめ?

こんな感じでしょうか。
-- 
酒井 清隆 (E-mail: ksakai at example.com)

-------------- next part --------------
===================================================================
RCS file: RCS/mls.c,v
retrieving revision 1.1
diff -c -r1.1 mls.c
*** mls.c	1999/12/07 08:40:54	1.1
--- mls.c	1999/12/07 09:52:39
***************
*** 9,14 ****
--- 9,15 ----
  #include <stdlib.h>
  #include <string.h>
  #include <ctype.h>
+ #include <sys/stat.h>
  
  #if HAVE_DIRENT_H
  # include <dirent.h>
***************
*** 30,36 ****
  #include <pwd.h>
  
  #define MAILHOME "Mail"
! #define BSIZ 2048
  #define N 3
  
  char *FIELDS[] = {"From:", "Subject:", "To:", };
--- 31,37 ----
  #include <pwd.h>
  
  #define MAILHOME "Mail"
! #define BSIZ 4096
  #define N 3
  
  char *FIELDS[] = {"From:", "Subject:", "To:", };
***************
*** 158,182 ****
  	fflush(stdout);
  }
  
  void
  scanfolder(char *fld) {
  	struct dirent **dirents;
  	char *dir = expandfolder(fld);
  	FILE *fp;
! 	int i, n;
  
  	if (chdir(dir) != 0) {
  		fprintf(stderr, "can't change folder to %s\n", fld);
  		exit(EXIT_FAILURE);
  	}
  
  	for (i = 0; i < N; i++)
  		SLEN[i] = strlen(FIELDS[i]);
  
! 	n = scandir(".", &dirents, selectnum, compnum);
  
  	for (i = 0; i < n; i++) {
! 		fp = fopen(dirents[i]->d_name, "r");
  		if (fp == NULL) continue;
  		dumpheader(fp);
  		fclose(fp);
--- 159,238 ----
  	fflush(stdout);
  }
  
+ int numfile(char *s) {
+ 	if (*s == '\0')
+ 		return 0;
+ 
+ 	while (*s != '\0')
+ 		if (!isdigit(*s++))
+ 			return 0;
+ 
+ 	return 1;
+ }
+ 
+ static int intcompare(int *i, int *j) {
+ 	if (*i > *j)
+ 		return 1;
+ 	if (*i < *j)
+ 		return -1;
+ 	return 0;
+ }
+ 
+ #define SIZE 128
  void
  scanfolder(char *fld) {
  	struct dirent **dirents;
  	char *dir = expandfolder(fld);
  	FILE *fp;
! 	int i, n, m;
! 	DIR *dirp;
! 	struct dirent *dp;
! 	struct stat st;
! 	char *fname;
! 	int *list;
! 	char buf[64];
  
  	if (chdir(dir) != 0) {
  		fprintf(stderr, "can't change folder to %s\n", fld);
  		exit(EXIT_FAILURE);
  	}
  
+ 	if ((dirp = opendir(".")) == NULL) {
+ 		fprintf(stderr, "can't open folder: %s\n", fld);
+ 		exit(EXIT_FAILURE);
+ 	}
+ 
  	for (i = 0; i < N; i++)
  		SLEN[i] = strlen(FIELDS[i]);
  
! 	if ((list = (int *) malloc(SIZE * sizeof(int))) == NULL) {
! 		fprintf(stderr, "memory fault\n");
! 		exit(EXIT_FAILURE);
! 	}
! 	m = SIZE;
! 	n = 0;
! 	while ((dp = readdir(dirp)) != NULL) {
! 		fname = dp->d_name;
! 		stat(fname, &st);
! 		if (!(st.st_mode & S_IFREG))
! 			continue;
! 
! 		if (numfile(fname)) {
! 			if (n >= m - 1) {
! 				m += SIZE;
! 				list = (int *) realloc(list, m * sizeof(int));
! 			}
! 			*(list + n) = atoi(fname);
! 			n++;
! 		}
! 	}
! 	closedir(dirp);
  
+ 	qsort(list, n, sizeof(int), (int (*)(const void *,const void *)) intcompare);
+ 	
  	for (i = 0; i < n; i++) {
! 		sprintf(buf, "%d", *(list + i));
! 		fp = fopen(buf, "r");
  		if (fp == NULL) continue;
  		dumpheader(fp);
  		fclose(fp);


Mew-dist メーリングリストの案内