install with -s
[unix-history] / usr / src / usr.bin / sccs / sccs.c
index 1a92081..8f3a613 100644 (file)
@@ -1,11 +1,26 @@
+/*
+ * 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>
 
 /*
 # include <pwd.h>
 
 /*
 **             Copyright 1980 Regents of the University of California
 */
 
 **             Copyright 1980 Regents of the University of California
 */
 
-static char SccsId[] = "@(#)sccs.c     1.59 %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
@@ -124,7 +120,7 @@ static char SccsId[] = "@(#)sccs.c  1.59 %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  ****************/
@@ -152,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 */
@@ -198,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
 };
 
@@ -227,6 +228,12 @@ bool       Debug;                  /* turn on tracing */
 # ifndef V6
 extern char    *getenv();
 # endif V6
 # 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;
@@ -239,10 +246,10 @@ main(argc, argv)
 # ifndef SCCSDIR
        register struct passwd *pw;
        extern struct passwd *getpwnam();
 # ifndef SCCSDIR
        register struct passwd *pw;
        extern struct passwd *getpwnam();
-       char buf[100];
+       char buf[FBUFSIZ];
 
        /* pull "SccsDir" out of the environment (possibly) */
 
        /* pull "SccsDir" out of the environment (possibly) */
-       p = getenv("PROJECT");
+       p = getenv("PROJECTDIR");
        if (p != NULL && p[0] != '\0')
        {
                if (p[0] == '/')
        if (p != NULL && p[0] != '\0')
        {
                if (p[0] == '/')
@@ -255,12 +262,12 @@ main(argc, argv)
                                usrerr("user %s does not exist", p);
                                exit(EX_USAGE);
                        }
                                usrerr("user %s does not exist", p);
                                exit(EX_USAGE);
                        }
-                       strcpy(buf, pw->pw_dir);
-                       strcat(buf, "/src");
+                       gstrcpy(buf, pw->pw_dir, sizeof(buf));
+                       gstrcat(buf, "/src", sizeof(buf));
                        if (access(buf, 0) < 0)
                        {
                        if (access(buf, 0) < 0)
                        {
-                               strcpy(buf, pw->pw_dir);
-                               strcat(buf, "/source");
+                               gstrcpy(buf, pw->pw_dir, sizeof(buf));
+                               gstrcat(buf, "/source", sizeof(buf));
                                if (access(buf, 0) < 0)
                                {
                                        usrerr("project %s has no source!", p);
                                if (access(buf, 0) < 0)
                                {
                                        usrerr("project %s has no source!", p);
@@ -300,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
 
@@ -356,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;
@@ -548,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);
@@ -713,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();
@@ -744,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);
@@ -757,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? */
@@ -855,10 +895,10 @@ clean(mode, argv)
        int mode;
        char **argv;
 {
        int mode;
        char **argv;
 {
-       struct direct dir;
-       char buf[100];
+       struct direct *dir;
+       char buf[FBUFSIZ];
        char *bufend;
        char *bufend;
-       register FILE *dirfd;
+       register DIR *dirfd;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
@@ -911,18 +951,18 @@ clean(mode, argv)
        **  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, "/");
+               gstrcat(buf, "/", sizeof(buf));
        if (subdir != NULL)
        {
        if (subdir != NULL)
        {
-               strcat(buf, subdir);
-               strcat(buf, "/");
+               gstrcat(buf, subdir, sizeof(buf));
+               gstrcat(buf, "/", sizeof(buf));
        }
        }
-       strcat(buf, SccsPath);
+       gstrcat(buf, SccsPath, sizeof(buf));
        bufend = &buf[strlen(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);
@@ -936,16 +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(bufend, "/p.");
+               gstrcpy(bufend, "/p.", sizeof(buf));
                basefile = bufend + 3;
                basefile = bufend + 3;
-               strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
-               basefile[sizeof dir.d_name - 2] = '\0';
+               gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
 
                /*
                **  open and scan the p-file.
 
                /*
                **  open and scan the p-file.
@@ -980,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");
@@ -1058,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;
@@ -1068,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 */
@@ -1125,6 +1163,32 @@ unedit(fn)
                }
        }
 
                }
        }
 
+       /*
+        * 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)
        {
@@ -1154,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
@@ -1309,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();
 
@@ -1454,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);
+}