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

📄 test17.c

📁 MINIX2.0操作系统源码 MINIX2.0操作系统源码
💻 C
📖 第 1 页 / 共 3 页
字号:
  /* Try to chmod a file, check and restore old values, check */
  if (chmod("drwx/file09", 0700) != OK)
	err(5, CHMOD, "'drwx/file09'");	/* set rwx */
  else {
	/* Check protection */
	if (get_mode("drwx/file09") != 0700) err(7, CHMOD, "mode");

	/* Test if chmod accepts just filenames too */
	if (chdir("drwx") != OK)
		err(5, CHDIR, "to '/drwx'");
	else if (chmod("file09", 0177) != OK)	/* restore oldies */
		err(5, CHMOD, "'h1'");
	else
		/* Check if value has been restored */
	if (get_mode("../drwx/file09") != 0177)
		err(7, CHMOD, "restored mode");
  }

  /* Try setuid and setgid */
  if ((chmod("file09", 04777) != OK) || (get_mode("file09") != 04777))
	err(11, CHMOD, "not set uid-bit");
  if ((chmod("file09", 02777) != OK) || (get_mode("file09") != 02777))
	err(11, CHMOD, "not set gid-bit");

  /* Remove testfile */
  try_unlink("file09");

  if (chdir("..") != OK) err(5, CHDIR, "to '..'");

  /* Try to chmod directory */
  if (chmod("d---", 0777) != OK)
	err(5, CHMOD, "dir 'd---'");
  else {
	if (get_mode("d---") != 0777) err(7, CHMOD, "protection value");
	if (chmod("d---", 0000) != OK) err(5, CHMOD, "dir 'a' 2nd time");

	/* Check if old value has been restored */
	if (get_mode("d---") != 0000)
		err(7, CHMOD, "restored protection value");
  }

  /* Try to make chmod failures */

  /* We still are in dir root */
  /* Wrong filename */
  if (chmod("non-file", 0777) != FAIL)
	err(3, CHMOD, NIL);
  else
	check(CHMOD, ENOENT);

}				/* test 09 */

/* New page */


/* "t4.c", created by Rene Montsma and Menno Wilcke */

/*****************************************************************************
 *                              test LINK/UNLINK                             *
 ****************************************************************************/
void test10()
{
  int n, n1;
  char a[ARSIZE], b[ARSIZE], *f, *lf;

  f = "file10";
  lf = "linkfile10";

  if ((n = creat(f, 0702)) != FF)	/* no other open files */
	err(13, CREAT, f);
  else {
	/* Now link correctly */
	if (link(f, lf) != OK)
		err(5, LINK, lf);
	else if ((n1 = open(lf, RW)) < 0)
		err(5, OPEN, "'linkfile10'");
	else {
		init_array(a);
		clear_array(b);

		/* Write on 'file10' means being able to    * read
		 * through linked filedescriptor       */
		if (write(n, a, ARSIZE) != ARSIZE) err(1, WRITE, "bad");
		if (read(n1, b, ARSIZE) != ARSIZE) err(1, READ, "bad");
		if (comp_array(a, b, ARSIZE) != OK) err(8, "r/w", NIL);

		/* Clean up: unlink and close (twice): */
		Remove(n, f);
		try_close(n1, "'linkfile10'");

		/* Check if "linkfile" exists and the info    * on it
		 * is correct ('file' has been deleted) */
		if ((n1 = open(lf, R)) < 0)
			err(5, OPEN, "'linkfile10'");
		else {
			/* See if 'linkfile' still contains 0..511 ? */

			clear_array(b);
			if (read(n1, b, ARSIZE) != ARSIZE)
				err(1, READ, "bad");
			if (comp_array(a, b, ARSIZE) != OK)
				err(8, "r/w", NIL);

			try_close(n1, "'linkfile10' 2nd time");
			try_unlink(lf);
		}
	}
  }

  /* Try if unlink fails with incorrect parameters */
  /* File does not exist: */
  if (unlink("non-file") != FAIL)
	err(2, UNLINK, "name");
  else
	check(UNLINK, ENOENT);

  /* Dir can't be written */
  if (unlink("dr-x/rwx") != FAIL)
	err(11, UNLINK, "could unlink in non-writable dir.");
  else
	check(UNLINK, EACCES);

  /* Try to unlink a dir being user */
  if (unlink("drwx") != FAIL)
	err(11, UNLINK, "unlink dir's as user");
  else
	check(UNLINK, EPERM);

  /* Try giving link wrong input */

  /* First try if link fails with incorrect parameters * name1 does not
   * exist.                             */
  if (link("non-file", "linkfile") != FAIL)
	err(2, LINK, "1st name");
  else
	check(LINK, ENOENT);

  /* Name2 exists already */
  if (link("drwx/rwx", "drwx/rw-") != FAIL)
	err(2, LINK, "2nd name");
  else
	check(LINK, EEXIST);

  /* Directory of name2 not writable:  */
  if (link("drwx/rwx", "dr-x/linkfile") != FAIL)
	err(11, LINK, "link non-writable  file");
  else
	check(LINK, EACCES);

  /* Try to link a dir, being a user */
  if (link("drwx", "linkfile") != FAIL)
	err(11, LINK, "link a dir without superuser!");
  else
	check(LINK, EPERM);

  /* File has too many links */
  if ((n = link_alot("drwx/rwx")) != LINKCOUNT - 1)	/* file already has one
							 * link */
	err(5, LINK, "many files");
  if (unlink_alot(n) != n) err(5, UNLINK, "all linked files");

}				/* test10 */


