Subject: chflags - immutable|appendonly files come to 2BSD(#206 - Part 10 of 14)
Index:	bin,etc,lib,sys/many_files 2.11BSD

Description:
	Files/directories can not be declared append-only, immutable, archived 
	or not to be dumped under 2.11BSD.

	dump(8) lacked the ability to bypass files marked for 'nodump'.

	Upper case only and Hazeltine terminal support made the tty driver 
	(and several utilities - 'vi' and 'getty' to name two) needlessly
	complicated and larger (especially since those devices are obsolete).

	open(2) can not provide for atomically obtaining a flock(2)'d
	file descriptor.

	open(2)ing with O_NDELAY (now called O_NONBLOCK) would not wait
	for carrier on the first i/o operation.

	Obsolete stub system calls 'setdopt' and 'getdopt' were still
	present in the kernel even though they have never done anything
	except take up space.

Repeat-By:
	This section does not apply this time since this is an 'update'
	which adds new functionality rather than fixing an existing bug.

Fix:
	Refer to part 0 (#197) for the complete description and installation
	instructions.

	This is part 10 of 14 (#206).

	Files affected by this part are:

/usr/src/bin/Makefile
/usr/src/bin/csh/sh.sem.c
/usr/src/bin/ls/ls.c
/usr/src/bin/sh/service.c
/usr/src/bin/stty.c

=====================cut  here========================
*** /usr/src/bin/Makefile.old	Sat Jan 15 23:41:35 1994
--- /usr/src/bin/Makefile	Sat Dec  3 20:54:58 1994
***************
*** 3,9 ****
  # All rights reserved.  The Berkeley software License Agreement
  # specifies the terms and conditions for redistribution.
  #
! #	@(#)Makefile	5.19.2 (2.11BSD GTE) 1/15/94
  #
  DESTDIR=
  CFLAGS=	-O
--- 3,9 ----
  # All rights reserved.  The Berkeley software License Agreement
  # specifies the terms and conditions for redistribution.
  #
! #	@(#)Makefile	5.19.3 (2.11BSD GTE) 12/3/94
  #
  DESTDIR=
  CFLAGS=	-O
***************
*** 11,17 ****
  
  # Programs that live in subdirectories, and have makefiles of their own.
  #
! SUBDIR=	adb ar as awk chpass csh diff ld login make nm passwd sed sh tp
  
  # Shell scripts that need only be installed and are never removed.
  #
--- 11,18 ----
  
  # Programs that live in subdirectories, and have makefiles of their own.
  #
! SUBDIR=	adb ar as awk chflags chpass csh diff ld login ls make nm passwd \
! 	sed sh tp
  
  # Shell scripts that need only be installed and are never removed.
  #
***************
*** 21,27 ****
  # explicit make lines.
  #
  STD=	cat cc chgrp chmod cmp cp date dd du echo ed grep hostid \
! 	hostname kill ln ls mkdir mt mv nice od pagesize pr \
  	pwd rm rmail rmdir size stty sync tar tee test time who
  
  # C programs that live in the current directory and need explicit make lines.
--- 22,28 ----
  # explicit make lines.
  #
  STD=	cat cc chgrp chmod cmp cp date dd du echo ed grep hostid \
! 	hostname kill ln mkdir mt mv nice od pagesize pr \
  	pwd rm rmail rmdir size stty sync tar tee test time who
  
  # C programs that live in the current directory and need explicit make lines.
*** /usr/src/bin/csh/sh.sem.c.old	Sat Aug 31 00:11:45 1991
--- /usr/src/bin/csh/sh.sem.c	Fri Dec  9 20:19:59 1994
***************
*** 5,15 ****
   */
  
  #if	!defined(lint) && defined(DOSCCS)
! static char *sccsid = "@(#)sh.sem.c	5.4 (Berkeley) 5/13/86";
  #endif
  
  #include "sh.h"
  #include "sh.proc.h"
  #include <sys/ioctl.h>
  
  /*
--- 5,16 ----
   */
  
  #if	!defined(lint) && defined(DOSCCS)
! static char *sccsid = "@(#)sh.sem.c	5.4.1 (2.11BSD GTE) 12/9/94";
  #endif
  
  #include "sh.h"
  #include "sh.proc.h"
+ #include <fcntl.h>
  #include <sys/ioctl.h>
  
  /*
***************
*** 360,367 ****
  	if (cp = t->t_drit) {
  		cp = globone(Dfix1(cp));
  		xfree(cp);
! 		if ((flags & FCAT) && open(cp, 1) >= 0)
! 			(void) lseek(1, (off_t)0, 2);
  		else {
  			if (!(flags & FANY) && adrof("noclobber")) {
  				if (flags & FCAT)
--- 361,368 ----
  	if (cp = t->t_drit) {
  		cp = globone(Dfix1(cp));
  		xfree(cp);
! 		if ((flags & FCAT) && open(cp, O_WRONLY | O_APPEND) >= 0)
! 			;
  		else {
  			if (!(flags & FANY) && adrof("noclobber")) {
  				if (flags & FCAT)
*** /usr/src/bin/ls/ls.c.old	Tue May 16 20:38:42 1989
--- /usr/src/bin/ls/ls.c	Sat Dec  3 20:50:06 1994
***************
*** 4,18 ****
   * specifies the terms and conditions for redistribution.
   */
  
! #ifndef lint
  char copyright[] =
  "@(#) Copyright (c) 1980 Regents of the University of California.\n\
   All rights reserved.\n";
- #endif not lint
  
! #ifndef lint
! static char sccsid[] = "@(#)ls.c	5.9 (Berkeley) 10/22/87";
! #endif not lint
  
  /*
   * ls
--- 4,16 ----
   * specifies the terms and conditions for redistribution.
   */
  
! #if	!defined(lint) && defined(DOSCCS)
  char copyright[] =
  "@(#) Copyright (c) 1980 Regents of the University of California.\n\
   All rights reserved.\n";
  
! static char sccsid[] = "@(#)ls.c	5.9.1 (2.11BSD GTE) 12/3/94";
! #endif
  
  /*
   * ls
***************
*** 25,30 ****
--- 23,30 ----
  #include <sys/dir.h>
  #include <stdio.h>
  #include <sgtty.h>
+ #include <strings.h>
+ #include <sys/time.h>
  
  #define	kbytes(size)	(((size) + 1023) / 1024)
  
***************
*** 31,37 ****
  struct afile {
  	char	ftype;		/* file type, e.g. 'd', 'c', 'f' */
  	ino_t	fnum;		/* inode number of file */
! 	short	fflags;		/* mode&~S_IFMT, perhaps ISARG */
  	short	fnl;		/* number of links */
  	uid_t	fuid;		/* owner id */
  	gid_t	fgid;		/* group id */
--- 31,38 ----
  struct afile {
  	char	ftype;		/* file type, e.g. 'd', 'c', 'f' */
  	ino_t	fnum;		/* inode number of file */
! 	short	fmode;		/* mode&~S_IFMT, perhaps ISARG */
! 	u_short	fflags;		/* st_flags (uappnd, uchg, schg, ...) */
  	short	fnl;		/* number of links */
  	uid_t	fuid;		/* owner id */
  	gid_t	fgid;		/* group id */
***************
*** 49,59 ****
  	struct	subdirs *sd_next;
  } *subdirs;
  
! int	aflg, dflg, gflg, lflg, sflg, tflg, uflg, iflg, fflg, cflg, rflg = 1;
! int	qflg, Aflg, Cflg, Fflg, Lflg, Rflg;
  
- int	usetabs;
- 
  time_t	now, sixmonthsago;
  
  char	*dotp = ".";
--- 50,58 ----
  	struct	subdirs *sd_next;
  } *subdirs;
  
! char	aflg, dflg, gflg, lflg, sflg, tflg, uflg, iflg, fflg, cflg, rflg = 1;
! char	oflg, qflg, Aflg, Cflg, Fflg, Lflg, Rflg, usetabs;
  
  time_t	now, sixmonthsago;
  
  char	*dotp = ".";
***************
*** 67,76 ****
  char	*fmtentry();
  char	*getname(), *getgroup();
  
- char	*ctime();
- char	*malloc(), *calloc(), *realloc();
- char	*strcpy(), *strcat();
- 
  main(argc, argv)
  	int argc;
  	char *argv[];
--- 66,71 ----
***************
*** 94,100 ****
  			usetabs = 1;
  	} else
  		usetabs = 1;
! 	while ((ch = getopt(argc, argv, "1ACLFRacdfgilqrstu")) != EOF)
  		switch((char)ch) {
  		case '1':
  			Cflg = 0; break;
--- 89,95 ----
  			usetabs = 1;
  	} else
  		usetabs = 1;
! 	while ((ch = getopt(argc, argv, "1ACLFRacdfgiloqrstu")) != EOF)
  		switch((char)ch) {
  		case '1':
  			Cflg = 0; break;
***************
*** 122,127 ****
--- 117,124 ----
  			iflg++; break;
  		case 'l':
  			lflg++; break;
+ 		case 'o':
+ 			oflg++; break;
  		case 'q':
  			qflg = 1; break;
  		case 'r':
***************
*** 134,142 ****
  			uflg++; break;
  		case '?':
  		default:
! 			fputs("usage: ls [ -1ACLFRacdfgilqrstu ] [ file ]\n", stderr);
  			exit(1);
  	}
  	if (fflg) { 
  		aflg++; lflg = 0; sflg = 0; tflg = 0;
  	}
--- 131,141 ----
  			uflg++; break;
  		case '?':
  		default:
! 			fputs("usage: ls [ -1ACLFRacdfgiloqrstu ] [ file ]\n", stderr);
  			exit(1);
  	}
+ 	if (!lflg)
+ 		oflg = 0;
  	if (fflg) { 
  		aflg++; lflg = 0; sflg = 0; tflg = 0;
  	}
***************
*** 157,163 ****
  	for (i = 0; i < argc; i++) {
  		if (gstat(fp, *argv, 1, (int *)0)) {
  			fp->fname = *argv;
! 			fp->fflags |= ISARG;
  			fp++;
  		}
  		argv++;
--- 156,162 ----
  	for (i = 0; i < argc; i++) {
  		if (gstat(fp, *argv, 1, (int *)0)) {
  			fp->fname = *argv;
! 			fp->fmode |= ISARG;
  			fp++;
  		}
  		argv++;
***************
*** 227,233 ****
  			dp->sd_next = subdirs; subdirs = dp;
  		}
  	for (fp = dfp0; fp < dfplast; fp++) {
! 		if ((fp->fflags&ISARG) == 0 && fp->fname)
  			cfree(fp->fname);
  		if (fp->flinkto)
  			cfree(fp->flinkto);
--- 226,232 ----
  			dp->sd_next = subdirs; subdirs = dp;
  		}
  	for (fp = dfp0; fp < dfplast; fp++) {
! 		if ((fp->fmode&ISARG) == 0 && fp->fname)
  			cfree(fp->fname);
  		if (fp->flinkto)
  			cfree(fp->flinkto);
***************
*** 298,304 ****
  	static struct afile azerofile;
  
  	*fp = azerofile;
! 	fp->fflags = 0;
  	fp->fnum = 0;
  	fp->ftype = '-';
  	if (statarg || sflg || lflg || tflg) {
--- 297,303 ----
  	static struct afile azerofile;
  
  	*fp = azerofile;
! 	fp->fmode = 0;
  	fp->fnum = 0;
  	fp->ftype = '-';
  	if (statarg || sflg || lflg || tflg) {
***************
*** 343,349 ****
  			break;
  		}
  		fp->fnum = stb.st_ino;
! 		fp->fflags = stb.st_mode & ~S_IFMT;
  		fp->fnl = stb.st_nlink;
  		fp->fuid = stb.st_uid;
  		fp->fgid = stb.st_gid;
--- 342,349 ----
  			break;
  		}
  		fp->fnum = stb.st_ino;
! 		fp->fmode = stb.st_mode & ~S_IFMT;
! 		fp->fflags = stb.st_flags;
  		fp->fnl = stb.st_nlink;
  		fp->fuid = stb.st_uid;
  		fp->fgid = stb.st_gid;
***************
*** 365,380 ****
  	register struct afile *fp;
  	register int i, j, w;
  	int width = 0, nentry = fplast - fp0;
! 	int columns, lines;
  	char *cp;
  
  	if (fp0 == fplast)
  		return;
  	if (lflg || Cflg == 0)
  		columns = 1;
  	else {
  		for (fp = fp0; fp < fplast; fp++) {
! 			int len = strlen(fmtentry(fp));
  
  			if (len > width)
  				width = len;
--- 365,389 ----
  	register struct afile *fp;
  	register int i, j, w;
  	int width = 0, nentry = fplast - fp0;
! 	int columns, lines, maxflags;
  	char *cp;
  
  	if (fp0 == fplast)
  		return;
+ 	maxflags = 0;
+ 	if (oflg) {
+ 		for (fp = fp0; fp < fplast; fp++)
+ 		    {
+ 		    i = strlen(flags_to_string(fp->fflags, "-"));
+ 		    if (i > maxflags)
+ 			maxflags = i;
+ 		    }
+ 	}
  	if (lflg || Cflg == 0)
  		columns = 1;
  	else {
  		for (fp = fp0; fp < fplast; fp++) {
! 			int len = strlen(fmtentry(fp, maxflags));
  
  			if (len > width)
  				width = len;
***************
*** 391,397 ****
  	for (i = 0; i < lines; i++) {
  		for (j = 0; j < columns; j++) {
  			fp = fp0 + j * lines + i;
! 			cp = fmtentry(fp);
  			fputs(cp, stdout);
  			if (fp + lines >= fplast) {
  				putchar('\n');
--- 400,406 ----
  	for (i = 0; i < lines; i++) {
  		for (j = 0; j < columns; j++) {
  			fp = fp0 + j * lines + i;
! 			cp = fmtentry(fp, maxflags);
  			fputs(cp, stdout);
  			if (fp + lines >= fplast) {
  				putchar('\n');
***************
*** 415,425 ****
  {
  
  	if (dflg == 0 && fflg == 0) {
! 		if ((f1->fflags&ISARG) && f1->ftype == 'd') {
! 			if ((f2->fflags&ISARG) == 0 || f2->ftype != 'd')
  				return (1);
  		} else {
! 			if ((f2->fflags&ISARG) && f2->ftype == 'd')
  				return (-1);
  		}
  	}
--- 424,434 ----
  {
  
  	if (dflg == 0 && fflg == 0) {
! 		if ((f1->fmode&ISARG) && f1->ftype == 'd') {
! 			if ((f2->fmode&ISARG) == 0 || f2->ftype != 'd')
  				return (1);
  		} else {
! 			if ((f2->fmode&ISARG) && f2->ftype == 'd')
  				return (-1);
  		}
  	}
***************
*** 457,476 ****
  savestr(str)
  	char *str;
  {
! 	char *cp = malloc(strlen(str) + 1);
  
  	if (cp == NULL) {
  		fputs("ls: out of memory\n", stderr);
  		exit(1);
  	}
! 	return(strcpy(cp, str));
  }
  
  char	*fmtinum(), *fmtsize(), *fmtlstuff(), *fmtmode();
  
  char *
! fmtentry(fp)
  	register struct afile *fp;
  {
  	static char fmtres[BUFSIZ];
  	register char *cp, *dp;
--- 466,486 ----
  savestr(str)
  	char *str;
  {
! 	register char *cp = strdup(str);
  
  	if (cp == NULL) {
  		fputs("ls: out of memory\n", stderr);
  		exit(1);
  	}
! 	return(cp);
  }
  
  char	*fmtinum(), *fmtsize(), *fmtlstuff(), *fmtmode();
  
  char *
! fmtentry(fp, maxflags)
  	register struct afile *fp;
+ 	int maxflags;
  {
  	static char fmtres[BUFSIZ];
  	register char *cp, *dp;
***************
*** 478,484 ****
  	(void) sprintf(fmtres, "%s%s%s",
  	    iflg ? fmtinum(fp) : "",
  	    sflg ? fmtsize(fp) : "",
! 	    lflg ? fmtlstuff(fp) : "");
  	dp = &fmtres[strlen(fmtres)];
  	for (cp = fp->fname; *cp; cp++)
  		if (qflg && (*cp < ' ' || *cp >= 0177))
--- 488,494 ----
  	(void) sprintf(fmtres, "%s%s%s",
  	    iflg ? fmtinum(fp) : "",
  	    sflg ? fmtsize(fp) : "",
! 	    lflg ? fmtlstuff(fp, maxflags) : "");
  	dp = &fmtres[strlen(fmtres)];
  	for (cp = fp->fname; *cp; cp++)
  		if (qflg && (*cp < ' ' || *cp >= 0177))
***************
*** 492,498 ****
  			*dp++ = '@';
  		else if (fp->ftype == 's')
  			*dp++ = '=';
! 		else if (fp->fflags & 0111)
  			*dp++ = '*';
  	}
  	if (lflg && fp->flinkto) {
--- 502,508 ----
  			*dp++ = '@';
  		else if (fp->ftype == 's')
  			*dp++ = '=';
! 		else if (fp->fmode & 0111)
  			*dp++ = '*';
  	}
  	if (lflg && fp->flinkto) {
***************
*** 521,527 ****
  fmtsize(p)
  	register struct afile *p;
  {
! 	static char sizebuf[32];
  
  	(void) sprintf(sizebuf, "%4ld ", kbytes(dbtob(p->fblks)));
  	return (sizebuf);
--- 531,537 ----
  fmtsize(p)
  	register struct afile *p;
  {
! 	static char sizebuf[16];
  
  	(void) sprintf(sizebuf, "%4ld ", kbytes(dbtob(p->fblks)));
  	return (sizebuf);
***************
*** 528,538 ****
  }
  
  char *
! fmtlstuff(p)
  	register struct afile *p;
  {
  	static char lstuffbuf[256];
! 	char gname[32], uname[32], fsize[32], ftime[32];
  	register char *lp = lstuffbuf;
  
  	/* type mode uname gname fsize ftime */
--- 538,549 ----
  }
  
  char *
! fmtlstuff(p, maxflags)
  	register struct afile *p;
+ 	int maxflags;
  {
  	static char lstuffbuf[256];
! 	char gname[32], uname[32], fsize[32], ftime[32], fflags[64];
  	register char *lp = lstuffbuf;
  
  	/* type mode uname gname fsize ftime */
***************
*** 551,556 ****
--- 562,571 ----
  	  else
  		(void) sprintf(gname, "%-9u", p->fgid);
  	}
+ /* get flags */
+ 	if (oflg)
+ 		(void) sprintf(fflags, "%-*s ", maxflags, 
+ 				flags_to_string(p->fflags, "-"));
  /* get fsize */
  	if (p->ftype == 'b' || p->ftype == 'c')
  		(void) sprintf(fsize, "%3d,%4d",
***************
*** 568,576 ****
  	}
  /* splat */
  	*lp++ = p->ftype;
! 	lp = fmtmode(lp, p->fflags);
! 	(void) sprintf(lp, "%3d %s%s%s%s",
! 	    p->fnl, uname, gflg ? gname : "", fsize, ftime);
  	return (lstuffbuf);
  }
  
--- 583,591 ----
  	}
  /* splat */
  	*lp++ = p->ftype;
! 	lp = fmtmode(lp, p->fmode);
! 	(void) sprintf(lp, "%3d %s%s%s%s%s",
! 	    p->fnl, uname, gflg ? gname : "", oflg ? fflags : "", fsize, ftime);
  	return (lstuffbuf);
  }
  
*** /usr/src/bin/sh/service.c.old	Tue Mar 19 18:57:34 1985
--- /usr/src/bin/sh/service.c	Fri Dec  9 20:19:48 1994
***************
*** 1,5 ****
! #ifndef lint
! static char sccsid[] = "@(#)service.c	4.4 3/19/85";
  #endif
  
  #
--- 1,5 ----
! #if	!defined(lint) && defined(DOSCCS)
! static char sccsid[] = "@(#)service.c	4.4.1 12/9/94";
  #endif
  
  #
***************
*** 12,19 ****
   */
  
  #include	"defs.h"
  
- 
  PROC VOID	gsort();
  
  #define ARGMK	01
--- 12,19 ----
   */
  
  #include	"defs.h"
+ #include	<fcntl.h>
  
  PROC VOID	gsort();
  
  #define ARGMK	01
***************
*** 58,65 ****
  			THEN	fd=chkopen(ion);
  			ELIF flags&rshflg
  			THEN	failed(ion,restricted);
! 			ELIF iof&IOAPP ANDF (fd=open(ion,1))>=0
! 			THEN	lseek(fd, 0L, 2);
  			ELSE	fd=create(ion);
  			FI
  			IF fd>=0
--- 58,65 ----
  			THEN	fd=chkopen(ion);
  			ELIF flags&rshflg
  			THEN	failed(ion,restricted);
! 			ELIF iof&IOAPP ANDF (fd=open(ion,O_WRONLY|O_APPEND))>=0
! 			THEN	;
  			ELSE	fd=create(ion);
  			FI
  			IF fd>=0
***************
*** 91,97 ****
  	REG UFD		f;
  
  	REP path=catpath(path,name);
! 	PER (f=open(curstak(),0))<0 ANDF path DONE
  	return(f);
  }
  
--- 91,97 ----
  	REG UFD		f;
  
  	REP path=catpath(path,name);
! 	PER (f=open(curstak(), O_RDONLY))<0 ANDF path DONE
  	return(f);
  }
  
*** /usr/src/bin/stty.c.old	Sun Dec 25 20:46:38 1988
--- /usr/src/bin/stty.c	Fri Dec  9 20:18:34 1994
***************
*** 4,18 ****
   * specifies the terms and conditions for redistribution.
   */
  
! #ifndef lint
  char copyright[] =
  "@(#) Copyright (c) 1980 Regents of the University of California.\n\
   All rights reserved.\n";
- #endif not lint
  
! #ifndef lint
! static char sccsid[] = "@(#)stty.c	5.4 (Berkeley) 4/4/86";
! #endif not lint
  
  /*
   * set teletype modes
--- 4,16 ----
   * specifies the terms and conditions for redistribution.
   */
  
! #if	!defined(lint) && defined(DOSCCS)
  char copyright[] =
  "@(#) Copyright (c) 1980 Regents of the University of California.\n\
   All rights reserved.\n";
  
! static char sccsid[] = "@(#)stty.c	5.4.1 (2.11BSD GTE) 12/9/94";
! #endif
  
  /*
   * set teletype modes
***************
*** 66,75 ****
  	"nl",		0, CRMOD, 0, 0,
  	"echo",		ECHO, 0, 0, 0,
  	"-echo",	0, ECHO, 0, 0,
- 	"LCASE",	LCASE, 0, 0, 0,
- 	"lcase",	LCASE, 0, 0, 0,
- 	"-LCASE",	0, LCASE, 0, 0,
- 	"-lcase",	0, LCASE, 0, 0,
  	"-tabs",	XTABS, 0, 0, 0,
  	"tabs",		0, XTABS, 0, 0,
  	"tandem",	TANDEM, 0, 0, 0,
--- 64,69 ----
***************
*** 110,117 ****
  	"-crterase",	0, 0, 0, LCRTERA,
  	"crtkill",	0, 0, LCRTKIL, LPRTERA,
  	"-crtkill",	0, 0, 0, LCRTKIL,
- 	"tilde",	0, 0, LTILDE, 0,
- 	"-tilde",	0, 0, 0, LTILDE,
  	"mdmbuf",	0, 0, LMDMBUF, 0,
  	"-mdmbuf",	0, 0, 0, LMDMBUF,
  	"litout",	0, 0, LLITOUT, 0,
--- 104,109 ----
***************
*** 374,381 ****
  		fprintf(stderr,"-nl "+((m&CRMOD)==0));
  	if(all==2 || (m&ECHO)==0)
  		fprintf(stderr,"-echo "+((m&ECHO)!=0));
- 	if(all==2 || (m&LCASE))
- 		fprintf(stderr,"-lcase "+((m&LCASE)!=0));
  	if(all==2 || (m&TANDEM))
  		fprintf(stderr,"-tandem "+((m&TANDEM)!=0));
  	fprintf(stderr,"-tabs "+((m&XTABS)!=XTABS));
--- 366,371 ----
***************
*** 421,427 ****
  			any = 0;
  			nothing = 0;
  		}
- 		lpit(LTILDE, "-tilde ");
  		lpit(LFLUSHO, "-flusho ");
  		lpit(LMDMBUF, "-mdmbuf ");
  		lpit(LLITOUT, "-litout ");
--- 411,416 ----
