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

📄 tree.c

📁 以目录树的形式显示一个目录下及其各层子目录下所有文件
💻 C
📖 第 1 页 / 共 4 页
字号:
    fprintf(outfile,"\t\t<style type=\"text/css\">\n\t\t\t<!-- \n");    fprintf(outfile,"\t\t\tBODY { font-family : courier, monospace, sans-serif; }\n");    fprintf(outfile,"\t\t\tP { font-weight: normal; font-family : courier, monospace, sans-serif; color: black; background-color: transparent;}\n");    fprintf(outfile,"\t\t\tB { font-weight: normal; color: black; background-color: transparent;}\n");    fprintf(outfile,"\t\t\tA:visited { font-weight : normal; text-decoration : none; background-color : transparent; margin : 0px 0px 0px 0px; padding : 0px 0px 0px 0px; display: inline; }\n");    fprintf(outfile,"\t\t\tA:link    { font-weight : normal; text-decoration : none; margin : 0px 0px 0px 0px; padding : 0px 0px 0px 0px; display: inline; }\n");    fprintf(outfile,"\t\t\tA:hover   { color : #000000; font-weight : normal; text-decoration : underline; background-color : yellow; margin : 0px 0px 0px 0px; padding : 0px 0px 0px 0px; display: inline; }\n");    fprintf(outfile,"\t\t\tA:active  { color : #000000; font-weight: normal; background-color : transparent; margin : 0px 0px 0px 0px; padding : 0px 0px 0px 0px; display: inline; }\n");    fprintf(outfile,"\t\t\t.VERSION { font-size: small; font-family : arial, sans-serif; }\n");    /* We can use CSS for nolinks as well */    fprintf(outfile,"\t\t\t.NORM { color: black; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.FIFO { color: purple; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.CHAR { color: yellow; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.DIR  { color: blue; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.BLOCK { color: yellow; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.LINK { color: aqua; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.SOCK { color: fuchsia; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t.EXEC { color: green; background-color: transparent;}\n");    fprintf(outfile,"\t\t\t-->\n\t\t</style>\n");    fprintf(outfile,"\t</head>\n");    fprintf(outfile,"\t<body>\n");    fprintf(outfile,"\t\t<h1>%s</h1>\n\t\t\t<p>",title);    fflag = FALSE;    if (nolinks) {      if (force_color) fprintf(outfile, "<b class=\"NORM\">%s</b>\n",host);      else fprintf(outfile,"%s\n",host);    } else {      if (force_color) fprintf(outfile,"<a class=\"NORM\" href=\"%s\">%s</a>\n",host,host);      else fprintf(outfile,"<a href=\"%s\">%s</a>\n",host,host);    }    curdir = gnu_getcwd();  }  if (dirname) {    for(colored=i=0;dirname[i];i++,colored=0) {      if (fflag) {	do {	  j=strlen(dirname[i]);	  if (j > 1 && dirname[i][j-1] == '/') dirname[i][--j] = 0;	} while (j > 1 && dirname[i][j-1] == '/');      }      if ((n = lstat(dirname[i],&st)) >= 0) {	saveino(st.st_ino, st.st_dev);	if (colorize) colored = color(st.st_mode,dirname[i],n<0,FALSE);      }      if (!Hflag) printit(dirname[i]);      if (colored) fprintf(outfile,"%s",endcode);      if (!Hflag) listdir(dirname[i],&dtotal,&ftotal,0,0);      else {	if (chdir(dirname[i])) {	  fprintf(outfile,"%s [error opening dir]\n",dirname[i]);	  exit(1);	} else {	  listdir(".",&dtotal,&ftotal,0,0);	  chdir(curdir);	}      }    }  } else {    if ((n = lstat(".",&st)) >= 0) {      saveino(st.st_ino, st.st_dev);      if (colorize) colored = color(st.st_mode,".",n<0,FALSE);    }    if (!Hflag) fprintf(outfile,".");    if (colored) fprintf(outfile,"%s",endcode);    listdir(".",&dtotal,&ftotal,0,0);  }  if (Hflag)    fprintf(outfile,"\t\t<br><br>\n\t\t</p>\n\t\t<p>\n");  if (!noreport) {    if (dflag)      fprintf(outfile,"\n%d director%s\n",dtotal,(dtotal==1? "y":"ies"));    else      fprintf(outfile,"\n%d director%s, %d file%s\n",dtotal,(dtotal==1? "y":"ies"),ftotal,(ftotal==1? "":"s"));  }  if (Hflag) {    fprintf(outfile,"\t\t<br><br>\n\t\t</p>\n");    fprintf(outfile,"\t\t<hr>\n");    fprintf(outfile,"\t\t<p class=\"VERSION\">\n");    fprintf(outfile,hversion,linedraw->copy, linedraw->copy, linedraw->copy);    fprintf(outfile,"\t\t</p>\n");    fprintf(outfile,"\t</body>\n");    fprintf(outfile,"</html>\n");  }  if (outfilename != NULL) fclose(outfile);  return 0;}/*usage: tree [-adfghilnpqrstuxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]	[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]	[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]	[<directory list>]*/void usage(int n){  switch(n) {    case 1:      fprintf(stderr,"usage: tree [-adfghilnpqrstuxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]\n\t[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]\n\t[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]\n\t[<directory list>]\n");      break;    case 2:      fprintf(stderr,"usage: tree [-adfghilnpqrstuxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]\n\t[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]\n\t[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]\n\t[<directory list>]\n");      fprintf(stderr,"    -a          All files are listed.\n");      fprintf(stderr,"    -d          List directories only.\n");      fprintf(stderr,"    -l          Follow symbolic links like directories.\n");      fprintf(stderr,"    -f          Print the full path prefix for each file.\n");      fprintf(stderr,"    -i          Don't print indentation lines.\n");      fprintf(stderr,"    -q          Print non-printable characters as '?'.\n");      fprintf(stderr,"    -N          Print non-printable characters as is.\n");      fprintf(stderr,"    -p          Print the protections for each file.\n");      fprintf(stderr,"    -u          Displays file owner or UID number.\n");      fprintf(stderr,"    -g          Displays file group owner or GID number.\n");      fprintf(stderr,"    -s          Print the size in bytes of each file.\n");      fprintf(stderr,"    -h          Print the size in a more human readable way.\n");      fprintf(stderr,"    -D          Print the date of last modification.\n");      fprintf(stderr,"    -F          Appends '/', '=', '*', or '|' as per ls -F.\n");      fprintf(stderr,"    -r          Sort files in reverse alphanumeric order.\n");      fprintf(stderr,"    -t          Sort files by last modification time.\n");      fprintf(stderr,"    -x          Stay on current filesystem only.\n");      fprintf(stderr,"    -L level    Descend only level directories deep.\n");      fprintf(stderr,"    -A          Print ANSI lines graphic indentation lines.\n");      fprintf(stderr,"    -S          Print with ASCII graphics indentation lines.\n");      fprintf(stderr,"    -n          Turn colorization off always (-C overrides).\n");      fprintf(stderr,"    -C          Turn colorization on always.\n");      fprintf(stderr,"    -P pattern  List only those files that match the pattern given.\n");      fprintf(stderr,"    -I pattern  Do not list files that match the given pattern.\n");      fprintf(stderr,"    -H baseHREF Prints out HTML format with baseHREF as top directory.\n");      fprintf(stderr,"    -T string   Replace the default HTML title and H1 header with string.\n");      fprintf(stderr,"    -R          Rerun tree when max dir level reached.\n");      fprintf(stderr,"    -o file     Output to file instead of stdout.\n");      fprintf(stderr,"    --inodes    Print inode number of each file.\n");      fprintf(stderr,"    --device    Print device ID number to which each file belongs.\n");      fprintf(stderr,"    --noreport  Turn off file/directory count at end of tree listing.\n");      fprintf(stderr,"    --nolinks   Turn off hyperlinks in HTML output.\n");      fprintf(stderr,"    --dirsfirst List directories before files.\n");      fprintf(stderr,"    --charset X Use charset X for HTML and indentation line output.\n");  }  exit(0);}void listdir(char *d, int *dt, int *ft, u_long lev, dev_t dev){  char *path, nlf = FALSE, colored = FALSE;  long pathsize = 0;  struct _info **dir, **sav;  struct stat sb;  int n,m,e;  char hclr[20], *hdir, *hcmd;  path=malloc(pathsize=4096);  if ((Level >= 0) && (lev > Level)) {    if (!Hflag) fprintf(outfile,"\n");    return;  }  if (xdev && lev == 0) {    stat(d,&sb);    dev = sb.st_dev;  }  sav = dir = read_dir(d,&n);  if (!dir && n) {    fprintf(outfile," [error opening dir]\n");    free(path);    return;  }  if (!n) {    fprintf(outfile,"\n");    free(path);    free_dir(sav);    return;  }  qsort(dir,n,sizeof(struct _info *),cmpfunc);  if (lev >= maxdirs-1) dirs = xrealloc(dirs,sizeof(int) * (maxdirs += 1024));  dirs[lev] = 1;  if (!*(dir+1)) dirs[lev] = 2;  fprintf(outfile,"\n");  while(*dir) {    if (!noindent) indent();    path[0] = 0;#ifdef __USE_FILE_OFFSET64    if (inodeflag) sprintf(path," %7lld",(*dir)->inode);#else    if (inodeflag) sprintf(path," %7ld",(*dir)->inode);#endif    if (devflag) sprintf(path+strlen(path), " %3d", (int)(*dir)->dev);#ifdef __EMX__    if (pflag) sprintf(path+strlen(path), " %s",prot((*dir)->attr));#else    if (pflag) sprintf(path+strlen(path), " %s", prot((*dir)->mode));#endif    if (uflag) sprintf(path+strlen(path), " %-8.8s", uidtoname((*dir)->uid));    if (gflag) sprintf(path+strlen(path), " %-8.8s", gidtoname((*dir)->gid));    if (sflag) psize(path+strlen(path),(*dir)->size);    if (Dflag) sprintf(path+strlen(path), " %s", do_date((*dir)->mtime));    if (path[0] == ' ') {      path[0] = '[';      if (Hflag) {	int i;	for(i=0;path[i];i++) {	  if (path[i] == ' ') fprintf(outfile,"%s",sp);	  else fprintf(outfile,"%c", path[i]);	}	fprintf(outfile,"]%s%s", sp, sp);      } else fprintf(outfile, "%s]  ",path);    }    if (colorize)      colored = color((*dir)->mode,(*dir)->name,(*dir)->orphan,FALSE);    if (fflag) {      if (sizeof(char) * (strlen(d)+strlen((*dir)->name)+2) > pathsize)	path=xrealloc(path,pathsize=(sizeof(char) * (strlen(d)+strlen((*dir)->name)+1024)));      if (!strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name);      else sprintf(path,"%s/%s",d,(*dir)->name);    } else {      if (sizeof(char) * (strlen((*dir)->name)+1) > pathsize)	path=xrealloc(path,pathsize=(sizeof(char) * (strlen((*dir)->name)+1024)));      sprintf(path,"%s",(*dir)->name);    }    if (Hflag) {      if (Rflag && (lev == Level) && (*dir)->isdir) {	if (nolinks) fprintf(outfile,"%s",(*dir)->name);	else {	  fprintf(outfile,"<a href=\"%s",host);	  url_encode(outfile,d+1);	  putc('/',outfile);	  url_encode(outfile,(*dir)->name);	  fprintf(outfile,"/00Tree.html\">");	  html_encode(outfile,(*dir)->name);	  fprintf(outfile,"</a>\n");	}	hdir = gnu_getcwd();	if (sizeof(char) * (strlen(hdir)+strlen(d)+strlen((*dir)->name)+2) > pathsize)	  path = xrealloc(path, pathsize = sizeof(char) * (strlen(hdir)+strlen(d)+strlen((*dir)->name) + 1024));	sprintf(path,"%s%s/%s",hdir,d+1,(*dir)->name);	fprintf(stderr,"Entering directory %s\n",path);	hcmd = xmalloc(sizeof(char) * (49 + strlen(host) + strlen(d) + strlen((*dir)->name)) + 10 + (2*strlen(path)));	sprintf(hcmd,"tree -n -H %s%s/%s -L %d -R -o %s/00Tree.html %s\n", host,d+1,(*dir)->name,Level+1,path,path);	system(hcmd);	free(hdir);	free(hcmd);      } else {	if (nolinks) {	  if (force_color) {	   /*	    * Note that the B element has been set to normal weight in the	    * style portion of the output. so using <b> will just gives us a element	    * for which we can assign a color class to.	    */	    fprintf(outfile, "<b class=\"%s\">%s</b>",		    (*dir)->isdir ?  "DIR"  :		    (*dir)->isexe ?  "EXEC" :		    (*dir)->isfifo ? "FIFO" :		    (*dir)->issok ?  "SOCK" : "NORM", (*dir)->name);	  } else	    fprintf(outfile,"%s",(*dir)->name);	} else {	  if (force_color) {	    sprintf(hclr, "%s",		    (*dir)->isdir ?  "DIR"  :		    (*dir)->isexe ?  "EXEC" :		    (*dir)->isfifo ? "FIFO" :		    (*dir)->issok ?  "SOCK" : "NORM");	    fprintf(outfile,"<a class=\"%s\" href=\"%s%s/%s%s\">%s</a>", hclr, host,d+1,(*dir)->name,		    ((*dir)->isdir?"/":""),(*dir)->name);	  } else {	    fprintf(outfile,"<a href=\"%s",host);	    url_encode(outfile,d+1);	    putc('/',outfile);	    url_encode(outfile,(*dir)->name);	    fprintf(outfile,"%s\">",((*dir)->isdir?"/":""));	    html_encode(outfile,(*dir)->name);	    fprintf(outfile,"</a>");	  }	}      }    } else printit(path);    if (colored) fprintf(outfile,"%s",endcode);    if (Fflag && !(*dir)->lnk) {      if (!dflag && (*dir)->isdir) fprintf(outfile,"/");      else if ((*dir)->issok) fprintf(outfile,"=");      else if ((*dir)->isfifo) fprintf(outfile,"|");      else if (!(*dir)->isdir && (*dir)->isexe) fprintf(outfile,"*");    }    if ((*dir)->lnk && !Hflag) {      fprintf(outfile,"%s->%s",sp,sp);      if (colorize) colored = color((*dir)->lnkmode,(*dir)->lnk,(*dir)->orphan,TRUE);      printit((*dir)->lnk);      if (colored) fprintf(outfile,"%s",endcode);      if (Fflag) {	m = (*dir)->lnkmode & S_IFMT;	e = ((*dir)->lnkmode & S_IEXEC) | ((*dir)->lnkmode & (S_IEXEC>>3)) | ((*dir)->lnkmode & (S_IEXEC>>6));	if (!dflag && m == S_IFDIR) fprintf(outfile,"/");	else if (m == S_IFSOCK) fprintf(outfile,"=");	else if (m == S_IFIFO) fprintf(outfile,"|");	else if (!(m == S_IFDIR) && e) fprintf(outfile,"*");      }    }    if ((*dir)->isdir) {      if ((*dir)->lnk) {	if (lflag && !(xdev && dev != (*dir)->dev)) {	  if (findino((*dir)->inode,(*dir)->dev)) {	    fprintf(outfile,"  [recursive, not followed]");	  } else {	    saveino((*dir)->inode, (*dir)->dev);	    if (*(*dir)->lnk == '/')	      listdir((*dir)->lnk,dt,ft,lev+1,dev);	    else {	      if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024));	      if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->lnk);	      else sprintf(path,"%s/%s",d,(*dir)->lnk);	      listdir(path,dt,ft,lev+1,dev);	    }	    nlf = TRUE;	  }	}      } else if (!(xdev && dev != (*dir)->dev)) {	if (strlen(d)+strlen((*dir)->name)+2 > pathsize) path=xrealloc(path,pathsize=(strlen(d)+strlen((*dir)->name)+1024));	if (fflag && !strcmp(d,"/")) sprintf(path,"%s%s",d,(*dir)->name);	else sprintf(path,"%s/%s",d,(*dir)->name);	saveino((*dir)->inode, (*dir)->dev);	listdir(path,dt,ft,lev+1,dev);	nlf = TRUE;      }      *dt += 1;    } else *ft += 1;    if (*(dir+1) && !*(dir+2)) dirs[lev] = 2;    if (nlf) nlf = FALSE;    else {      if (Hflag) fprintf(outfile,"<br>");      fprintf(outfile,"\n");    }    dir++;  }  dirs[lev] = 0;  free(path);  free_dir(sav);}struct _info **read_dir(char *dir, int *n){  static char *path = NULL, *lbuf = NULL;  static long pathsize, lbufsize;  struct _info **dl;  struct dirent *ent;  struct stat lst,st;  DIR *d;  int ne, p = 0, len, rs;  pathsize = lbufsize = strlen(dir)+4096;  if (path == NULL) {    path=xmalloc(pathsize);    lbuf=xmalloc(lbufsize);  }  *n = 1;  if ((d=opendir(dir)) == NULL) return NULL;  dl = (struct _info **)xmalloc(sizeof(struct _info *) * (ne = MINIT));  while((ent = (struct dirent *)readdir(d))) {    if (!strcmp("..",ent->d_name) || !strcmp(".",ent->d_name)) continue;    if (!Hflag && !strcmp(ent->d_name,"00Tree.html")) continue;    if (!aflag && ent->d_name[0] == '.') continue;    if (strlen(dir)+strlen(ent->d_name)+2 > pathsize) path = xrealloc(path,pathsize=(strlen(dir)+strlen(ent->d_name)+4096));    sprintf(path,"%s/%s",dir,ent->d_name);    if (lstat(path,&lst) < 0) continue;    if ((rs = stat(path,&st)) < 0) st.st_mode = 0;#ifndef __EMX__    if ((lst.st_mode & S_IFMT) != S_IFDIR && !(((st.st_mode & S_IFMT) == S_IFLNK) && lflag)) {      if (pattern && patmatch(ent->d_name,pattern) != 1) continue;    }    if (ipattern && patmatch(ent->d_name,ipattern) == 1) continue;#endif    if (dflag && ((st.st_mode & S_IFMT) != S_IFDIR)) continue;#ifndef __EMX__    if (pattern && ((st.st_mode & S_IFMT) == S_IFLNK) && !lflag) continue;#endif    if (p == (ne-1)) dl = (struct _info **)xrealloc(dl,sizeof(struct _info *) * (ne += MINC));    dl[p] = (struct _info *)xmalloc(sizeof(struct _info));    dl[p]->name = scopy(ent->d_name);    dl[p]->mode = lst.st_mode;    dl[p]->uid = lst.st_uid;    dl[p]->gid = lst.st_gid;    dl[p]->size = lst.st_size;    dl[p]->dev = st.st_dev;    dl[p]->inode = st.st_ino;    dl[p]->lnk = NULL;    dl[p]->orphan = FALSE;    dl[p]->atime = lst.st_atime;    dl[p]->ctime = lst.st_ctime;    dl[p]->mtime = lst.st_mtime;#ifdef __EMX__    dl[p]->attr = lst.st_attr;#else    if ((lst.st_mode & S_IFMT) == S_IFLNK) {      if (lst.st_size+1 > lbufsize) lbuf = xrealloc(lbuf,lbufsize=(lst.st_size+8192));      if ((len=readlink(path,lbuf,lbufsize-1)) < 0) {	dl[p]->lnk = scopy("[Error reading symbolic link information]");	dl[p]->isdir = dl[p]->issok = dl[p]->isexe = FALSE;	dl[p++]->lnkmode = st.st_mode;	continue;      } else {	lbuf[len] = 0;

⌨️ 快捷键说明

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