+ dirfd = fopen(SccsPath, "r");
+ if (dirfd == NULL)
+ {
+ fprintf(stderr, "Sccs: cannot open %s\n", SccsPath);
+ return;
+ }
+
+ /*
+ ** Scan the SCCS directory looking for s. files.
+ */
+
+ gotedit = FALSE;
+ while (fread(&dir, sizeof dir, 1, dirfd) != NULL)
+ {
+ if (dir.d_ino == 0 || strcmpn(dir.d_name, "s.", 2) != 0)
+ continue;
+
+ /* got an s. file -- see if the p. file exists */
+ strcpy(buf, SccsPath);
+ strcat(buf, "/p.");
+ basefile = &buf[strlen(buf)];
+ strcpyn(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
+ basefile[sizeof dir.d_name - 2] = '\0';
+ pfp = fopen(buf, "r");
+ if (pfp != NULL)
+ {
+ while (fgets(pline, sizeof pline, pfp) != NULL)
+ printf("%12s: being edited: %s", basefile, pline);
+ fclose(pfp);
+ gotedit = TRUE;
+ continue;
+ }
+
+ /* the s. file exists and no p. file exists -- unlink the g-file */
+ if (really)
+ {
+ strcpyn(buf, &dir.d_name[2], sizeof dir.d_name - 2);
+ buf[sizeof dir.d_name - 2] = '\0';
+ unlink(buf);
+ }
+ }
+
+ fclose(dirfd);
+ if (!gotedit && !really)
+ printf("Nothing being edited\n");
+}
+\f/*
+** UNEDIT -- unedit a file
+**
+** Checks to see that the current user is actually editting
+** the file and arranges that s/he is not editting it.
+**
+** Parameters:
+** fn -- the name of the file to be unedited.
+**
+** Returns:
+** none.
+**
+** Side Effects:
+** fn is removed
+** entries are removed from pfile.
+*/
+
+unedit(fn)
+ char *fn;
+{
+ register FILE *pfp;
+ char *pfn;
+ static char tfn[] = "/tmp/sccsXXXXX";
+ FILE *tfp;
+ register char *p;
+ register char *q;
+ bool delete = FALSE;
+ bool others = FALSE;
+ char *myname;
+ extern char *getlogin();
+ struct pfile *pent;
+ extern struct pfile *getpfile();
+ char buf[120];
+
+ /* make "s." filename & find the trailing component */
+ pfn = makefile(fn);
+ q = &pfn[strlen(pfn) - 1];
+ while (q > pfn && *q != '/')
+ q--;
+ if (q <= pfn && (q[0] != 's' || q[1] != '.'))
+ {
+ fprintf(stderr, "Sccs: bad file name \"%s\"\n", fn);
+ return;
+ }
+
+ /* turn "s." into "p." */
+ *++q = 'p';
+
+ pfp = fopen(pfn, "r");
+ if (pfp == NULL)
+ {
+ printf("%12s: not being edited\n", fn);
+ return;
+ }
+
+ /*
+ ** Copy p-file to temp file, doing deletions as needed.
+ */
+
+ mktemp(tfn);
+ tfp = fopen(tfn, "w");
+ if (tfp == NULL)
+ {
+ fprintf(stderr, "Sccs: cannot create \"%s\"\n", tfn);
+ exit(EX_OSERR);
+ }
+
+ myname = getlogin();
+ while ((pent = getpfile(pfp)) != NULL)
+ {
+ if (strcmp(pent->p_user, myname) == 0)
+ {
+ /* a match */
+ delete++;
+ }
+ else
+ {
+ fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid,
+ pent->p_nsid, pent->p_user, pent->p_date,
+ pent->p_time);
+ others++;
+ }
+ }
+
+ /* do final cleanup */
+ if (others)
+ {
+ if (freopen(tfn, "r", tfp) == NULL)
+ {
+ fprintf(stderr, "Sccs: cannot reopen \"%s\"\n", tfn);
+ exit(EX_OSERR);
+ }
+ if (freopen(pfn, "w", pfp) == NULL)
+ {
+ fprintf(stderr, "Sccs: cannot create \"%s\"\n", pfn);
+ return;
+ }
+ while (fgets(buf, sizeof buf, tfp) != NULL)
+ fputs(buf, pfp);
+ }
+ else
+ {
+ unlink(pfn);
+ }
+ fclose(tfp);
+ fclose(pfp);
+ unlink(tfn);
+
+ if (delete)
+ {
+ unlink(fn);
+ printf("%12s: removed\n", fn);
+ }
+ else
+ {
+ printf("%12s: not being edited by you\n", fn);
+ }
+}
+\f/*
+** GETPFILE -- get an entry from the p-file
+**
+** Parameters:
+** pfp -- p-file file pointer
+**
+** Returns:
+** pointer to p-file struct for next entry
+** NULL on EOF or error
+**
+** Side Effects:
+** Each call wipes out results of previous call.
+*/
+
+struct pfile *
+getpfile(pfp)
+ FILE *pfp;
+{
+ static struct pfile ent;
+ static char buf[120];
+ register char *p;
+ extern char *nextfield();
+
+ if (fgets(buf, sizeof buf, pfp) == NULL)
+ return (NULL);
+
+ ent.p_osid = p = buf;
+ ent.p_nsid = 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);
+
+ return (&ent);
+}
+
+
+char *
+nextfield(p)
+ register char *p;
+{
+ if (p == NULL || *p == '\0')
+ return (NULL);
+ while (*p != ' ' && *p != '\n' && *p != '\0')
+ p++;
+ if (*p == '\n' || *p == '\0')
+ {
+ *p = '\0';
+ return (NULL);
+ }
+ *p++ = '\0';
+ return (p);
+}