install with -s
[unix-history] / usr / src / usr.bin / sccs / sccs.c
index 001f69f..8f3a613 100644 (file)
@@ -1,7 +1,23 @@
+/*
+ * 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 <sys/param.h>
 # include <sys/stat.h>
 # include <stdio.h>
 # include <sys/param.h>
 # include <sys/stat.h>
-# include <dir.h>
+# include <sys/dir.h>
 # include <errno.h>
 # include <signal.h>
 # include <sysexits.h>
 # include <errno.h>
 # include <signal.h>
 # include <sysexits.h>
 **             Copyright 1980 Regents of the University of California
 */
 
 **             Copyright 1980 Regents of the University of California
 */
 
-static char SccsId[] = "@(#)sccs.c     1.67 %G%";
 \f
 /*******************  Configuration Information  ********************/
 
 \f
 /*******************  Configuration Information  ********************/
 
@@ -213,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;
@@ -225,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] == '/')
@@ -241,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);
@@ -346,7 +367,7 @@ command(argv, forkflag, arg0)
 {
        register struct sccsprog *cmd;
        register char *p;
 {
        register struct sccsprog *cmd;
        register char *p;
-       char buf[100];
+       char buf[FBUFSIZ];
        extern struct sccsprog *lookup();
        char *nav[1000];
        char **np;
        extern struct sccsprog *lookup();
        char *nav[1000];
        char **np;
@@ -549,7 +570,8 @@ command(argv, forkflag, arg0)
                while (*np != NULL)
                {
                        printf("\n%s:\n", *np);
                while (*np != NULL)
                {
                        printf("\n%s:\n", *np);
-                       sprintf(buf, "-i%s", *np);
+                       strcpy(buf, "-i");
+                       gstrcat(buf, *np, sizeof(buf));
                        ap[0] = buf;
                        argv[0] = tail(*np);
                        argv[1] = NULL;
                        ap[0] = buf;
                        argv[0] = tail(*np);
                        argv[1] = NULL;
@@ -557,7 +579,8 @@ command(argv, forkflag, arg0)
                        argv[1] = p;
                        if (rval == 0)
                        {
                        argv[1] = p;
                        if (rval == 0)
                        {
-                               sprintf(buf, ",%s", tail(*np));
+                               strcpy(buf, ",");
+                               gstrcat(buf, tail(*np), sizeof(buf));
                                if (link(*np, buf) >= 0)
                                        unlink(*np);
                        }
                                if (link(*np, buf) >= 0)
                                        unlink(*np);
                        }
@@ -730,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();
@@ -761,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);
@@ -774,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? */
@@ -873,7 +896,7 @@ clean(mode, argv)
        char **argv;
 {
        struct direct *dir;
        char **argv;
 {
        struct direct *dir;
-       char buf[100];
+       char buf[FBUFSIZ];
        char *bufend;
        register DIR *dirfd;
        register char *basefile;
        char *bufend;
        register DIR *dirfd;
        register char *basefile;
@@ -928,15 +951,15 @@ 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)];
 
        dirfd = opendir(buf);
        bufend = &buf[strlen(buf)];
 
        dirfd = opendir(buf);
@@ -958,9 +981,9 @@ clean(mode, argv)
                        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;
-               strcpy(basefile, &dir->d_name[2]);
+               gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
 
                /*
                **  open and scan the p-file.
 
                /*
                **  open and scan the p-file.
@@ -995,8 +1018,8 @@ 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)
                {
-                       char    unlinkbuf[100];
-                       strcpy(unlinkbuf, &dir->d_name[2]);
+                       char    unlinkbuf[FBUFSIZ];
+                       gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
                        unlink(unlinkbuf);
                }
        }
                        unlink(unlinkbuf);
                }
        }
@@ -1073,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;
@@ -1083,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 */
@@ -1140,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)
        {
@@ -1169,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
@@ -1324,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();
 
@@ -1478,3 +1532,44 @@ username()
        return (p);
 # endif UIDUSER
 }
        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);
+}