int link_alot(bigboss)
char *bigboss;
{
  int i;
  static char employee[6] = "aaaaa";

  /* Every file has already got 1 link, so link 0176 times */
  for (i = 1; i < LINKCOUNT; i++) {
	if (link(bigboss, employee) != OK)
		break;
	else
		get_new(employee);
  }

  return(i - 1);		/* number of linked files */
}				/* link_alot */

int unlink_alot(number)
int number;			/* number of files to be unlinked */
{
  int j;
  static char employee[6] = "aaaaa";

  for (j = 0; j < number; j++) {
	if (unlink(employee) != OK)
		break;
	else
		get_new(employee);
  }

  return(j);			/* return number of unlinked files */
}				/* unlink_alot */

void get_new(name)
char name[];
 /* Every call changes string 'name' to a string alphabetically          *
  * higher. Start with "aaaaa", next value: "aaaab" .                    *
  * N.B. after "aaaaz" comes "aaabz" and not "aaaba" (not needed).       *
  * The last possibility will be "zzzzz".                                *
  * Total # possibilities: 26+25*4 = 126 = MAXLINK -1 (exactly needed)   */
{
  int i;

  for (i = 4; i >= 0; i--)
	if (name[i] != 'z') {
		name[i]++;
		break;
	}
}				/* get_new */

/* New page */

/*****************************************************************************
 *                              test PIPE                                    *
 ****************************************************************************/
void test11()
{
  int n, fd[2];
  char a[ARSIZE], b[ARSIZE];

  if (pipe(fd) != OK)
	err(13, PIPE, NIL);
  else {
	/* Try reading and writing on a pipe */
	init_array(a);
	clear_array(b);

	if (write(fd[1], a, ARSIZE) != ARSIZE)
		err(5, WRITE, "on pipe");
	else if (read(fd[0], b, (ARSIZE / 2)) != (ARSIZE / 2))
		err(5, READ, "on pipe (2nd time)");
	else if (comp_array(a, b, (ARSIZE / 2)) != OK)
		err(7, PIPE, "values read/written");
	else if (read(fd[0], b, (ARSIZE / 2)) != (ARSIZE / 2))
		err(5, READ, "on pipe 2");
	else if (comp_array(&a[ARSIZE / 2], b, (ARSIZE / 2)) != OK)
		err(7, PIPE, "pipe created");

	/* Try to let the pipe make a mistake */
	if (write(fd[0], a, ARSIZE) != FAIL)
		err(11, WRITE, "write on fd[0]");
	if (read(fd[1], b, ARSIZE) != FAIL) err(11, READ, "read on fd[1]");

	try_close(fd[1], "'fd[1]'");

	/* Now we shouldn't be able to read, because fd[1] has been closed */
	if (read(fd[0], b, ARSIZE) != END_FILE) err(2, PIPE, "'fd[1]'");

	try_close(fd[0], "'fd[0]'");
  }
  if (pipe(fd) < 0)
	err(5, PIPE, "2nd time");
  else {
	/* Test lseek on a pipe: should fail */
	if (write(fd[1], a, ARSIZE) != ARSIZE)
		err(5, WRITE, "on pipe (2nd time)");
	if (lseek(fd[1], 10L, SEEK_SET) != FAIL)
		err(11, LSEEK, "lseek on a pipe");
	else
		check(PIPE, ESPIPE);

	/* Eat half of the pipe: no writing should be possible */
	try_close(fd[0], "'fd[0]'  (2nd time)");

	/* This makes UNIX crash: omit it if pdp or VAX */
#ifndef NOCRASH
	if (write(fd[1], a, ARSIZE) != FAIL)
		err(11, WRITE, "write on wrong pipe");
	else
		check(PIPE, EPIPE);
#endif
	try_close(fd[1], "'fd[1]'  (2nd time)");
  }

  /* BUG :                                                            *
   * Here we planned to test if we could write 4K bytes on a pipe.    *
   * However, this was not possible to implement, because the whole   *
   * Monix system crashed when we tried to write more then 3584 bytes *
   * (3.5K) on a pipe. That's why we try to write only 3.5K in the    *
   * folowing test.                                                   */
  if (pipe(fd) < 0)
	err(5, PIPE, "3rd time");
  else {
	for (n = 0; n < (PIPESIZE / ARSIZE); n++)
		if (write(fd[1], a, ARSIZE) != ARSIZE)
			err(5, WRITE, "on pipe (3rd time) 4K");
	try_close(fd[1], "'fd[1]' (3rd time)");

	for (n = 0; n < (PIPESIZE / ARSIZE); n++)
		if (read(fd[0], b, ARSIZE) != ARSIZE)
			err(5, READ, "from pipe (3rd time) 4K");
	try_close(fd[0], "'fd[0]' (3rd time)");
  }

  /* Test opening a lot of files */
  if ((n = open_alot()) != MAXOPEN) err(5, OPEN, "MAXOPEN files");
  if (pipe(fd) != FAIL)
	err(9, PIPE, "open");
  else
	check(PIPE, EMFILE);
  if (close_alot(n) != n) err(5, CLOSE, "all opened files");
}				/* test11 */

/* New page */


void comp_stats(stbf1, stbf2)
struct stat *stbf1, *stbf2;
{
  if (stbf1->st_dev != stbf2->st_dev) err(7, "st/fst", "'dev'");
  if (stbf1->st_ino != stbf2->st_ino) err(7, "st/fst", "'ino'");
  if (stbf1->st_mode != stbf2->st_mode) err(7, "st/fst", "'mode'");
  if (stbf1->st_nlink != stbf2->st_nlink) err(7, "st/fst", "'nlink'");
  if (stbf1->st_uid != stbf2->st_uid) err(7, "st/fst", "'uid'");
  if (stbf1->st_gid != stbf2->st_gid) err(7, "st/fst", "'gid'");
  if (stbf1->st_rdev != stbf2->st_rdev) err(7, "st/fst", "'rdev'");
  if (stbf1->st_size != stbf2->st_size) err(7, "st/fst", "'size'");
  if (stbf1->st_atime != stbf2->st_atime) err(7, "st/fst", "'atime'");
  if (stbf1->st_mtime != stbf2->st_mtime) err(7, "st/fst", "'mtime'");
}				/* comp_stats */

/* New page */


/* "t5.c", created by Rene Montsma and Menno Wilcke */



void comp_inodes(m, m1)
int m, m1;			/* twee filedes's */
{
  struct stat stbf1, stbf2;

  if (fstat(m, &stbf1) == OK)
	if (fstat(m1, &stbf2) == OK) {
		if (stbf1.st_ino != stbf2.st_ino)
			err(7, DUP, "inode number");
	} else
		err(100, "comp_inodes", "cannot 'fstat' (m1)");
  else
	err(100, "comp_inodes", "cannot 'fstat' (m)");
}				/* comp_inodes */

/* "support.c", created by Rene Montsma and Menno Wilcke */

/* Err, make_and_fill_dirs, init_array, clear_array, comp_array,
   try_close, try_unlink, Remove, get_mode, check, open_alot,
   close_alot, clean_up_the_mess.
*/

/***********************************************************************
 *				EXTENDED FIONS			       *
 **********************************************************************/
/* First extended functions (i.e. not oldfashioned monixcalls.
   e(), nlcr(), octal.*/

void e(string)
char *string;
{
  printf("Error: %s ", string);
}

void nlcr()
{
  printf("\n");
}

void str(s)
char *s;
{
  printf(s);
}

/*****************************************************************************
*                                                                            *
*                               ERR(or) messages                             *
*                                                                            *
*****************************************************************************/
void err(number, scall, name)
 /* Give nice error messages */

char *scall, *name;
int number;

{
  errct++;

⌨️ 快捷键说明

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