include aculib in cleanup
[unix-history] / usr / src / usr.bin / sccs / sccs.c
index d63bad7..4781865 100644 (file)
@@ -1,11 +1,10 @@
 # include <stdio.h>
 # include <stdio.h>
-# include <sys/types.h>
+# include <sys/param.h>
 # include <sys/stat.h>
 # include <sys/stat.h>
-# include <sys/dir.h>
+# include <dir.h>
 # include <errno.h>
 # include <signal.h>
 # include <sysexits.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.61.1.1 %G%";
+static char SccsId[] = "@(#)sccs.c     1.70 %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 +105,7 @@ static char SccsId[] = "@(#)sccs.c  1.61.1.1 %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,7 +133,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 CREATE                8       /* create new files */
+# 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 */
@@ -201,7 +182,8 @@ struct sccsprog SccsProg[] =
        "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",
        "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",
-       "create",       CREATE, NO_SDOT,                NULL,
+       "enter",        ENTER,  NO_SDOT,                NULL,
+       "create",       CMACRO, NO_SDOT,                "enter/get:ixbeskcl -t",
        NULL,           -1,     0,                      NULL
 };
 
        NULL,           -1,     0,                      NULL
 };
 
@@ -231,6 +213,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;
@@ -243,10 +231,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] == '/')
@@ -259,12 +247,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);
@@ -364,7 +352,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;
@@ -556,7 +544,7 @@ command(argv, forkflag, arg0)
                syserr("cannot exec %s", cmd->sccspath);
                exit(EX_OSERR);
 
                syserr("cannot exec %s", cmd->sccspath);
                exit(EX_OSERR);
 
-         case CREATE:          /* create new sccs files */
+         case ENTER:           /* enter new sccs files */
                /* skip over flag arguments */
                for (np = &ap[1]; *np != NULL && **np == '-'; np++)
                        continue;
                /* skip over flag arguments */
                for (np = &ap[1]; *np != NULL && **np == '-'; np++)
                        continue;
@@ -567,7 +555,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;
@@ -575,7 +564,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);
                        }
@@ -748,7 +738,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();
@@ -779,11 +769,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);
@@ -792,11 +782,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? */
@@ -890,10 +880,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;
@@ -946,18 +936,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);
@@ -971,16 +961,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.
@@ -1015,14 +1003,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");
@@ -1093,7 +1081,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;
@@ -1103,7 +1091,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 */
@@ -1160,6 +1148,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)
        {
@@ -1189,8 +1203,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
@@ -1344,7 +1363,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();
 
@@ -1498,3 +1517,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);
+}