📄 test17.c
字号:
if (errct > MAXERR) {
printf("Too many errors; test aborted\n");
quit();
}
e("");
str("\t");
switch (number) {
case 0:
str(scall);
str(": illegal initial value.");
break;
case 1:
str(scall);
str(": ");
str(name);
str(" value returned.");
break;
case 2:
str(scall);
str(": accepting illegal ");
str(name);
str(".");
break;
case 3:
str(scall);
str(": accepting non-existing file.");
break;
case 4:
str(scall);
str(": could search non-searchable dir (");
str(name);
str(").");
break;
case 5:
str(scall);
str(": cannot ");
str(scall);
str(" ");
str(name);
str(".");
break;
case 7:
str(scall);
str(": incorrect ");
str(name);
str(".");
break;
case 8:
str(scall);
str(": wrong values.");
break;
case 9:
str(scall);
str(": accepting too many ");
str(name);
str(" files.");
break;
case 10:
str(scall);
str(": even a superuser can't do anything!");
break;
case 11:
str(scall);
str(": could ");
str(name);
str(".");
break;
case 12:
str(scall);
str(": could write in non-writable dir (");
str(name);
str(").");
break;
case 13:
str(scall);
str(": wrong filedes returned (");
str(name);
str(").");
break;
case 100:
str(scall); /* very common */
str(": ");
str(name);
str(".");
break;
default: str("errornumber does not exist!\n");
}
nlcr();
} /* err */
/*****************************************************************************
* *
* MAKE_AND_FILL_DIRS *
* *
*****************************************************************************/
void make_and_fill_dirs()
/* Create 8 dir.'s: "d---", "d--x", "d-w-", "d-wx", "dr--", "dr-x", *
* "drw-", "drwx". * Then create 8 files
* in "drwx", and some needed files in other dirs. */
{
int mode, i;
for (i = 0; i < 8; i++) {
mkdir(dir[i], 0700);
chown(dir[i], USER_ID, GROUP_ID);
}
setuid(USER_ID);
setgid(GROUP_ID);
for (mode = 0; mode < 8; mode++) put_file_in_dir("drwx", mode);
put_file_in_dir("d-wx", RWX);
put_file_in_dir("dr-x", RWX);
put_file_in_dir("drw-", RWX);
chmod_8_dirs(8); /* 8 means; 8 different modes */
} /* make_and_fill_dirs */
void put_file_in_dir(dirname, mode)
char *dirname;
int mode;
/* Fill directory 'dirname' with file with mode 'mode'. */
{
int nr;
if (chdir(dirname) != OK)
err(5, CHDIR, "to dirname (put_f_in_dir)");
else {
/* Creat the file */
if ((nr = creat(fnames[mode], mode * 0100)) < 0)
err(13, CREAT, fnames[mode]);
else
try_close(nr, fnames[mode]);
if (chdir("..") != OK)
err(5, CHDIR, "to previous dir (put_f_in_dir)");
}
} /* put_file_in_dir */
/*****************************************************************************
* *
* MISCELLANEOUS *
* *
*(all about arrays, 'try_close', 'try_unlink', 'Remove', 'get_mode')*
* *
*****************************************************************************/
void init_array(a)
char *a;
{
int i;
i = 0;
while (i++ < ARSIZE) *a++ = 'a' + (i % 26);
} /* init_array */
void clear_array(b)
char *b;
{
int i;
i = 0;
while (i++ < ARSIZE) *b++ = '0';
} /* clear_array */
int comp_array(a, b, range)
char *a, *b;
int range;
{
if ((range < 0) || (range > ARSIZE)) {
err(100, "comp_array", "illegal range");
return(FAIL);
} else {
while (range-- && (*a++ == *b++));
if (*--a == *--b)
return(OK);
else
return(FAIL);
}
} /* comp_array */
void try_close(filedes, name)
int filedes;
char *name;
{
if (close(filedes) != OK) err(5, CLOSE, name);
} /* try_close */
void try_unlink(fname)
char *fname;
{
if (unlink(fname) != 0) err(5, UNLINK, fname);
} /* try_unlink */
void Remove(fdes, fname)
int fdes;
char *fname;
{
try_close(fdes, fname);
try_unlink(fname);
} /* Remove */
int get_mode(name)
char *name;
{
struct stat stbf1;
if (stat(name, &stbf1) != OK) {
err(5, STAT, name);
return(stbf1.st_mode); /* return a mode which will cause *
* error in the calling function *
* (file/dir bit) */
} else
return(stbf1.st_mode & 07777); /* take last 4 bits */
} /* get_mode */
/*****************************************************************************
* *
* CHECK *
* *
*****************************************************************************/
void check(scall, number)
int number;
char *scall;
{
if (errno != number) {
e(NIL);
str("\t");
str(scall);
str(": bad errno-value: ");
put(errno);
str(" should have been: ");
put(number);
nlcr();
}
} /* check */
void put(nr)
int nr;
{
switch (nr) {
case 0: str("unused"); break;
case 1: str("EPERM"); break;
case 2: str("ENOENT"); break;
case 3: str("ESRCH"); break;
case 4: str("EINTR"); break;
case 5: str("EIO"); break;
case 6: str("ENXIO"); break;
case 7: str("E2BIG"); break;
case 8: str("ENOEXEC"); break;
case 9: str("EBADF"); break;
case 10: str("ECHILD"); break;
case 11: str("EAGAIN"); break;
case 12: str("ENOMEM"); break;
case 13: str("EACCES"); break;
case 14: str("EFAULT"); break;
case 15: str("ENOTBLK"); break;
case 16: str("EBUSY"); break;
case 17: str("EEXIST"); break;
case 18: str("EXDEV"); break;
case 19: str("ENODEV"); break;
case 20: str("ENOTDIR"); break;
case 21: str("EISDIR"); break;
case 22: str("EINVAL"); break;
case 23: str("ENFILE"); break;
case 24: str("EMFILE"); break;
case 25: str("ENOTTY"); break;
case 26: str("ETXTBSY"); break;
case 27: str("EFBIG"); break;
case 28: str("ENOSPC"); break;
case 29: str("ESPIPE"); break;
case 30: str("EROFS"); break;
case 31: str("EMLINK"); break;
case 32: str("EPIPE"); break;
case 33: str("EDOM"); break;
case 34: str("ERANGE"); break;
}
}
/*****************************************************************************
* *
* ALOT-functions *
* *
*****************************************************************************/
int open_alot()
{
int i;
for (i = 0; i < MAXOPEN; i++)
if (open(file[i], R) == FAIL) break;
if (i == 0) err(5, "open_alot", "at all");
return(i);
} /* open_alot */
int close_alot(number)
int number;
{
int i, count = 0;
if (number > MAXOPEN)
err(5, "close_alot", "accept this argument");
else
for (i = FF; i < number + FF; i++)
if (close(i) != OK) count++;
return(number - count); /* return number of closed files */
} /* close_alot */
/*****************************************************************************
* *
* CLEAN UP THE MESS *
* *
*****************************************************************************/
void clean_up_the_mess()
{
int i;
char dirname[6];
/* First remove 'a lot' files */
for (i = 0; i < MAXOPEN; i++) try_unlink(file[i]);
/* Unlink the files in dir 'drwx' */
if (chdir("drwx") != OK)
err(5, CHDIR, "to 'drwx'");
else {
for (i = 0; i < 8; i++) try_unlink(fnames[i]);
if (chdir("..") != OK) err(5, CHDIR, "to '..'");
}
/* Before unlinking files in some dirs, make them writable */
chmod_8_dirs(RWX);
/* Unlink files in other dirs */
try_unlink("d-wx/rwx");
try_unlink("dr-x/rwx");
try_unlink("drw-/rwx");
/* Unlink dirs */
for (i = 0; i < 8; i++) {
strcpy(dirname, "d");
strcat(dirname, fnames[i]);
/* 'dirname' contains the directoryname */
rmdir(dirname);
}
/* FINISH */
} /* clean_up_the_mess */
void chmod_8_dirs(sw)
int sw; /* if switch == 8, give all different
* mode,else the same mode */
{
int mode;
int i;
if (sw == 8)
mode = 0;
else
mode = sw;
for (i = 0; i < 8; i++) {
chmod(dir[i], 040000 + mode * 0100);
if (sw == 8) mode++;
}
}
void quit()
{
chdir("..");
system("rm -rf DIR*");
if (errct == 0) {
printf("ok\n");
exit(0);
} else {
printf("%d errors\n", errct);
exit(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -