/* $Header: /home/yav/xpx/RCS/dir.c,v 1.11 1995/11/23 17:05:13 yav Exp $ * directory access * written by yav (UHD98984@pcvan.or.jp) */ #include "headers.h" #ifdef HAVE_SYS_DIR_H # ifdef HAVE_OPENDIR # define USE_OPENDIR # endif #endif #ifdef USE_OPENDIR # include # include #else # ifdef HAVE_SYS_WAIT_H # include # endif #endif #include char rcsid_dir[] = "$Id: dir.c,v 1.11 1995/11/23 17:05:13 yav Exp $"; void get_current_dir( #ifdef __STDC__ char *buf) #else buf) char *buf; #endif { #ifdef HAVE_GETCWD getcwd(buf, MAXPATH); #else getwd(buf); #endif } void mark_dir( #ifdef __STDC__ char *dir, char *filebuf, int len, int n) #else dir, filebuf, len, n) char *dir; char *filebuf; int len; int n; #endif { int i; struct stat statbuf; char buf[MAXPATH]; for (i = 0; i < n; i++) { sprintf(buf, "%s/%s", dir, filebuf+i*len); stat(buf, &statbuf); if (statbuf.st_mode & S_IFDIR) strcat(filebuf+i*len, "/"); } } #ifdef USE_OPENDIR /* * return : 0 error */ int get_dir( #ifdef __STDC__ char *name, char *filebuf, int len, int slotn) #else name, filebuf, len, slotn) char *name; char *filebuf; int len; int slotn; #endif { int n; DIR *dp; struct direct *dir; if ((dp = opendir(name)) == NULL) return 0; for (n = 0; n < slotn && (dir = readdir(dp)) != NULL; n++) { if (dir->d_ino == 0) continue; strncpy(filebuf+n*len, dir->d_name, len); *(filebuf+n*len+len-1) = '\0'; } closedir(dp); #ifdef __STDC__ qsort(filebuf, n, len, (int (*)(const void *, const void *))strcmp); #else qsort(filebuf, n, len, strcmp); #endif mark_dir(name, filebuf, len, n); return n; } #else /* USE_OPENDIR */ int get_dir( #ifdef __STDC__ char *name, char *filebuf, int len, int slotn) #else name, filebuf, len, slotn) char *name; char *filebuf; int len; int slotn; #endif { FILE *fp; int i, n; int fd[2]; char buf[MAXPATH]; pipe(fd); #ifdef HAVE_VFORK i = vfork(); #else i = fork(); #endif if (i == -1) { fprintf(stderr, "fork error!\n"); return 0; } if (i == 0) { close(1); dup(fd[1]); close(fd[1]); close(fd[0]); execlp("ls", "ls", "-a", name, NULL); fprintf(stderr, "exec ls error!\n"); #ifdef HAVE_VFORK _exit(1); #else exit(1); #endif } fp = fdopen(fd[0], "r"); close(fd[1]); for (n = 0; n < slotn && fgets(buf, sizeof(buf), fp) != NULL; n++) { i = strlen(buf); if (i && buf[i-1] == '\n') buf[i-1] = '\0'; if (strlen(buf) == 0) break; strncpy(filebuf+n*len, buf, len-1); *(filebuf+n*len+len-1) = '\0'; } fclose(fp); wait(&i); mark_dir(name, filebuf, len, n); return n; } #endif /* USE_OPENDIR */ /* End of file */