new subnets, interface addressing
[unix-history] / usr / src / usr.bin / sccs / sccs.c
index d2d0ffe..8f3a613 100644 (file)
@@ -1,11 +1,27 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * 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[] = "@(#)sccs.c     5.1 (Berkeley) %G%";
+#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>
 
 /*
 **  SCCS.C -- human-oriented front end to the SCCS system.
 
 /*
 **  SCCS.C -- human-oriented front end to the SCCS system.
 **             Copyright 1980 Regents of the University of California
 */
 
 **             Copyright 1980 Regents of the University of California
 */
 
-static char SccsId[] = "@(#)sccs.c     1.55 %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
-
-# ifdef CORY
-# define PROGPATH(name)        "/usr/eecs/bin/name"
-# endif CORY
-
-/* 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
@@ -123,7 +120,7 @@ static char SccsId[] = "@(#)sccs.c  1.55 %G%";
 # endif NOT MYNAME
 
 # ifndef PROGPATH
 # endif NOT MYNAME
 
 # ifndef PROGPATH
-# define PROGPATH(name)        "/usr/sccs/name"        /* place to find binaries */
+# define PROGPATH(name)        "/usr/local/name"       /* place to find binaries */
 # endif PROGPATH
 
 /****************  End of Configuration Information  ****************/
 # endif PROGPATH
 
 /****************  End of Configuration Information  ****************/
@@ -134,10 +131,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 */
@@ -155,6 +148,7 @@ struct sccsprog
 # 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 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 */
@@ -191,7 +185,7 @@ struct sccsprog SccsProg[] =
        "sccsdiff",     SHELL,  REALUSER,               PROGPATH(sccsdiff),
        "edit",         CMACRO, NO_SDOT,                "get -e",
        "delget",       CMACRO, NO_SDOT,                "delta:mysrp/get:ixbeskcl -t",
        "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",
+       "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,
        "fix",          FIX,    NO_SDOT,                NULL,
        "clean",        CLEAN,  REALUSER|NO_SDOT,       (char *) CLEANC,
        "info",         CLEAN,  REALUSER|NO_SDOT,       (char *) INFOC,
@@ -201,6 +195,10 @@ struct sccsprog SccsProg[] =
        "diffs",        DIFFS,  NO_SDOT|REALUSER,       NULL,
        "-diff",        DODIFF, NO_SDOT|REALUSER,       PROGPATH(bdiff),
        "print",        CMACRO, 0,                      "prt -e/get -p -m -s",
        "diffs",        DIFFS,  NO_SDOT|REALUSER,       NULL,
        "-diff",        DODIFF, NO_SDOT|REALUSER,       PROGPATH(bdiff),
        "print",        CMACRO, 0,                      "prt -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
 };
 
        NULL,           -1,     0,                      NULL
 };
 
@@ -212,6 +210,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 */
@@ -226,6 +225,15 @@ 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
+
+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;
@@ -234,6 +242,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.
@@ -262,10 +307,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
 
@@ -318,7 +367,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;
@@ -510,6 +559,35 @@ command(argv, forkflag, arg0)
                syserr("cannot exec %s", cmd->sccspath);
                exit(EX_OSERR);
 
                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);
@@ -675,7 +753,7 @@ 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 bool isdir();
        extern char *malloc();
        extern char *rindex();
        extern bool isdir();
@@ -706,11 +784,11 @@ makefile(name)
        /* first the directory part */
        if (name[0] != '/')
        {
        /* first the directory part */
        if (name[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 */
        strncat(buf, name, p - name);
        
        /* then the head of the pathname */
        strncat(buf, name, p - name);
@@ -719,11 +797,11 @@ makefile(name)
        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 i haven't changed it, why did I do all this? */
@@ -817,9 +895,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 *dirfd;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
@@ -830,12 +909,15 @@ clean(mode, argv)
        register char **ap;
        extern char *username();
        char *usernm = NULL;
        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 (**ap == '-')
                {
        {
                if (**ap == '-')
                {
@@ -856,18 +938,31 @@ clean(mode, argv)
                                break;
                        }
                }
                                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");
+       dirfd = opendir(buf);
        if (dirfd == NULL)
        {
                usrerr("cannot open %s", buf);
        if (dirfd == NULL)
        {
                usrerr("cannot open %s", buf);
@@ -881,20 +976,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(dirfd)) {
+               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.
@@ -920,9 +1009,8 @@ 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);
                }
@@ -930,14 +1018,14 @@ clean(mode, argv)
                /* the s. file exists and no p. file exists -- unlink the g-file */
                if (mode == CLEANC && !gotpfent)
                {
                /* the s. file exists and no p. file exists -- unlink the g-file */
                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(dirfd);
        if (!gotedit && mode == INFOC)
        {
                printf("Nothing being edited");
        if (!gotedit && mode == INFOC)
        {
                printf("Nothing being edited");
@@ -1008,7 +1096,7 @@ unedit(fn)
        char *fn;
 {
        register FILE *pfp;
        char *fn;
 {
        register FILE *pfp;
-       char *pfn;
+       char *cp, *pfn;
        static char tfn[] = "/tmp/sccsXXXXX";
        FILE *tfp;
        register char *q;
        static char tfn[] = "/tmp/sccsXXXXX";
        FILE *tfp;
        register char *q;
@@ -1018,7 +1106,7 @@ unedit(fn)
        extern char *username();
        struct pfile *pent;
        extern struct pfile *getpfent();
        extern char *username();
        struct pfile *pent;
        extern struct pfile *getpfent();
-       char buf[120];
+       char buf[PFILELG];
        extern char *makefile();
 
        /* make "s." filename & find the trailing component */
        extern char *makefile();
 
        /* make "s." filename & find the trailing component */
@@ -1070,13 +1158,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)
        {
@@ -1106,8 +1218,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
@@ -1261,7 +1378,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();
 
@@ -1273,8 +1390,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);
 }
@@ -1296,6 +1412,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
@@ -1382,7 +1523,53 @@ username()
        return (pw->pw_name);
 # else
        extern char *getlogin();
        return (pw->pw_name);
 # else
        extern char *getlogin();
+       extern char *getenv();
+       register char *p;
 
 
-       return (getlogin());
+       p = getenv("USER");
+       if (p == NULL || p[0] == '\0')
+               p = getlogin();
+       return (p);
 # endif UIDUSER
 }
 # 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);
+}