BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / sccs / sccs.c
index 2466dca..41d6473 100644 (file)
@@ -1,11 +1,41 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that: (1) source distributions retain this entire copyright
+ * notice and comment, and (2) distributions including binaries display
+ * the following acknowledgement:  ``This product includes software
+ * developed by the University of California, Berkeley and its contributors''
+ * in the documentation or other materials provided with the distribution
+ * and in all advertising materials mentioning features or use of this
+ * software. Neither the name of the University nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#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[] = "@(#)sccs.c     5.9 (Berkeley) 6/1/90";
+#endif /* not lint */
+
 # include <stdio.h>
 # include <stdio.h>
-# include <sys/types.h>
+# include <sys/param.h>
 # include <sys/stat.h>
 # include <sys/dir.h>
 # include <errno.h>
 # include <signal.h>
 # include <sysexits.h>
 # include <sys/stat.h>
 # include <sys/dir.h>
 # include <errno.h>
 # include <signal.h>
 # include <sysexits.h>
-# include <whoami.h>
+# include <pwd.h>
+# include "pathnames.h"
 
 /*
 **  SCCS.C -- human-oriented front end to the SCCS system.
 
 /*
 **  SCCS.C -- human-oriented front end to the SCCS system.
 **             get,
 **             delta,
 **             rmdel,
 **             get,
 **             delta,
 **             rmdel,
-**             chghist,
+**             cdc,
 **             etc.            Straight out of SCCS; only difference
 **                             is that pathnames get modified as
 **                             described above.
 **             etc.            Straight out of SCCS; only difference
 **                             is that pathnames get modified as
 **                             described above.
+**             enter           Front end doing "sccs admin -i<name> <name>"
+**             create          Macro for "enter" followed by "get".
 **             edit            Macro for "get -e".
 **             unedit          Removes a file being edited, knowing
 **                             about p-files, etc.
 **             delget          Macro for "delta" followed by "get".
 **             deledit         Macro for "delta" followed by "get -e".
 **             edit            Macro for "get -e".
 **             unedit          Removes a file being edited, knowing
 **                             about p-files, etc.
 **             delget          Macro for "delta" followed by "get".
 **             deledit         Macro for "delta" followed by "get -e".
-**             info            Tell what files being edited.
+**             branch          Macro for "get -b -e", followed by "delta
+**                             -s -n", followd by "get -e -t -g".
+**             diffs           "diff" the specified version of files
+**                             and the checked-out version.
+**             print           Macro for "prs -e" followed by "get -p -m".
+**             tell            List what files are being edited.
+**             info            Print information about files being edited.
 **             clean           Remove all files that can be
 **                             regenerated from SCCS files.
 **             check           Like info, but return exit status, for
 **             clean           Remove all files that can be
 **                             regenerated from SCCS files.
 **             check           Like info, but return exit status, for
 **             Copyright 1980 Regents of the University of California
 */
 
 **             Copyright 1980 Regents of the University of California
 */
 
-static char SccsId[] = "@(#)sccs.c     1.48 %G%";
 \f
 /*******************  Configuration Information  ********************/
 
 \f
 /*******************  Configuration Information  ********************/
 
-/* special defines for local berkeley systems */
-# include <whoami.h>
-
-# ifdef CSVAX
-# define UIDUSER
-# define PROGPATH(name)        "/usr/local/name"
-# endif CSVAX
-
-# ifdef INGVAX
-# define PROGPATH(name)        "/usr/local/name"
-# endif INGVAX
-
-/* end of berkeley systems defines */
-
 # ifndef SCCSPATH
 # define SCCSPATH      "SCCS"  /* pathname in which to find s-files */
 # endif NOT SCCSPATH
 # ifndef SCCSPATH
 # define SCCSPATH      "SCCS"  /* pathname in which to find s-files */
 # endif NOT SCCSPATH
@@ -118,10 +141,6 @@ static char SccsId[] = "@(#)sccs.c 1.48 %G%";
 # define MYNAME                "sccs"  /* name used for printing errors */
 # endif NOT MYNAME
 
 # define MYNAME                "sccs"  /* name used for printing errors */
 # endif NOT MYNAME
 
-# ifndef PROGPATH
-# define PROGPATH(name)        "/usr/sccs/name"        /* place to find binaries */
-# endif PROGPATH
-
 /****************  End of Configuration Information  ****************/
 \f
 typedef char   bool;
 /****************  End of Configuration Information  ****************/
 \f
 typedef char   bool;
@@ -130,10 +149,6 @@ typedef char       bool;
 
 # define bitset(bit, word)     ((bool) ((bit) & (word)))
 
 
 # define bitset(bit, word)     ((bool) ((bit) & (word)))
 
-# ifdef UIDUSER
-# include <pwd.h>
-# endif UIDUSER
-
 struct sccsprog
 {
        char    *sccsname;      /* name of SCCS routine */
 struct sccsprog
 {
        char    *sccsname;      /* name of SCCS routine */
@@ -150,6 +165,8 @@ struct sccsprog
 # define UNEDIT                4       /* unedit a file */
 # define SHELL         5       /* call a shell file (like PROG) */
 # define DIFFS         6       /* diff between sccs & file out */
 # define UNEDIT                4       /* unedit a file */
 # define SHELL         5       /* call a shell file (like PROG) */
 # define DIFFS         6       /* diff between sccs & file out */
+# define DODIFF                7       /* internal call to diff program */
+# define ENTER         8       /* enter new files */
 
 /* bits for sccsflags */
 # define NO_SDOT       0001    /* no s. on front of args */
 
 /* bits for sccsflags */
 # define NO_SDOT       0001    /* no s. on front of args */
@@ -172,27 +189,35 @@ struct sccsprog
 
 struct sccsprog SccsProg[] =
 {
 
 struct sccsprog SccsProg[] =
 {
-       "admin",        PROG,   REALUSER,       PROGPATH(admin),
-       "chghist",      PROG,   0,              PROGPATH(rmdel),
-       "comb",         PROG,   0,              PROGPATH(comb),
-       "delta",        PROG,   0,              PROGPATH(delta),
-       "get",          PROG,   0,              PROGPATH(get),
-       "help",         PROG,   NO_SDOT,        PROGPATH(help),
-       "prt",          PROG,   0,              PROGPATH(prt),
-       "rmdel",        PROG,   REALUSER,       PROGPATH(rmdel),
-       "what",         PROG,   NO_SDOT,        PROGPATH(what),
-       "sccsdiff",     SHELL,  REALUSER,       PROGPATH(sccsdiff),
-       "edit",         CMACRO, NO_SDOT,        "get -e",
-       "delget",       CMACRO, NO_SDOT,        "delta:mysrp/get:ixbeskcl -t",
-       "deledit",      CMACRO, NO_SDOT,        "delta:mysrp/get:ixbskcl -e -t",
-       "fix",          FIX,    NO_SDOT,        NULL,
-       "clean",        CLEAN,  REALUSER,       (char *) CLEANC,
-       "info",         CLEAN,  REALUSER,       (char *) INFOC,
-       "check",        CLEAN,  REALUSER,       (char *) CHECKC,
-       "tell",         CLEAN,  REALUSER,       (char *) TELLC,
-       "unedit",       UNEDIT, NO_SDOT,        NULL,
-       "diffs",        DIFFS,  NO_SDOT|REALUSER, NULL,
-       NULL,           -1,     0,              NULL
+       "admin",        PROG,   REALUSER,               PROGPATH(admin),
+       "cdc",          PROG,   0,                      PROGPATH(rmdel),
+       "comb",         PROG,   0,                      PROGPATH(comb),
+       "delta",        PROG,   0,                      PROGPATH(delta),
+       "get",          PROG,   0,                      PROGPATH(get),
+       "help",         PROG,   NO_SDOT,                PROGPATH(help),
+       "prs",          PROG,   0,                      PROGPATH(prs),
+       "prt",          PROG,   0,                      PROGPATH(prt),
+       "rmdel",        PROG,   REALUSER,               PROGPATH(rmdel),
+       "val",          PROG,   0,                      PROGPATH(val),
+       "what",         PROG,   NO_SDOT,                PROGPATH(what),
+       "sccsdiff",     SHELL,  REALUSER,               PROGPATH(sccsdiff),
+       "edit",         CMACRO, NO_SDOT,                "get -e",
+       "delget",       CMACRO, NO_SDOT,                "delta:mysrp/get:ixbeskcl -t",
+       "deledit",      CMACRO, NO_SDOT,                "delta:mysrp -n/get:ixbskcl -e -t -g",
+       "fix",          FIX,    NO_SDOT,                NULL,
+       "clean",        CLEAN,  REALUSER|NO_SDOT,       (char *) CLEANC,
+       "info",         CLEAN,  REALUSER|NO_SDOT,       (char *) INFOC,
+       "check",        CLEAN,  REALUSER|NO_SDOT,       (char *) CHECKC,
+       "tell",         CLEAN,  REALUSER|NO_SDOT,       (char *) TELLC,
+       "unedit",       UNEDIT, NO_SDOT,                NULL,
+       "diffs",        DIFFS,  NO_SDOT|REALUSER,       NULL,
+       "-diff",        DODIFF, NO_SDOT|REALUSER,       PROGPATH(bdiff),
+       "print",        CMACRO, 0,                      "prs -e/get -p -m -s",
+       "branch",       CMACRO, NO_SDOT,
+               "get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
+       "enter",        ENTER,  NO_SDOT,                NULL,
+       "create",       CMACRO, NO_SDOT,                "enter/get:ixbeskcl -t",
+       NULL,           -1,     0,                      NULL
 };
 
 /* one line from a p-file */
 };
 
 /* one line from a p-file */
@@ -203,6 +228,7 @@ struct pfile
        char    *p_user;        /* user who did edit */
        char    *p_date;        /* date of get */
        char    *p_time;        /* time of get */
        char    *p_user;        /* user who did edit */
        char    *p_date;        /* date of get */
        char    *p_time;        /* time of get */
+       char    *p_aux;         /* extra info at end */
 };
 
 char   *SccsPath = SCCSPATH;   /* pathname of SCCS files */
 };
 
 char   *SccsPath = SCCSPATH;   /* pathname of SCCS files */
