⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.c

📁 以目录树的形式显示一个目录下及其各层子目录下所有文件
💻 C
📖 第 1 页 / 共 4 页
字号:
	dl[p]->lnk = scopy(lbuf);	if (rs < 0) dl[p]->orphan = TRUE;	dl[p]->lnkmode = st.st_mode;      }    }#endif    dl[p]->isdir = ((st.st_mode & S_IFMT) == S_IFDIR);    dl[p]->issok = ((st.st_mode & S_IFMT) == S_IFSOCK);    dl[p]->isfifo = ((st.st_mode & S_IFMT) == S_IFIFO);    dl[p++]->isexe = ((st.st_mode & S_IEXEC) | (st.st_mode & (S_IEXEC>>3)) | (st.st_mode & (S_IEXEC>>6))) ? 1 : 0;  }  closedir(d);  *n = p;  dl[p] = NULL;  return dl;}/* Sorting functions */int alnumsort(struct _info **a, struct _info **b){  return strcmp((*a)->name,(*b)->name);}int reversealnumsort(struct _info **a, struct _info **b){  return strcmp((*b)->name,(*a)->name);}int timesort(struct _info **a, struct _info **b){  if ((*a)->mtime == (*b)->mtime) return strcmp((*a)->name,(*b)->name);  return (*a)->mtime < (*b)->mtime;}int dirsfirstsort(struct _info **a, struct _info **b){  if ((*a)->isdir == (*b)->isdir) return strcmp((*a)->name,(*b)->name);  else return (*a)->isdir ? -1 : 1;}/** Necessary only on systems without glibc **/void *xmalloc (size_t size){  register void *value = malloc (size);  if (value == 0) {    fprintf(stderr,"tree: virtual memory exhausted.\n");    exit(1);  }  return value;}void *xrealloc (void *ptr, size_t size){  register void *value = realloc (ptr,size);  if (value == 0) {    fprintf(stderr,"tree: virtual memory exhausted.\n");    exit(1);  }  return value;}char *gnu_getcwd(){  int size = 100;  char *buffer = (char *) xmalloc (size);       while (1)    {      char *value = getcwd (buffer, size);      if (value != 0)	return buffer;      size *= 2;      free (buffer);      buffer = (char *) xmalloc (size);    }}/* * Patmatch() code courtesy of Thomas Moore (dark@mama.indstate.edu) * '|' support added by David MacMahon (davidm@astron.Berkeley.EDU) * returns: *    1 on a match *    0 on a mismatch *   -1 on a syntax error in the pattern */int patmatch(char *buf, char *pat){  int match = 1,m,n;  char *bar = strchr(pat, '|');  /* If a bar is found, call patmatch recursively on the two sub-patterns */  if (bar) {    /* If the bar is the first or last character, it's a syntax error */    if (bar == pat || !bar[1]) {      return -1;    }    /* Break pattern into two sub-patterns */    *bar = '\0';    match = patmatch(buf, pat);    if (!match) {      match = patmatch(buf,bar+1);    }    /* Join sub-patterns back into one pattern */    *bar = '|';    return match;  }  while(*pat && match) {    switch(*pat) {    case '[':      pat++;      if(*pat != '^') {	n = 1;	match = 0;      } else {	pat++;	n = 0;      }      while(*pat != ']'){	if(*pat == '\\') pat++;	if(!*pat /* || *pat == '/' */ ) return -1;	if(pat[1] == '-'){	  m = *pat;	  pat += 2;	  if(*pat == '\\' && *pat)	    pat++;	  if(*buf >= m && *buf <= *pat)	    match = n;	  if(!*pat)	    pat--;	} else if(*buf == *pat) match = n;	pat++;      }      buf++;      break;    case '*':      pat++;      if(!*pat) return 1;      while(*buf && !(match = patmatch(buf++,pat)));      return match;    case '?':      if(!*buf) return 0;      buf++;      break;    case '\\':      if(*pat)	pat++;    default:      match = (*buf++ == *pat);      break;    }    pat++;    if(match<1) return match;  }  if(!*buf) return match;  return 0;}/* * They cried out for ANSI-lines (not really), but here they are, as an option * for the xterm and console capable among you, as a run-time option. */void indent(){  int i;  if (ansilines) {    if (dirs[0]) fprintf(outfile,"\033(0");    for(i=0;dirs[i];i++) {      if (dirs[i+1]) {	if (dirs[i] == 1) fprintf(outfile,"\170   ");	else printf("    ");      } else {	if (dirs[i] == 1) fprintf(outfile,"\164\161\161 ");	else fprintf(outfile,"\155\161\161 ");      }    }    if (dirs[0]) fprintf(outfile,"\033(B");  } else {    if (Hflag) fprintf(outfile,"\t\t\t\t   ");    for(i=0;dirs[i];i++) {      fprintf(outfile,"%s ",	      dirs[i+1] ? (dirs[i]==1 ? linedraw->vert     : (Hflag? "&nbsp;&nbsp;&nbsp;" : "   ") )			: (dirs[i]==1 ? linedraw->vert_left:linedraw->corner));    }  }}void free_dir(struct _info **d){  int i;  for(i=0;d[i];i++) {    free(d[i]->name);    if (d[i]->lnk) free(d[i]->lnk);    free(d[i]);  }  free(d);}#ifdef __EMX__char *prot(long m)#elsechar *prot(u_short m)#endif{#ifdef __EMX__  const static u_short ifmt[]={    FILE_ARCHIVED, FILE_DIRECTORY, FILE_SYSTEM,FILE_HIDDEN, FILE_READONLY  };  const u_short*p;  static char buf[6];  char*cp;  for(p=ifmt,cp=strcpy(buf,"adshr");*cp;++p,++cp)    if(!(m&*p))      *cp='-';#else  static char buf[11];  static u_short ifmt[] = {S_IFIFO, S_IFCHR, S_IFDIR, S_IFBLK, S_IFREG, S_IFLNK, S_IFSOCK, 0};  static char fmt[] = {'p','c','d','b','-','l','s','?'};  int i;  for(i=0;ifmt[i] && (m&S_IFMT) != ifmt[i];i++);  buf[0] = fmt[i];  buf[1] = (m & S_IREAD)? 'r' : '-';  buf[2] = (m & S_IWRITE)? 'w' : '-';  buf[3] = (m & S_IEXEC)? 'x' : '-';  if (m & S_ISUID) buf[3] = (buf[3]=='-')? 'S' : 's';  buf[4] = (m & (S_IREAD>>3))? 'r' : '-';  buf[5] = (m & (S_IWRITE>>3))? 'w' : '-';  buf[6] = (m & (S_IEXEC>>3))? 'x' : '-';  if (m & S_ISGID) buf[6] = (buf[6]=='-')? 'S' : 's';  buf[7] = (m & (S_IREAD>>6))? 'r' : '-';  buf[8] = (m & (S_IWRITE>>6))? 'w' : '-';  buf[9] = (m & (S_IEXEC>>6))? 'x' : '-';  if (m & S_ISVTX) buf[9] = (buf[9]=='-')? 'T' : 't';  buf[10] = 0;#endif  return buf;}static char *month[] = {  "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};#define SIXMONTHS (6*31*24*60*60)char *do_date(time_t t){  time_t c = time(0);  struct tm *tm;  static char buf[30];  if (t > c) return " The Future ";  tm = localtime(&t);  sprintf(buf,"%s %2d",month[tm->tm_mon],tm->tm_mday);  if (t+SIXMONTHS < c) sprintf(buf+6,"  %4d",1900+tm->tm_year);  else sprintf(buf+6," %2d:%02d",tm->tm_hour,tm->tm_min);  return buf;}char *uidtoname(int uid){  struct xtable *o, *p, *t;  struct passwd *ent;  char ubuf[6];  int uent = HASH(uid);  for(o = p = utable[uent]; p ; p=p->nxt) {    if (uid == p->xid) return p->name;    else if (uid < p->xid) break;    o = p;  }  /* uh oh, time to do a real lookup */  t = xmalloc(sizeof(struct xtable));  if ((ent = getpwuid(uid)) != NULL) t->name = scopy(ent->pw_name);  else {    sprintf(ubuf,"%d",uid);    t->name = scopy(ubuf);  }  t->xid = uid;  t->nxt = p;  if (p == utable[uent]) utable[uent] = t;  else o->nxt = t;  return t->name;}char *gidtoname(int gid){  struct xtable *o, *p, *t;  struct group *ent;  char gbuf[6];  int gent = HASH(gid);  for(o = p = gtable[gent]; p ; p=p->nxt) {    if (gid == p->xid) return p->name;    else if (gid < p->xid) break;    o = p;  }  /* uh oh, time to do a real lookup */  t = xmalloc(sizeof(struct xtable));  if ((ent = getgrgid(gid)) != NULL) t->name = scopy(ent->gr_name);  else {    sprintf(gbuf,"%d",gid);    t->name = scopy(gbuf);  }  t->xid = gid;  t->nxt = p;  if (p == gtable[gent]) gtable[gent] = t;  else o->nxt = t;  return t->name;}void printit(unsigned char *s){  int c;  if (Nflag) {    fprintf(outfile,"%s",s);    return;  }  if (MB_CUR_MAX > 1) {    wchar_t *ws, *tp;    ws = xmalloc(sizeof(wchar_t)* (c=(strlen(s)+1)));    if (mbstowcs(ws,s,c) > 0) {      for(tp=ws;*tp;tp++)	if (iswprint(*tp)) fprintf(outfile,"%lc",(wint_t)*tp);	else {	  if (qflag) putc('?',outfile);	  else fprintf(outfile,"\\%03o",(unsigned int)*tp);	}      free(ws);      return;    }    free(ws);  }  for(;*s;s++) {    c = *s;#ifdef __EMX__    if(_nls_is_dbcs_lead(*(unsigned char*)s)){      putc(*s,outfile);      putc(*++s,outfile);    } else#endif      if (isprint(c)) putc(c,outfile);      else {	if (qflag) {	  if (MB_CUR_MAX > 1 && c > 127) putc(c,outfile);	  else putc('?',outfile);	} else {	  if (c < ' ') fprintf(outfile,"^%c",c+'@');	  else if (c == 127) fprintf(outfile,"^?");	  else fprintf(outfile,"\\%03o",c);	}      }  }}void psize(char *buf, off_t size){  char *unit="BKMGTPEZY";  int idx;  if (!hflag) sprintf(buf, sizeof(off_t) == sizeof(long long)? " %11lld" : " %9ld", size);  else {    for (idx=size<1024?0:1; size >= (1024*1024); idx++,size>>=10);    if (!idx) sprintf(buf, " %4d", (int)size);    else sprintf(buf, ((size>>10) >= 10)? " %3.0f%c" : " %3.1f%c", (float)size/(float)1024,unit[idx]);  }}void html_encode(FILE *fd, char *s){  for(;*s;s++) {    switch(*s) {      case '<':	fputs("&lt;",fd);	break;      case '>':	fputs("&gt;",fd);	break;      case '&':	fputs("&amp;",fd);	break;      case '"':	fputs("&quot;",fd);	break;      default:	fputc(*s,fd);//	fputc(isprint(*s)?*s:'?',fd);	break;    }  }}void url_encode(FILE *fd, char *s){  for(;*s;s++) {    switch(*s) {      case ' ':      case '"':      case '#':      case '%':      case '<':      case '>':      case '[':      case ']':      case '^':      case '\\':      case '?':      case '+':	fprintf(fd,"%%%02X",*s);	break;      case '&':	fprintf(fd,"&amp;");	break;      default:	fprintf(fd,isprint((u_int)*s)?"%c":"%%%02X",(u_char)*s);        break;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -