BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / sccs / sccs.c
index 001f69f..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 <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 <pwd.h>
 # include <errno.h>
 # include <signal.h>
 # include <sysexits.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.67 %G%";
 \f
 /*******************  Configuration Information  ********************/
 
 \f
 /*******************  Configuration Information  ********************/
 
@@ -104,10 +141,6 @@ static char SccsId[] = "@(#)sccs.c 1.67 %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/local/name"       /* place to find binaries */
-# endif PROGPATH
-
 /****************  End of Configuration Information  ****************/
 \f
 typedef char   bool;
 /****************  End of Configuration Information  ****************/
 \f
 typedef char   bool;
@@ -157,7 +190,7 @@ struct sccsprog
 struct sccsprog SccsProg[] =
 {
        "admin",        PROG,   REALUSER,               PROGPATH(admin),
 struct sccsprog SccsProg[] =
 {
        "admin",        PROG,   REALUSER,               PROGPATH(admin),
-       "chghist",      PROG,   0,                      PROGPATH(rmdel),
+       "cdc",          PROG,   0,                      PROGPATH(rmdel),
        "comb",         PROG,   0,                      PROGPATH(comb),
        "delta",        PROG,   0,                      PROGPATH(delta),
        "get",          PROG,   0,                      PROGPATH(get),
        "comb",         PROG,   0,                      PROGPATH(comb),
        "delta",        PROG,   0,                      PROGPATH(delta),
        "get",          PROG,   0,                      PROGPATH(get),
@@ -179,7 +212,7 @@ struct sccsprog SccsProg[] =
        "unedit",       UNEDIT, NO_SDOT,                NULL,
        "diffs",        DIFFS,  NO_SDOT|REALUSER,       NULL,
        "-diff",        DODIFF, NO_SDOT|REALUSER,       PROGPATH(bdiff),
        "unedit",       UNEDIT, NO_SDOT,                NULL,
        "diffs",        DIFFS,  NO_SDOT|REALUSER,       NULL,
        "-diff",        DODIFF, NO_SDOT|REALUSER,       PROGPATH(bdiff),
-       "print",        CMACRO, 0,                      "prt -e/get -p -m -s",
+       "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,
        "branch",       CMACRO, NO_SDOT,
                "get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
        "enter",        ENTER,  NO_SDOT,                NULL,
@@ -213,6 +246,14 @@ bool       Debug;                  /* turn on tracing */
 # ifndef V6
 extern char    *getenv();
 # endif V6
 # 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,10 +266,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 +282,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 +387,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;
@@ -440,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 */
@@ -461,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;
@@ -549,7 +590,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 +599,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);
                        }
@@ -631,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)
@@ -663,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);
@@ -730,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;
 
@@ -743,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:
@@ -873,9 +957,9 @@ clean(mode, argv)
        char **argv;
 {
        struct direct *dir;
        char **argv;
 {
        struct direct *dir;
-       char buf[100];
+       char buf[FBUFSIZ];
        char *bufend;
        char *bufend;
-       register DIR *dirfd;
+       register DIR *dirp;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
        register char *basefile;
        bool gotedit;
        bool gotpfent;
@@ -928,19 +1012,19 @@ 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 = opendir(buf);
-       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);
@@ -953,14 +1037,14 @@ clean(mode, argv)
        */
 
        gotedit = FALSE;
        */
 
        gotedit = FALSE;
-       while (dir = readdir(dirfd)) {
+       while (dir = readdir(dirp)) {
                if (strncmp(dir->d_name, "s.", 2) != 0)
                        continue;
                
                /* got an s. file -- see if the p. file exists */
                if (strncmp(dir->d_name, "s.", 2) != 0)
                        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,14 +1079,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)
                {
-                       char    unlinkbuf[100];
-                       strcpy(unlinkbuf, &dir->d_name[2]);
+                       char    unlinkbuf[FBUFSIZ];
+                       gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
                        unlink(unlinkbuf);
                }
        }
 
        /* cleanup & report results */
                        unlink(unlinkbuf);
                }
        }
 
        /* cleanup & report results */
-       closedir(dirfd);
+       closedir(dirp);
        if (!gotedit && mode == INFOC)
        {
                printf("Nothing being edited");
        if (!gotedit && mode == INFOC)
        {
                printf("Nothing being edited");
@@ -1073,8 +1157,8 @@ 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;
        FILE *tfp;
        register char *q;
        bool delete = FALSE;
@@ -1083,8 +1167,8 @@ 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];
-       extern char *makefile();
+       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);
@@ -1140,6 +1224,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 +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
@@ -1204,33 +1319,9 @@ dodiff(getv, gfile)
        register int pid;
        auto int st;
        extern int errno;
        register int pid;
        auto int st;
        extern int errno;
-       int (*osig)();
-       register char *p;
-       register char **ap;
-       bool makescript = FALSE;
-
-       for (ap = getv; *ap != NULL; ap++)
-       {
-               p = *ap;
-               if (p[0] == '-')
-               {
-                       switch (p[1])
-                       {
-                         case 'E':
-                               p[1] = 'e';
-                               makescript = TRUE;
-                               break;
-                       }
-               }
-       }
+       sig_t osig;
 
 
-       if (makescript)
-       {
-               printf("sccs edit %s\n", gfile);
-               printf("ed - %s << 'xxEOFxx'\n", gfile);
-       }
-       else
-               printf("\n------- %s -------\n", gfile);
+       printf("\n------- %s -------\n", gfile);
        fflush(stdout);
 
        /* create context for diff to run in */
        fflush(stdout);
 
        /* create context for diff to run in */
@@ -1267,15 +1358,6 @@ dodiff(getv, gfile)
                }
                command(&getv[1], FALSE, "-diff:elsfhbC");
        }
                }
                command(&getv[1], FALSE, "-diff:elsfhbC");
        }
-
-       if (makescript)
-       {
-               printf("w\n");
-               printf("q\n");
-               printf("'xxEOFxx'\n");
-               printf("sccs delta %s\n", gfile);
-       }
-
        return (rval);
 }
 \f
        return (rval);
 }
 \f
@@ -1324,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();
 
@@ -1469,7 +1551,6 @@ username()
        return (pw->pw_name);
 # else
        extern char *getlogin();
        return (pw->pw_name);
 # else
        extern char *getlogin();
-       extern char *getenv();
        register char *p;
 
        p = getenv("USER");
        register char *p;
 
        p = getenv("USER");
@@ -1478,3 +1559,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);
+}