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

📄 fstest_utils.c

📁 Simple Operating Systems (简称SOS)是一个可以运行在X86平台上(包括QEMU
💻 C
字号:
/* Copyright (C) 2005  David Decotigny   This program is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public License   as published by the Free Software Foundation; either version 2   of the License, or (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,   USA. */#include <libc.h>#include <debug.h>#include "fstest_utils.h"/** Helper functions that dumps the contents of the current working    directory of the process */static void cwd_ls(int detailed, int recursive, int reclevel){  char tab[256], *c;  int i;  struct dirent * dirent;  DIR * here;  here = opendir(".");  if (! here)    return;  /* Build initial tabulation */  if (recursive)    {      for (c = tab, i = 0 ; (i < reclevel) && (i < sizeof(tab)/2) ; i++)	{	  *c++ = ' ';	  *c++ = ' ';	}      *c++ = '\0';    }  else    *tab = '\0';  while ((dirent = readdir(here)) != NULL)    {      char entrychar;      char * entrysuffix;      switch(dirent->type)	{	case S_IFREG: entrychar='-'; entrysuffix=""; break;	case S_IFDIR: entrychar='d'; entrysuffix="/"; break;	case S_IFLNK: entrychar='l'; entrysuffix="@"; break;	default: entrychar='?'; entrysuffix="?!?"; break;	}      if (detailed)	{	  struct stat stat;	  char target_name[SOS_FS_DIRENT_NAME_MAXLEN];	  	  if (lstat(dirent->name, & stat))	    continue;	  *target_name = '\0';	  if (stat.st_type==S_IFLNK)	    {	      int fd = open(dirent->name, O_RDONLY | O_NOFOLLOW);	      if (fd >= 0)		{		  int len = read(fd, target_name, sizeof(target_name) - 1);		  if (len < 0)		    *target_name='\0';		  else		    target_name[len] = '\0';		  close(fd);		}	    }	  bochs_printf("%s%c%c%c%c %lld %s%s%s%s (location=0x%llx)\n",		       tab, entrychar,		       (stat.st_access_rights&S_IRUSR)?'r':'-',		       (stat.st_access_rights&S_IWUSR)?'w':'-',		       (stat.st_access_rights&S_IXUSR)?'x':'-',		       stat.st_size,		       dirent->name,		       entrysuffix,		       (stat.st_type==S_IFLNK)?" -> ":"",		       target_name,		       stat.st_storage_location);	}      else	  bochs_printf("%s%s%s\n",		       tab, dirent->name, entrysuffix);      /* Next iteration */      if (recursive)	{	  int fd_here = dirfd(here);	  if (chdir(dirent->name))	    continue;	  cwd_ls(detailed, recursive, reclevel+1);	  if(fchdir(fd_here))            {		closedir(here);	        return;            }	}    }  closedir(here);}void ls(const char * path, int detailed, int recursive){  int fd_here = open(".", O_RDONLY | O_DIRECTORY);  if (fd_here < 0)    return;  if (chdir(path))    {      close(fd_here);      return;    }  bochs_printf("----------- Contents of %s:\n", path);  cwd_ls(detailed, recursive, 1);  bochs_printf("---------------------------\n");  fchdir(fd_here);  close(fd_here);}

⌨️ 快捷键说明

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