@@ -217,6 +243,17 @@ bool       RealUser;               /* if set, running as real user */
 # ifdef DEBUG
 bool   Debug;                  /* turn on tracing */
 # endif
 # ifdef DEBUG
 bool   Debug;                  /* turn on tracing */
 # endif
+# ifndef V6
+extern char    *getenv();
+# endif V6
+
+extern char    *sys_siglist[];
+
+char *gstrcat(), *strcat();
+char *gstrncat(), *strncat();
+char *gstrcpy(), *strcpy();
+#define        FBUFSIZ BUFSIZ
+#define        PFILELG 120
 \f
 main(argc, argv)
        int argc;
 \f
 main(argc, argv)
        int argc;
@@ -225,6 +262,43 @@ main(argc, argv)
        register char *p;
        extern struct sccsprog *lookup();
        register int i;
        register char *p;
        extern struct sccsprog *lookup();
        register int i;
+# ifndef V6
+# ifndef SCCSDIR
+       register struct passwd *pw;
+       extern struct passwd *getpwnam();
+       char buf[FBUFSIZ];
+
+       /* pull "SccsDir" out of the environment (possibly) */
+       p = getenv("PROJECTDIR");
+       if (p != NULL && p[0] != '\0')
+       {
+               if (p[0] == '/')
+                       SccsDir = p;
+               else
+               {
+                       pw = getpwnam(p);
+                       if (pw == NULL)
+                       {
+                               usrerr("user %s does not exist", p);
+                               exit(EX_USAGE);
+                       }
+                       gstrcpy(buf, pw->pw_dir, sizeof(buf));
+                       gstrcat(buf, "/src", sizeof(buf));
+                       if (access(buf, 0) < 0)
+                       {
+                               gstrcpy(buf, pw->pw_dir, sizeof(buf));
+                               gstrcat(buf, "/source", sizeof(buf));
+                               if (access(buf, 0) < 0)
+                               {
+                                       usrerr("project %s has no source!", p);
+                                       exit(EX_USAGE);
+                               }
+                       }
+                       SccsDir = buf;
+               }
+       }
+# endif SCCSDIR
+# endif V6
 
        /*
        **  Detect and decode flags intended for this program.
 
        /*
        **  Detect and decode flags intended for this program.
@@ -253,10 +327,14 @@ main(argc, argv)
 # ifndef SCCSDIR
                          case 'p':             /* path of sccs files */
                                SccsPath = ++p;
 # ifndef SCCSDIR
                          case 'p':             /* path of sccs files */
                                SccsPath = ++p;
+                               if (SccsPath[0] == '\0' && argv[1] != NULL)
+                                       SccsPath = *++argv;
                                break;
 
                          case 'd':             /* directory to search from */
                                SccsDir = ++p;
                                break;
 
                          case 'd':             /* directory to search from */
                                SccsDir = ++p;
+                               if (SccsDir[0] == '\0' && argv[1] != NULL)
+                                       SccsDir = *++argv;
                                break;
 # endif
 
                                break;
 # endif
 
@@ -309,7 +387,7 @@ command(argv, forkflag, arg0)
 {
        register struct sccsprog *cmd;
        register char *p;
 {
        register struct sccsprog *cmd;
        register char *p;
-       char buf[40];
+       char buf[FBUFSIZ];
        extern struct sccsprog *lookup();
        char *nav[1000];
        char **np;
        extern struct sccsprog *lookup();
        char *nav[1000];
        char **np;
@@ -403,7 +481,7 @@ command(argv, forkflag, arg0)
          case SHELL:           /* call a shell file */
                *ap = cmd->sccspath;
                *--ap = "sh";
          case SHELL:           /* call a shell file */
                *ap = cmd->sccspath;
                *--ap = "sh";
-               rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
+               rval = callprog(_PATH_BSHELL, cmd->sccsflags, ap, forkflag);
                break;
 
          case PROG:            /* call an sccs prog */
                break;
 
          case PROG:            /* call an sccs prog */
@@ -424,7 +502,7 @@ command(argv, forkflag, arg0)
                break;
 
          case FIX:             /* fix a delta */
                break;
 
          case FIX:             /* fix a delta */
-               if (strncmp(ap[1], "-r", 2) != 0)
+               if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
                {
                        usrerr("-r flag needed for fix command");
                        rval = EX_USAGE;
                {
                        usrerr("-r flag needed for fix command");
                        rval = EX_USAGE;
@@ -480,6 +558,56 @@ command(argv, forkflag, arg0)
                }
                break;
 
                }
                break;
 
+         case DODIFF:          /* internal diff call */
+               setuid(getuid());
+               for (np = ap; *np != NULL; np++)
+               {
+                       if ((*np)[0] == '-' && (*np)[1] == 'C')
+                               (*np)[1] = 'c';
+               }
+
+               /* insert "-" argument */
+               np[1] = NULL;
+               np[0] = np[-1];
+               np[-1] = "-";
+
+               /* execute the diff program of choice */
+# ifndef V6
+               execvp("diff", ap);
+# endif
+               execv(cmd->sccspath, argv);
+               syserr("cannot exec %s", cmd->sccspath);
+               exit(EX_OSERR);
+
+         case ENTER:           /* enter new sccs files */
+               /* skip over flag arguments */
+               for (np = &ap[1]; *np != NULL && **np == '-'; np++)
+                       continue;
+               argv = np;
+
+               /* do an admin for each file */
+               p = argv[1];
+               while (*np != NULL)
+               {
+                       printf("\n%s:\n", *np);
+                       strcpy(buf, "-i");
+                       gstrcat(buf, *np, sizeof(buf));
+                       ap[0] = buf;
+                       argv[0] = tail(*np);
+                       argv[1] = NULL;
+                       rval = command(ap, TRUE, "admin");
+                       argv[1] = p;
+                       if (rval == 0)
+                       {
+                               strcpy(buf, ",");
+                               gstrcat(buf, tail(*np), sizeof(buf));
+                               if (link(*np, buf) >= 0)
+                                       unlink(*np);
+                       }
+                       np++;
+               }
+               break;
+
          default:
                syserr("oper %d", cmd->sccsoper);
                exit(EX_SOFTWARE);
          default:
                syserr("oper %d", cmd->sccsoper);
                exit(EX_SOFTWARE);
@@ -546,7 +674,12 @@ callprog(progpath, flags, argv, forkflag)
        bool forkflag;
 {
        register int i;
        bool forkflag;
 {
        register int i;
+       register int wpid;
        auto int st;
        auto int st;
+       register int sigcode;
+       register int coredumped;
+       register char *sigmsg;
+       auto char sigmsgbuf[10+1];      /* "Signal 127" + terminating '\0' */
 
 # ifdef DEBUG
        if (Debug)
 
 # ifdef DEBUG
        if (Debug)
@@ -578,9 +711,30 @@ callprog(progpath, flags, argv, forkflag)
                }
                else if (i > 0)
                {
                }
                else if (i > 0)
                {
-                       wait(&st);
-                       if ((st & 0377) == 0)
+                       while ((wpid = wait(&st)) != -1 && wpid != i)
+                               ;
+                       if ((sigcode = st & 0377) == 0)
                                st = (st >> 8) & 0377;
                                st = (st >> 8) & 0377;
+                       else
+                       {
+                               coredumped = sigcode & 0200;
+                               sigcode &= 0177;
+                               if (sigcode != SIGINT && sigcode != SIGPIPE)
+                               {
+                                       if (sigcode < NSIG)
+                                               sigmsg = sys_siglist[sigcode];
+                                       else
+                                       {
+                                               sprintf(sigmsgbuf, "Signal %d",
+                                                   sigcode);
+                                               sigmsg = sigmsgbuf;
+                                       }
+                                       fprintf(stderr, "sccs: %s: %s%s", argv[0],
+                                           sigmsg,
+                                           coredumped ? " - core dumped": "");
+                               }
+                               st = EX_SOFTWARE;
+                       }
                        if (OutFile >= 0)
                        {
                                close(OutFile);
                        if (OutFile >= 0)
                        {
                                close(OutFile);
@@ -645,9 +799,10 @@ makefile(name)
        char *name;
 {
        register char *p;
        char *name;
 {
        register char *p;
-       char buf[512];
+       char buf[3*FBUFSIZ];
        extern char *malloc();
        extern char *rindex();
        extern char *malloc();
        extern char *rindex();
+       extern bool safepath();
        extern bool isdir();
        register char *q;
 
        extern bool isdir();
        register char *q;
 
@@ -658,51 +813,65 @@ makefile(name)
                p++;
 
        /*
                p++;
 
        /*
-       **  See if the name can be used as-is.
+       **  Check to see that the path is "safe", i.e., that we
+       **  are not letting some nasty person use the setuid part
+       **  of this program to look at or munge some presumably
+       **  hidden files.
        */
 
        */
 
-       if (SccsPath[0] != '/' || name[0] == '/' || strncmp(name, "./", 2) == 0)
-       {
-               if (strncmp(p, "s.", 2) == 0)
-                       return (name);
-               if (isdir(name))
-                       return (name);
-       }
+       if (SccsDir[0] == '/' && !safepath(name))
+               return (NULL);
 
        /*
 
        /*
-       **  Create the actual pathname.
+       **  Create the base pathname.
        */
 
        /* first the directory part */
        */
 
        /* first the directory part */
-       if (name[0] != '/')
+       if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
        {
        {
-               strcpy(buf, SccsDir);
-               strcat(buf, "/");
+               gstrcpy(buf, SccsDir, sizeof(buf));
+               gstrcat(buf, "/", sizeof(buf));
        }
        else
        }
        else
-               strcpy(buf, "");
+               gstrcpy(buf, "", sizeof(buf));
        
        /* then the head of the pathname */
        
        /* then the head of the pathname */
-       strncat(buf, name, p - name);
+       gstrncat(buf, name, p - name, sizeof(buf));
        q = &buf[strlen(buf)];
        q = &buf[strlen(buf)];
-       strcpy(q, p);
+
+       /* now copy the final part of the name, in case useful */
+       gstrcpy(q, p, sizeof(buf));
+
+       /* so is it useful? */
        if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
        {
                /* sorry, no; copy the SCCS pathname & the "s." */
        if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
        {
                /* sorry, no; copy the SCCS pathname & the "s." */
-               strcpy(q, SccsPath);
-               strcat(buf, "/s.");
+               gstrcpy(q, SccsPath, sizeof(buf));
+               gstrcat(buf, "/s.", sizeof(buf));
 
                /* and now the end of the name */
 
                /* and now the end of the name */
-               strcat(buf, p);
+               gstrcat(buf, p, sizeof(buf));
        }
 
        /* if i haven't changed it, why did I do all this? */
        if (strcmp(buf, name) == 0)
                p = name;
        }
 
        /* if i haven't changed it, why did I do all this? */
        if (strcmp(buf, name) == 0)
                p = name;
+       else
+       {
+               /* but if I have, squirrel it away */
+               p = malloc(strlen(buf) + 1);
+               if (p == NULL)
+               {
+                       perror("Sccs: no mem");
+                       exit(EX_OSERR);
+               }
+               strcpy(p, buf);
+       }
 
 
-       return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
+       return (p);
 }
 }
-\f/*
+\f
+/*
 **  ISDIR -- return true if the argument is a directory.
 **
 **     Parameters:
 **  ISDIR -- return true if the argument is a directory.
 **
 **     Parameters:
@@ -787,9 +956,10 @@ clean(mode, argv)
        int mode;
        char **argv;
 {
        int mode;
        char **argv;
 {
-       struct direct dir;
-       char buf[100];
-       register FILE *dirfd;
+       struct direct *dir;
+       char buf[FBUFSIZ];
+       char *bufend;
+       register DIR *dirp;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
@@ -798,28 +968,63 @@ clean(mode, argv)
        extern struct pfile *getpfent();
        register struct pfile *pf;
        register char **ap;
        extern struct pfile *getpfent();
        register struct pfile *pf;
        register char **ap;
+       extern char *username();
+       char *usernm = NULL;
+       char *subdir = NULL;
+       char *cmdname;
 
        /*
        **  Process the argv
        */
 
 
        /*
        **  Process the argv
        */
 
-       for (ap = argv; *ap != NULL; ap++)
+       cmdname = *argv;
+       for (ap = argv; *++ap != NULL; )
        {
        {
-               if (strcmp(*ap, "-b") == 0)
-                       nobranch = TRUE;
+               if (**ap == '-')
+               {
+                       /* we have a flag */
+                       switch ((*ap)[1])
+                       {
+                         case 'b':
+                               nobranch = TRUE;
+                               break;
+
+                         case 'u':
+                               if ((*ap)[2] != '\0')
+                                       usernm = &(*ap)[2];
+                               else if (ap[1] != NULL && ap[1][0] != '-')
+                                       usernm = *++ap;
+                               else
+                                       usernm = username();
+                               break;
+                       }
+               }
+               else
+               {
+                       if (subdir != NULL)
+                               usrerr("too many args");
+                       else
+                               subdir = *ap;
+               }
        }
 
        /*
        **  Find and open the SCCS directory.
        */
 
        }
 
        /*
        **  Find and open the SCCS directory.
        */
 
-       strcpy(buf, SccsDir);
+       gstrcpy(buf, SccsDir, sizeof(buf));
        if (buf[0] != '\0')
        if (buf[0] != '\0')
-               strcat(buf, "/");
-       strcat(buf, SccsPath);
+               gstrcat(buf, "/", sizeof(buf));
+       if (subdir != NULL)
+       {
+               gstrcat(buf, subdir, sizeof(buf));
+               gstrcat(buf, "/", sizeof(buf));
+       }
+       gstrcat(buf, SccsPath, sizeof(buf));
+       bufend = &buf[strlen(buf)];
 
 
-       dirfd = fopen(buf, "r");
-       if (dirfd == NULL)
+       dirp = opendir(buf);
+       if (dirp == NULL)
        {
                usrerr("cannot open %s", buf);
                return (EX_NOINPUT);
        {
                usrerr("cannot open %s", buf);
                return (EX_NOINPUT);
@@ -832,20 +1037,14 @@ clean(mode, argv)
        */
 
        gotedit = FALSE;
        */
 
        gotedit = FALSE;
-       while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL)
-       {
-               if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0)
+       while (dir = readdir(dirp)) {
+               if (strncmp(dir->d_name, "s.", 2) != 0)
                        continue;
                
                /* got an s. file -- see if the p. file exists */
                        continue;
                
                /* got an s. file -- see if the p. file exists */
-               strcpy(buf, SccsDir);
-               if (buf[0] != '\0')
-                       strcat(buf, "/");
-               strcat(buf, SccsPath);
-               strcat(buf, "/p.");
-               basefile = &buf[strlen(buf)];
-               strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
-               basefile[sizeof dir.d_name - 2] = '\0';
+               gstrcpy(bufend, "/p.", sizeof(buf));
+               basefile = bufend + 3;
+               gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
 
                /*
                **  open and scan the p-file.
 
                /*
                **  open and scan the p-file.
@@ -862,6 +1061,8 @@ clean(mode, argv)
                        {
                                if (nobranch && isbranch(pf->p_nsid))
                                        continue;
                        {
                                if (nobranch && isbranch(pf->p_nsid))
                                        continue;
+                               if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
+                                       continue;
                                gotedit = TRUE;
                                gotpfent = TRUE;
                                if (mode == TELLC)
                                gotedit = TRUE;
                                gotpfent = TRUE;
                                if (mode == TELLC)
@@ -869,28 +1070,33 @@ clean(mode, argv)
                                        printf("%s\n", basefile);
                                        break;
                                }
                                        printf("%s\n", basefile);
                                        break;
                                }
-                               printf("%12s: being edited: %s %s %s %s %s\n",
-                                      basefile, pf->p_osid, pf->p_nsid,
-                                      pf->p_user, pf->p_date, pf->p_time);
+                               printf("%12s: being edited: ", basefile);
+                               putpfent(pf, stdout);
                        }
                        fclose(pfp);
                }
                        }
                        fclose(pfp);
                }
-               if (!gotpfent)
-                       continue;
                
                /* the s. file exists and no p. file exists -- unlink the g-file */
                
                /* the s. file exists and no p. file exists -- unlink the g-file */
-               if (mode == CLEANC)
+               if (mode == CLEANC && !gotpfent)
                {
                {
-                       strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2);
-                       buf[sizeof dir.d_name - 2] = '\0';
-                       unlink(buf);
+                       char    unlinkbuf[FBUFSIZ];
+                       gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
+                       unlink(unlinkbuf);
                }
        }
 
        /* cleanup & report results */
                }
        }
 
        /* cleanup & report results */
-       fclose(dirfd);
+       closedir(dirp);
        if (!gotedit && mode == INFOC)
        if (!gotedit && mode == INFOC)
-               printf("Nothing being edited\n");
+       {
+               printf("Nothing being edited");
+               if (nobranch)
+                       printf(" (on trunk)");
+               if (usernm == NULL)
+                       printf("\n");
+               else
+                       printf(" by %s\n", usernm);
+       }
        if (mode == CHECKC)
                exit(gotedit);
        return (EX_OK);
        if (mode == CHECKC)
                exit(gotedit);
        return (EX_OK);
@@ -951,22 +1157,18 @@ unedit(fn)
        char *fn;
 {
        register FILE *pfp;
        char *fn;
 {
        register FILE *pfp;
-       char *pfn;
-       static char tfn[] = "/tmp/sccsXXXXX";
+       char *cp, *pfn;
+       static char tfn[] = _PATH_TMP;
        FILE *tfp;
        register char *q;
        bool delete = FALSE;
        bool others = FALSE;
        char *myname;
        FILE *tfp;
        register char *q;
        bool delete = FALSE;
        bool others = FALSE;
        char *myname;
-       extern char *getlogin();
+       extern char *username();
        struct pfile *pent;
        extern struct pfile *getpfent();
        struct pfile *pent;
        extern struct pfile *getpfent();
-       char buf[120];
-       extern char *makefile();
-# ifdef UIDUSER
-       struct passwd *pw;
-       extern struct passwd *getpwuid();
-# endif UIDUSER
+       char buf[PFILELG];
+       extern char *makefile(), *rindex(), *tail();
 
        /* make "s." filename & find the trailing component */
        pfn = makefile(fn);
 
        /* make "s." filename & find the trailing component */
        pfn = makefile(fn);
@@ -1001,17 +1203,7 @@ unedit(fn)
        }
 
        /* figure out who I am */
        }
 
        /* figure out who I am */
-# ifdef UIDUSER
-       pw = getpwuid(getuid());
-       if (pw == NULL)
-       {
-               syserr("who are you? (uid=%d)", getuid());
-               exit(EX_OSERR);
-       }
-       myname = pw->pw_name;
-# else
-       myname = getlogin();
-# endif UIDUSER
+       myname = username();
 
        /*
        **  Copy p-file to temp file, doing deletions as needed.
 
        /*
        **  Copy p-file to temp file, doing deletions as needed.
@@ -1027,13 +1219,37 @@ unedit(fn)
                else
                {
                        /* output it again */
                else
                {
                        /* output it again */
-                       fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid,
-                           pent->p_nsid, pent->p_user, pent->p_date,
-                           pent->p_time);
+                       putpfent(pent, tfp);
                        others++;
                }
        }
 
                        others++;
                }
        }
 
+       /*
+        * Before changing anything, make sure we can remove
+        * the file in question (assuming it exists).
+        */
+       if (delete) {
+               extern int errno;
+
+               cp = tail(fn);
+               errno = 0;
+               if (access(cp, 0) < 0 && errno != ENOENT)
+                       goto bad;
+               if (errno == 0)
+                       /*
+                        * This is wrong, but the rest of the program
+                        * has built in assumptions about "." as well,
+                        * so why make unedit a special case?
+                        */
+                       if (access(".", 2) < 0) {
+       bad:
+                               printf("%12s: can't remove\n", cp);
+                               fclose(tfp);
+                               fclose(pfp);
+                               unlink(tfn);
+                               return (FALSE);
+                       }
+       }
        /* do final cleanup */
        if (others)
        {
        /* do final cleanup */
        if (others)
        {
@@ -1063,8 +1279,13 @@ unedit(fn)
        /* actually remove the g-file */
        if (delete)
        {
        /* actually remove the g-file */
        if (delete)
        {
-               unlink(tail(fn));
-               printf("%12s: removed\n", tail(fn));
+               /*
+                * Since we've checked above, we can
+                * use the return from unlink to
+                * determine if the file existed or not.
+                */
+               if (unlink(cp) >= 0)
+                       printf("%12s: removed\n", cp);
                return (TRUE);
        }
        else
                return (TRUE);
        }
        else
@@ -1098,9 +1319,10 @@ dodiff(getv, gfile)
        register int pid;
        auto int st;
        extern int errno;
        register int pid;
        auto int st;
        extern int errno;
-       int (*osig)();
+       sig_t osig;
 
        printf("\n------- %s -------\n", gfile);
 
        printf("\n------- %s -------\n", gfile);
+       fflush(stdout);
 
        /* create context for diff to run in */
        if (pipe(pipev) < 0)
 
        /* create context for diff to run in */
        if (pipe(pipev) < 0)
@@ -1118,7 +1340,7 @@ dodiff(getv, gfile)
                /* in parent; run get */
                OutFile = pipev[1];
                close(pipev[0]);
                /* in parent; run get */
                OutFile = pipev[1];
                close(pipev[0]);
-               rval = command(&getv[1], TRUE, "get -s -k -p");
+               rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
                osig = signal(SIGINT, SIG_IGN);
                while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
                        errno = 0;
                osig = signal(SIGINT, SIG_IGN);
                while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
                        errno = 0;
@@ -1134,13 +1356,7 @@ dodiff(getv, gfile)
                        syserr("dodiff: magic failed");
                        exit(EX_OSERR);
                }
                        syserr("dodiff: magic failed");
                        exit(EX_OSERR);
                }
-               execl(PROGPATH(bdiff), "bdiff", "-", gfile, NULL);
-# ifndef V6
-               execlp("bdiff", "bdiff", "-", gfile, NULL);
-               execlp("diff", "diff", "-", gfile, NULL);
-# endif NOT V6
-               syserr("bdiff: cannot execute");
-               exit(EX_OSERR);
+               command(&getv[1], FALSE, "-diff:elsfhbC");
        }
        return (rval);
 }
        }
        return (rval);
 }
