1148Seric # include <stdio.h> 2148Seric # include <sys/types.h> 3148Seric # include <sys/stat.h> 4261Seric # include <sys/dir.h> 5148Seric # include <sysexits.h> 6202Seric # include <whoami.h> 7148Seric 8*585Seric static char SccsId[] = "@(#)sccs.c 1.22 08/09/80"; 9155Seric 10157Seric # define bitset(bit, word) ((bit) & (word)) 11157Seric 12157Seric typedef char bool; 13200Seric # define TRUE 1 14200Seric # define FALSE 0 15157Seric 16148Seric struct sccsprog 17148Seric { 18148Seric char *sccsname; /* name of SCCS routine */ 19200Seric short sccsoper; /* opcode, see below */ 20200Seric short sccsflags; /* flags, see below */ 21148Seric char *sccspath; /* pathname of binary implementing */ 22148Seric }; 23148Seric 24200Seric /* values for sccsoper */ 25200Seric # define PROG 0 /* call a program */ 26201Seric # define CMACRO 1 /* command substitution macro */ 27226Seric # define FIX 2 /* fix a delta */ 28261Seric # define CLEAN 3 /* clean out recreatable files */ 29396Seric # define UNEDIT 4 /* unedit a file */ 30200Seric 31157Seric /* bits for sccsflags */ 32200Seric # define NO_SDOT 0001 /* no s. on front of args */ 33200Seric # define REALUSER 0002 /* protected (e.g., admin) */ 34148Seric 35202Seric # ifdef CSVAX 36202Seric # define PROGPATH(name) "/usr/local/name" 37202Seric # endif CSVAX 38202Seric 39202Seric # ifndef PROGPATH 40202Seric # define PROGPATH(name) "/usr/sccs/name" 41202Seric # endif PROGPATH 42202Seric 43148Seric struct sccsprog SccsProg[] = 44148Seric { 45202Seric "admin", PROG, REALUSER, PROGPATH(admin), 46202Seric "chghist", PROG, 0, PROGPATH(rmdel), 47202Seric "comb", PROG, 0, PROGPATH(comb), 48202Seric "delta", PROG, 0, PROGPATH(delta), 49202Seric "get", PROG, 0, PROGPATH(get), 50202Seric "help", PROG, NO_SDOT, PROGPATH(help), 51202Seric "prt", PROG, 0, PROGPATH(prt), 52202Seric "rmdel", PROG, REALUSER, PROGPATH(rmdel), 53202Seric "what", PROG, NO_SDOT, PROGPATH(what), 54393Seric "edit", CMACRO, 0, "get -e", 55393Seric "delget", CMACRO, 0, "delta/get", 56398Seric "deledit", CMACRO, 0, "delta/get -e", 57201Seric "del", CMACRO, 0, "delta/get", 58226Seric "delt", CMACRO, 0, "delta/get", 59226Seric "fix", FIX, 0, NULL, 60346Seric "clean", CLEAN, REALUSER, (char *) TRUE, 61346Seric "info", CLEAN, REALUSER, (char *) FALSE, 62396Seric "unedit", UNEDIT, 0, NULL, 63200Seric NULL, -1, 0, NULL 64148Seric }; 65148Seric 66396Seric struct pfile 67396Seric { 68396Seric char *p_osid; /* old SID */ 69396Seric char *p_nsid; /* new SID */ 70396Seric char *p_user; /* user who did edit */ 71396Seric char *p_date; /* date of get */ 72396Seric char *p_time; /* time of get */ 73396Seric }; 74396Seric 75157Seric char *SccsPath = "SCCS"; /* pathname of SCCS files */ 76157Seric bool RealUser; /* if set, running as real user */ 77393Seric # ifdef DEBUG 78393Seric bool Debug; /* turn on tracing */ 79393Seric # endif 80148Seric 81148Seric main(argc, argv) 82148Seric int argc; 83148Seric char **argv; 84148Seric { 85148Seric register char *p; 86262Seric extern struct sccsprog *lookup(); 87148Seric 88148Seric /* 89148Seric ** Detect and decode flags intended for this program. 90148Seric */ 91148Seric 92200Seric if (argc < 2) 93148Seric { 94200Seric fprintf(stderr, "Usage: sccs [flags] command [flags]\n"); 95200Seric exit(EX_USAGE); 96200Seric } 97200Seric argv[argc] = NULL; 98200Seric 99262Seric if (lookup(argv[0]) == NULL) 100200Seric { 101262Seric while ((p = *++argv) != NULL) 102148Seric { 103262Seric if (*p != '-') 104262Seric break; 105262Seric switch (*++p) 106262Seric { 107262Seric case 'r': /* run as real user */ 108262Seric setuid(getuid()); 109262Seric RealUser++; 110262Seric break; 111148Seric 112262Seric case 'p': /* path of sccs files */ 113262Seric SccsPath = ++p; 114262Seric break; 115148Seric 116393Seric # ifdef DEBUG 117393Seric case 'T': /* trace */ 118393Seric Debug++; 119393Seric break; 120393Seric # endif 121393Seric 122262Seric default: 123262Seric fprintf(stderr, "Sccs: unknown option -%s\n", p); 124262Seric break; 125262Seric } 126148Seric } 127262Seric if (SccsPath[0] == '\0') 128262Seric SccsPath = "."; 129148Seric } 130148Seric 131201Seric command(argv, FALSE); 132200Seric exit(EX_OK); 133200Seric } 134157Seric 135201Seric command(argv, forkflag) 136200Seric char **argv; 137201Seric bool forkflag; 138200Seric { 139200Seric register struct sccsprog *cmd; 140200Seric register char *p; 141201Seric register char *q; 142201Seric char buf[40]; 143262Seric extern struct sccsprog *lookup(); 144*585Seric char *nav[200]; 145393Seric char **avp; 146*585Seric register int i; 147*585Seric extern bool unedit(); 148200Seric 149393Seric # ifdef DEBUG 150393Seric if (Debug) 151393Seric { 152393Seric printf("command:\n"); 153393Seric for (avp = argv; *avp != NULL; avp++) 154393Seric printf(" \"%s\"\n", *avp); 155393Seric } 156393Seric # endif 157393Seric 158157Seric /* 159148Seric ** Look up command. 160200Seric ** At this point, argv points to the command name. 161148Seric */ 162148Seric 163396Seric cmd = lookup(argv[0]); 164262Seric if (cmd == NULL) 165148Seric { 166396Seric fprintf(stderr, "Sccs: Unknown command \"%s\"\n", argv[0]); 167148Seric exit(EX_USAGE); 168148Seric } 169148Seric 170148Seric /* 171200Seric ** Interpret operation associated with this command. 172157Seric */ 173157Seric 174200Seric switch (cmd->sccsoper) 175200Seric { 176200Seric case PROG: /* call an sccs prog */ 177201Seric callprog(cmd->sccspath, cmd->sccsflags, argv, forkflag); 178201Seric break; 179201Seric 180201Seric case CMACRO: /* command macro */ 181201Seric for (p = cmd->sccspath; *p != '\0'; p++) 182201Seric { 183393Seric avp = nav; 184393Seric *avp++ = buf; 185201Seric for (q = buf; *p != '/' && *p != '\0'; p++, q++) 186393Seric { 187393Seric if (*p == ' ') 188393Seric { 189393Seric *q = '\0'; 190393Seric *avp++ = &q[1]; 191393Seric } 192393Seric else 193393Seric *q = *p; 194393Seric } 195201Seric *q = '\0'; 196393Seric *avp = NULL; 197393Seric xcommand(&argv[1], *p != '\0', nav[0], nav[1], nav[2], 198393Seric nav[3], nav[4], nav[5], nav[6]); 199201Seric } 200201Seric fprintf(stderr, "Sccs internal error: CMACRO\n"); 201200Seric exit(EX_SOFTWARE); 202157Seric 203226Seric case FIX: /* fix a delta */ 204568Seric if (strncmp(argv[1], "-r", 2) != 0) 205226Seric { 206226Seric fprintf(stderr, "Sccs: -r flag needed for fix command\n"); 207226Seric break; 208226Seric } 209226Seric xcommand(&argv[1], TRUE, "get", "-k", NULL); 210226Seric xcommand(&argv[1], TRUE, "rmdel", NULL); 211226Seric xcommand(&argv[2], FALSE, "get", "-e", "-g", NULL); 212226Seric fprintf(stderr, "Sccs internal error: FIX\n"); 213226Seric exit(EX_SOFTWARE); 214226Seric 215261Seric case CLEAN: 216346Seric clean((bool) cmd->sccspath); 217261Seric break; 218261Seric 219396Seric case UNEDIT: 220*585Seric i = 0; 221396Seric for (avp = &argv[1]; *avp != NULL; avp++) 222*585Seric { 223*585Seric if (unedit(*avp)) 224*585Seric nav[i++] = *avp; 225*585Seric } 226*585Seric nav[i] = NULL; 227*585Seric if (i > 0) 228*585Seric xcommand(nav, FALSE, "get", NULL); 229396Seric break; 230396Seric 231200Seric default: 232200Seric fprintf(stderr, "Sccs internal error: oper %d\n", cmd->sccsoper); 233200Seric exit(EX_SOFTWARE); 234200Seric } 235200Seric } 236262Seric /* 237262Seric ** LOOKUP -- look up an SCCS command name. 238262Seric ** 239262Seric ** Parameters: 240262Seric ** name -- the name of the command to look up. 241262Seric ** 242262Seric ** Returns: 243262Seric ** ptr to command descriptor for this command. 244262Seric ** NULL if no such entry. 245262Seric ** 246262Seric ** Side Effects: 247262Seric ** none. 248262Seric */ 249200Seric 250262Seric struct sccsprog * 251262Seric lookup(name) 252262Seric char *name; 253262Seric { 254262Seric register struct sccsprog *cmd; 255226Seric 256262Seric for (cmd = SccsProg; cmd->sccsname != NULL; cmd++) 257262Seric { 258262Seric if (strcmp(cmd->sccsname, name) == 0) 259262Seric return (cmd); 260262Seric } 261262Seric return (NULL); 262262Seric } 263262Seric 264262Seric 265226Seric xcommand(argv, forkflag, arg0) 266226Seric char **argv; 267226Seric bool forkflag; 268226Seric char *arg0; 269226Seric { 270226Seric register char **av; 271226Seric char *newargv[1000]; 272226Seric register char **np; 273226Seric 274226Seric np = newargv; 275226Seric for (av = &arg0; *av != NULL; av++) 276226Seric *np++ = *av; 277226Seric for (av = argv; *av != NULL; av++) 278226Seric *np++ = *av; 279226Seric *np = NULL; 280226Seric command(newargv, forkflag); 281226Seric } 282226Seric 283200Seric callprog(progpath, flags, argv, forkflag) 284200Seric char *progpath; 285200Seric short flags; 286200Seric char **argv; 287200Seric bool forkflag; 288200Seric { 289200Seric register char *p; 290200Seric register char **av; 291200Seric extern char *makefile(); 292200Seric register int i; 293201Seric auto int st; 294200Seric 295200Seric if (*argv == NULL) 296200Seric return (-1); 297200Seric 298157Seric /* 299226Seric ** Fork if appropriate. 300148Seric */ 301148Seric 302200Seric if (forkflag) 303200Seric { 304393Seric # ifdef DEBUG 305393Seric if (Debug) 306393Seric printf("Forking\n"); 307393Seric # endif 308200Seric i = fork(); 309200Seric if (i < 0) 310200Seric { 311200Seric fprintf(stderr, "Sccs: cannot fork"); 312200Seric exit(EX_OSERR); 313200Seric } 314200Seric else if (i > 0) 315201Seric { 316201Seric wait(&st); 317201Seric return (st); 318201Seric } 319200Seric } 320200Seric 321200Seric /* 322226Seric ** Build new argument vector. 323226Seric */ 324226Seric 325226Seric /* copy program filename arguments and flags */ 326226Seric av = argv; 327226Seric while ((p = *++av) != NULL) 328226Seric { 329226Seric if (!bitset(NO_SDOT, flags) && *p != '-') 330226Seric *av = makefile(p); 331226Seric } 332226Seric 333226Seric /* 334200Seric ** Set protection as appropriate. 335200Seric */ 336200Seric 337200Seric if (bitset(REALUSER, flags)) 338200Seric setuid(getuid()); 339226Seric 340200Seric /* 341226Seric ** Call real SCCS program. 342200Seric */ 343200Seric 344226Seric execv(progpath, argv); 345148Seric fprintf(stderr, "Sccs: cannot execute "); 346200Seric perror(progpath); 347148Seric exit(EX_UNAVAILABLE); 348148Seric } 349148Seric 350148Seric 351148Seric char * 352148Seric makefile(name) 353148Seric char *name; 354148Seric { 355148Seric register char *p; 356148Seric register char c; 357148Seric char buf[512]; 358148Seric struct stat stbuf; 359148Seric extern char *malloc(); 360148Seric 361148Seric /* 362148Seric ** See if this filename should be used as-is. 363148Seric ** There are three conditions where this can occur. 364148Seric ** 1. The name already begins with "s.". 365148Seric ** 2. The name has a "/" in it somewhere. 366148Seric ** 3. The name references a directory. 367148Seric */ 368148Seric 369568Seric if (strncmp(name, "s.", 2) == 0) 370148Seric return (name); 371148Seric for (p = name; (c = *p) != '\0'; p++) 372148Seric { 373148Seric if (c == '/') 374148Seric return (name); 375148Seric } 376148Seric if (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR) 377148Seric return (name); 378148Seric 379148Seric /* 380148Seric ** Prepend the path of the sccs file. 381148Seric */ 382148Seric 383148Seric strcpy(buf, SccsPath); 384157Seric strcat(buf, "/s."); 385148Seric strcat(buf, name); 386148Seric p = malloc(strlen(buf) + 1); 387148Seric if (p == NULL) 388148Seric { 389148Seric perror("Sccs: no mem"); 390148Seric exit(EX_OSERR); 391148Seric } 392148Seric strcpy(p, buf); 393148Seric return (p); 394148Seric } 395261Seric /* 396261Seric ** CLEAN -- clean out recreatable files 397261Seric ** 398261Seric ** Any file for which an "s." file exists but no "p." file 399261Seric ** exists in the current directory is purged. 400261Seric ** 401261Seric ** Parameters: 402346Seric ** really -- if TRUE, remove everything. 403346Seric ** else, just report status. 404261Seric ** 405261Seric ** Returns: 406261Seric ** none. 407261Seric ** 408261Seric ** Side Effects: 409261Seric ** removes files in the current directory. 410261Seric */ 411261Seric 412346Seric clean(really) 413346Seric bool really; 414261Seric { 415261Seric struct direct dir; 416261Seric struct stat stbuf; 417261Seric char buf[100]; 418394Seric char pline[120]; 419346Seric register FILE *dirfd; 420346Seric register char *basefile; 421351Seric bool gotedit; 422394Seric FILE *pfp; 423261Seric 424261Seric dirfd = fopen(SccsPath, "r"); 425261Seric if (dirfd == NULL) 426261Seric { 427261Seric fprintf(stderr, "Sccs: cannot open %s\n", SccsPath); 428261Seric return; 429261Seric } 430261Seric 431261Seric /* 432261Seric ** Scan the SCCS directory looking for s. files. 433261Seric */ 434261Seric 435351Seric gotedit = FALSE; 436261Seric while (fread(&dir, sizeof dir, 1, dirfd) != NULL) 437261Seric { 438568Seric if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0) 439261Seric continue; 440261Seric 441261Seric /* got an s. file -- see if the p. file exists */ 442261Seric strcpy(buf, SccsPath); 443261Seric strcat(buf, "/p."); 444346Seric basefile = &buf[strlen(buf)]; 445568Seric strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2); 446346Seric basefile[sizeof dir.d_name - 2] = '\0'; 447394Seric pfp = fopen(buf, "r"); 448394Seric if (pfp != NULL) 449346Seric { 450394Seric while (fgets(pline, sizeof pline, pfp) != NULL) 451416Seric printf("%12s: being edited: %s", basefile, pline); 452394Seric fclose(pfp); 453351Seric gotedit = TRUE; 454261Seric continue; 455346Seric } 456261Seric 457261Seric /* the s. file exists and no p. file exists -- unlink the g-file */ 458346Seric if (really) 459346Seric { 460568Seric strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2); 461346Seric buf[sizeof dir.d_name - 2] = '\0'; 462346Seric unlink(buf); 463346Seric } 464261Seric } 465261Seric 466261Seric fclose(dirfd); 467351Seric if (!gotedit && !really) 468416Seric printf("Nothing being edited\n"); 469261Seric } 470396Seric /* 471396Seric ** UNEDIT -- unedit a file 472396Seric ** 473396Seric ** Checks to see that the current user is actually editting 474396Seric ** the file and arranges that s/he is not editting it. 475396Seric ** 476396Seric ** Parameters: 477416Seric ** fn -- the name of the file to be unedited. 478396Seric ** 479396Seric ** Returns: 480*585Seric ** TRUE -- if the file was successfully unedited. 481*585Seric ** FALSE -- if the file was not unedited for some 482*585Seric ** reason. 483396Seric ** 484396Seric ** Side Effects: 485396Seric ** fn is removed 486396Seric ** entries are removed from pfile. 487396Seric */ 488396Seric 489*585Seric bool 490396Seric unedit(fn) 491396Seric char *fn; 492396Seric { 493396Seric register FILE *pfp; 494396Seric char *pfn; 495396Seric static char tfn[] = "/tmp/sccsXXXXX"; 496396Seric FILE *tfp; 497396Seric register char *p; 498396Seric register char *q; 499396Seric bool delete = FALSE; 500396Seric bool others = FALSE; 501396Seric char *myname; 502396Seric extern char *getlogin(); 503396Seric struct pfile *pent; 504396Seric extern struct pfile *getpfile(); 505396Seric char buf[120]; 506396Seric 507396Seric /* make "s." filename & find the trailing component */ 508396Seric pfn = makefile(fn); 509396Seric q = &pfn[strlen(pfn) - 1]; 510396Seric while (q > pfn && *q != '/') 511396Seric q--; 512396Seric if (q <= pfn && (q[0] != 's' || q[1] != '.')) 513396Seric { 514396Seric fprintf(stderr, "Sccs: bad file name \"%s\"\n", fn); 515*585Seric return (FALSE); 516396Seric } 517396Seric 518396Seric /* turn "s." into "p." */ 519396Seric *++q = 'p'; 520396Seric 521396Seric pfp = fopen(pfn, "r"); 522396Seric if (pfp == NULL) 523396Seric { 524416Seric printf("%12s: not being edited\n", fn); 525*585Seric return (FALSE); 526396Seric } 527396Seric 528396Seric /* 529396Seric ** Copy p-file to temp file, doing deletions as needed. 530396Seric */ 531396Seric 532396Seric mktemp(tfn); 533396Seric tfp = fopen(tfn, "w"); 534396Seric if (tfp == NULL) 535396Seric { 536396Seric fprintf(stderr, "Sccs: cannot create \"%s\"\n", tfn); 537396Seric exit(EX_OSERR); 538396Seric } 539396Seric 540396Seric myname = getlogin(); 541396Seric while ((pent = getpfile(pfp)) != NULL) 542396Seric { 543396Seric if (strcmp(pent->p_user, myname) == 0) 544396Seric { 545396Seric /* a match */ 546396Seric delete++; 547396Seric } 548396Seric else 549396Seric { 550396Seric fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid, 551396Seric pent->p_nsid, pent->p_user, pent->p_date, 552396Seric pent->p_time); 553396Seric others++; 554396Seric } 555396Seric } 556396Seric 557396Seric /* do final cleanup */ 558396Seric if (others) 559396Seric { 560396Seric if (freopen(tfn, "r", tfp) == NULL) 561396Seric { 562396Seric fprintf(stderr, "Sccs: cannot reopen \"%s\"\n", tfn); 563396Seric exit(EX_OSERR); 564396Seric } 565396Seric if (freopen(pfn, "w", pfp) == NULL) 566396Seric { 567396Seric fprintf(stderr, "Sccs: cannot create \"%s\"\n", pfn); 568*585Seric return (FALSE); 569396Seric } 570396Seric while (fgets(buf, sizeof buf, tfp) != NULL) 571396Seric fputs(buf, pfp); 572396Seric } 573396Seric else 574396Seric { 575396Seric unlink(pfn); 576396Seric } 577396Seric fclose(tfp); 578396Seric fclose(pfp); 579396Seric unlink(tfn); 580396Seric 581396Seric if (delete) 582396Seric { 583396Seric unlink(fn); 584396Seric printf("%12s: removed\n", fn); 585*585Seric return (TRUE); 586396Seric } 587396Seric else 588396Seric { 589416Seric printf("%12s: not being edited by you\n", fn); 590*585Seric return (FALSE); 591396Seric } 592396Seric } 593396Seric /* 594396Seric ** GETPFILE -- get an entry from the p-file 595396Seric ** 596396Seric ** Parameters: 597396Seric ** pfp -- p-file file pointer 598396Seric ** 599396Seric ** Returns: 600396Seric ** pointer to p-file struct for next entry 601396Seric ** NULL on EOF or error 602396Seric ** 603396Seric ** Side Effects: 604396Seric ** Each call wipes out results of previous call. 605396Seric */ 606396Seric 607396Seric struct pfile * 608396Seric getpfile(pfp) 609396Seric FILE *pfp; 610396Seric { 611396Seric static struct pfile ent; 612396Seric static char buf[120]; 613396Seric register char *p; 614396Seric extern char *nextfield(); 615396Seric 616396Seric if (fgets(buf, sizeof buf, pfp) == NULL) 617396Seric return (NULL); 618396Seric 619396Seric ent.p_osid = p = buf; 620396Seric ent.p_nsid = p = nextfield(p); 621396Seric ent.p_user = p = nextfield(p); 622396Seric ent.p_date = p = nextfield(p); 623396Seric ent.p_time = p = nextfield(p); 624396Seric if (p == NULL || nextfield(p) != NULL) 625396Seric return (NULL); 626396Seric 627396Seric return (&ent); 628396Seric } 629396Seric 630396Seric 631396Seric char * 632396Seric nextfield(p) 633396Seric register char *p; 634396Seric { 635396Seric if (p == NULL || *p == '\0') 636396Seric return (NULL); 637396Seric while (*p != ' ' && *p != '\n' && *p != '\0') 638396Seric p++; 639396Seric if (*p == '\n' || *p == '\0') 640396Seric { 641396Seric *p = '\0'; 642396Seric return (NULL); 643396Seric } 644396Seric *p++ = '\0'; 645396Seric return (p); 646396Seric } 647