@@ -1190,7 +1406,7 @@ getpfent(pfp)
        FILE *pfp;
 {
        static struct pfile ent;
        FILE *pfp;
 {
        static struct pfile ent;
-       static char buf[120];
+       static char buf[PFILELG];
        register char *p;
        extern char *nextfield();
 
        register char *p;
        extern char *nextfield();
 
@@ -1202,8 +1418,7 @@ getpfent(pfp)
        ent.p_user = p = nextfield(p);
        ent.p_date = p = nextfield(p);
        ent.p_time = p = nextfield(p);
        ent.p_user = p = nextfield(p);
        ent.p_date = p = nextfield(p);
        ent.p_time = p = nextfield(p);
-       if (p == NULL || nextfield(p) != NULL)
-               return (NULL);
+       ent.p_aux = p = nextfield(p);
 
        return (&ent);
 }
 
        return (&ent);
 }
@@ -1225,6 +1440,31 @@ nextfield(p)
        *p++ = '\0';
        return (p);
 }
        *p++ = '\0';
        return (p);
 }
+\f/*
+**  PUTPFENT -- output a p-file entry to a file
+**
+**     Parameters:
+**             pf -- the p-file entry
+**             f -- the file to put it on.
+**
+**     Returns:
+**             none.
+**
+**     Side Effects:
+**             pf is written onto file f.
+*/
+
+putpfent(pf, f)
+       register struct pfile *pf;
+       register FILE *f;
+{
+       fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
+               pf->p_user, pf->p_date, pf->p_time);
+       if (pf->p_aux != NULL)
+               fprintf(f, " %s", pf->p_aux);
+       else
+               fprintf(f, "\n");
+}
 \f
 /*
 **  USRERR -- issue user-level error
 \f
 /*
 **  USRERR -- issue user-level error
@@ -1282,3 +1522,81 @@ syserr(f, p1, p2, p3)
                exit(EX_OSERR);
        }
 }
                exit(EX_OSERR);
        }
 }
+\f/*
+**  USERNAME -- return name of the current user
+**
+**     Parameters:
+**             none
+**
+**     Returns:
+**             name of current user
+**
+**     Side Effects:
+**             none
+*/
+
+char *
+username()
+{
+# ifdef UIDUSER
+       extern struct passwd *getpwuid();
+       register struct passwd *pw;
+
+       pw = getpwuid(getuid());
+       if (pw == NULL)
+       {
+               syserr("who are you? (uid=%d)", getuid());
+               exit(EX_OSERR);
+       }
+       return (pw->pw_name);
+# else
+       extern char *getlogin();
+       register char *p;
+
+       p = getenv("USER");
+       if (p == NULL || p[0] == '\0')
+               p = getlogin();
+       return (p);
+# endif UIDUSER
+}
+\f
+/*
+**     Guarded string manipulation routines; the last argument
+**     is the length of the buffer into which the strcpy or strcat
+**     is to be done.
+*/
+char *gstrcat(to, from, length)
+       char    *to, *from;
+       int     length;
+{
+       if (strlen(from) + strlen(to) >= length) {
+               gstrbotch(to, from);
+       }
+       return(strcat(to, from));
+}
+
+char *gstrncat(to, from, n, length)
+       char    *to, *from;
+       int     n;
+       int     length;
+{
+       if (n + strlen(to) >= length) {
+               gstrbotch(to, from);
+       }
+       return(strncat(to, from, n));
+}
+
+char *gstrcpy(to, from, length)
+       char    *to, *from;
+       int     length;
+{
+       if (strlen(from) >= length) {
+               gstrbotch(from, (char *)0);
+       }
+       return(strcpy(to, from));
+}
+gstrbotch(str1, str2)
+       char    *str1, *str2;
+{
+       usrerr("Filename(s) too long: %s %s", str1, str2);
+}