home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume23 / cio / cio.c next >
C/C++ Source or Header  |  1991-01-08  |  47KB  |  1,620 lines

  1. /*----------------------------------------------------------------------------
  2. / cio.c - This is a C program to replace the over-grown shell script
  3. / that started as something small...
  4. /
  5. / Authors:    Jason P. Winters and Craig J. Kim
  6. /
  7. /  We hereby declare this software to be Public Domain.  That means we don't
  8. /  care what you do with it.  We _would_ prefer that you at least leave our
  9. /  names in it, as befits Authors, but we can't force you.  Of course, since
  10. /  it's public domain, all risks/gains using it are your problems.  You don't
  11. /  have any reason to call us up and complain about it deleting 30megs of
  12. /  your source code without telling you. ( Software should at least *tell* you
  13. /  when it does something like that, right? )  :)
  14. /
  15. / Start Date:    November 23, 1988
  16. / Revisions:    29-Nov-88 jpw - initial coding completed.
  17. /        29-Nov-88 cjk - rewrite of front-end logic
  18. /        30-Nov-88 jpw - Added signals, cleanup of temp files.
  19. /        30-Nov-88 cjk - added -N option
  20. /        01-Dec-88 cjk - added usage
  21. /        01-Dec-88 jpw - added SUID controls for secure files.
  22. /        02-Dec-88 jpw - added -A option
  23. /        05-Dec-88 cjk - added '~' commands for message entering
  24. /        05-Dec-88 cjk - fixed cp to check file status before copying
  25. /        06-Dec-88 cjk - use separate process for user file input
  26. /        06-Dec-88 jpw - source now passes System V.2 lint
  27. /        07-Dec-88 cjk - added environment variable check routine
  28. /        20-Dec-88 cjk - chmod files in source dir to 0640
  29. /        21-Dec-88 cjk - put RCS header if not exists
  30. /        08-Mar-89 cjk - SCCS version
  31. /        02-Oct-89 jpw - Fixed parsing for header type to insert
  32. /                        Fixed small bug in directory creation routine
  33. /        10-May-90 jpw - Changed st += sprintf() code to allow for
  34. /                        broken sprint() calls.  Failed on Sun
  35. /                        machines.
  36. /
  37. / Known bugs:
  38. /       On some systems, the Control-D as end of input in the log entry
  39. /       routines will cause stdin to be closed, which means the next call
  40. /       to get an entry (such as a Title file) will fail.  A call to clearerr()
  41. /       has been added to fix this, but it has not been tested.
  42. /
  43. / To be done:
  44. /    1. When -U is used, the destination directory should be created if
  45. /       it does not exist already.  Also, the file mode should be changed to
  46. /       0640 so that overwriting is not possible by anyone other than
  47. /       $RCSOWN for security.
  48. /    2. Full "interactive" mode support
  49. /   3. Actually use the SCCS version. (Ugh!)
  50. /---------------------------------------------------------------------------*/
  51.  
  52. /*#define DEBUG        /* wanna know what's goin' on? */
  53. /*#define INTERACTIVE    /* enable -I option (incomplete) */
  54. /*#define void    int    /* if system can't handle void types. */
  55. #define V_RCS        /* RCS version */
  56. /*#define V_SCCS        /* SCCS version */
  57.  
  58. /*--------------------------------------------------- includes -------------*/
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <ctype.h>
  62. #include <sys/types.h>
  63. #include <sys/stat.h>
  64. #include <errno.h>
  65. #include <signal.h>
  66.  
  67. /*--------------------------------------------------- externals ------------*/
  68. extern int errno;              /* error code set by system library routines */
  69.  
  70. extern FILE *fopen();               /* open a stream (should be in stdio.h) */
  71. extern FILE *popen();                 /* open a pipe (should be in stdio.h) */
  72. extern char *getenv();                      /* read an environment variable */
  73. extern char *tmpnam();                      /* create a temporary file name */
  74. extern char *mktemp();                  /* create temp file with a template */
  75. extern char *malloc();                        /* allocate a chunk of memory */
  76. extern char *getcwd();                             /* get current directory */
  77. extern int  unlink();                             /* unlink (delete) a file */
  78. extern void exit();                            /* define these for LINT!!!! */
  79. extern void perror();                          /* define these for LINT!!!! */
  80. extern void free();                            /* define these for LINT!!!! */
  81.  
  82. /*--------------------------------------------------- defines --------------*/
  83. #ifndef TRUE
  84. # define TRUE    1
  85. # define FALSE    0
  86. #endif
  87.  
  88. #define TYPE_LOG    0        /* log message */
  89. #define TYPE_TITLE    1        /* title message */
  90. #define MAX_LOG        1020        /* max # of characters for a log */
  91.  
  92. /*--------------------------------------------------- forwards -------------*/
  93. void usage(/*_ void _*/);                    /* print program usage message */
  94. int  getfinput(/*_ char *name, int type _*/);             /* get title file */
  95. int  child_getfinput(/*_ char *name, int type _*/);      /* actual get file */
  96. void doincmds(/*_ char *argv[], int argc, int in _*/); /* perform user wish */
  97. void do_ciodir(/*_ char *filenm _*/);    /* do user wish in recursive if -R */
  98. int  cio(/*_ char *filenme _*/);                          /* the work horse */
  99. int  addrcsext(/*_ char *fname _*/);         /* add RCS file name extension */
  100. int  rmrcsext(/*_ char *fname _*/);       /* remove RCS file name extension */
  101. void inshdr(/*_ char *fname _*/);                      /* insert RCS header */
  102. int  makedir(/*_ char *path _*/);    /* make a directory and all its parent */
  103. char *strstr(/*_ char *s1, char *s2 _*/);  /* find a string within a string */
  104. char *strlwr(/*_ char *s _*/);               /* convert upper case to lower */
  105. int  asciifile(/*_ char *filename _*/);                  /* check file type */
  106. int  rcsfile(/*_ char *filename _*/);             /* check file is RCS file */
  107. int  strrd(/*_ char *buf, int max_size,  FILE *fle _*/);/* read from a file */
  108. void getdir(/*_ void _*/);        /* obtain necessary directory information */
  109. int  fix_envstr(/*_ char *cs _*/);  /* remove leading/trailing blanks, etc. */
  110. void getrcsdir(/*_ char *dir _*/);          /* build rcs dir out of working */
  111. void getworkdir(/*_ char *dir _*/);         /* build working dir out of rcs */
  112. void getsrcdir(/*_ char *dir _*/);       /* build source dir out of working */
  113. char *justname(/*_ char *fpath _*/);        /* return just filename portion */
  114. char *memalloc(/*_ int size _*/);  /* allocate memory and check for success */
  115. void sigcleanup(/*_ void _*/);                 /* cleanup in case interrupt */
  116. void get_final_id(/*_ void _*/);           /* get user id of RCS file owner */
  117. int  nextent(/*_ FILE *fp _*/);    /* read next entry from /etc/passwd file */
  118.  
  119. /*--------------------------------------------------- globals --------------*/
  120. #ifdef V_RCS
  121. char *ci_cmd = "ci";                /* command to use to check in a module. */
  122. char *co_cmd = "co";               /* command to use to check out a module. */
  123. #else
  124. char *ici_cmd = "admin";      /* command to check in a module for 1st time. */
  125. char *ci_cmd = "delta";                    /* command to check in a module. */
  126. char *co_cmd = "get";                     /* command to check out a module. */
  127. #endif
  128. char *currentdir;                                     /* current directory. */
  129. char *homedir;                                    /* user's home directory. */
  130. char *rcswrk;                                                  /* $RCSWORK. */
  131. char *rcsown;                                                   /* $RCSOWN. */
  132. char *rcsdir;                                                   /* $RCSDIR. */
  133. char *srcdir;                                                   /* $RCSSRC. */
  134. char *headdir;                                                 /* $RCSHEAD. */
  135. char *path;                                                       /* $PATH. */
  136. char *pwdfile = "/etc/passwd";                      /* default passwd file. */
  137. char cioopt[100];                      /* large buffer for command options. */
  138. char d_ent[90];                             /* small buffer for file reads. */
  139. char final[400];                    /* final dir to pass on as destination. */
  140. #ifdef V_SCCS
  141. char finalfile[400];                  /* final file without SCCS extension. */
  142. #endif
  143. char logstr[MAX_LOG + 4];                            /* log string to pass. */
  144. char title[100];                                     /* log string to pass. */
  145. char cmdbuf[2400];                                     /* do a single file. */
  146. char editfile[400];                  /* temp edit file, makes cleanup easy. */
  147. char pwdname[20];                                    /* Name found in file. */
  148. char ftypestr[82];                               /* command file(1) output. */
  149. char cii = FALSE;                       /* if set, we are in check in mode. */
  150. char recurse = FALSE;               /* if set, do recursive check in/out's. */
  151. char usertitle = FALSE;                       /* user specified title file. */
  152. char interactive = FALSE;                /* user friendly interactive mode. */
  153. char allfiles = FALSE;                    /* set it TRUE to copy all files. */
  154. char insertheader = FALSE;     /* insert RCS header at the top of the file. */
  155. #ifdef DEBUG
  156. char noexec = TRUE;                     /* set it FALSE for execution mode. */
  157. char verbose = TRUE;                                    /* be a chatterbox. */
  158. #else
  159. char noexec = FALSE;                  /* set it TRUE for no execution mode. */
  160. char verbose = FALSE;                                  /* not a chatterbox. */
  161. #endif
  162. char updsrcdir = FALSE;           /* update master source directory on cii. */
  163. #ifdef V_SCCS
  164. char do_admin = FALSE;                    /* set it TRUE if first check in. */
  165. #endif
  166. char *prognam;                                     /* name of this program. */
  167. struct stat filestat;                                  /* file status info. */
  168. int s_currentdir;                           /* length of current directory. */
  169. int s_homedir;                          /* length of user's home directory. */
  170. int s_rcswrk;                                        /* length of $RCSWORK. */
  171. int s_rcsdir;                                         /* length of $RCSDIR. */
  172. int s_srcdir;                                         /* length of $RCSSRC. */
  173. int s_path;                                             /* length of $PATH. */
  174. int s_rcsown;                                         /* length of $RCSOWN. */
  175. int s_headdir;                                       /* length of $RCSHEAD. */
  176. int user_id;                   /* Effective user id to use if ROOT process. */
  177. int real_user_id;                      /* Save buffer for original user_id. */
  178. int do_unlink;  /* add -l option if not set to avoid unlink of source file. */
  179.  
  180. /*--------------------------------------------------- main() ----------------
  181. / where we begin!
  182. /---------------------------------------------------------------------------*/
  183. main(argc, argv)
  184. int argc;
  185. char *argv[];
  186. {
  187.     register int in;
  188.     register char *cp;        /* used in string updates. */
  189.  
  190.     prognam = justname(argv[0]);    /* program name */
  191.     getdir();            /* go get the enviroment pointers. */
  192.  
  193.     /*
  194.      * figure what the user wants us to be by reading program name
  195.      */
  196.     if (!strcmp("ciitest", prognam) || !strcmp("cootest", prognam))
  197.     {
  198.         getrcsdir(final, currentdir);    /* setup variables. */
  199.         /*
  200.          * print out our variables here.
  201.         */
  202. #ifdef V_RCS
  203.         (void) printf("Homedir:%s:  rcsdir:%s:  rcswrk:%s:  rcssrc:%s:\n",
  204. #else
  205.         (void) printf("HOME:%s:  SCCSDIR:%s:  SCCSWRK:%s:  SCCSSRC:%s:\n",
  206. #endif
  207.                 homedir ? homedir : "",
  208.                 rcsdir ? rcsdir : "",
  209.                 rcswrk ? rcswrk : "",
  210.                 srcdir ? srcdir : "");
  211.         (void) printf("Final dir:%s:\n", final);
  212.         return(0);
  213.     }
  214.     if (!strcmp("cii", prognam))    /* this is input.. */
  215.         cii = TRUE;        /* show we are inputs. */
  216.     else if (strcmp("coo", prognam))/* it's not this either.. */
  217.     {
  218.         (void) printf("Just what did you think this program was, anyway??\n");
  219.         return(-1);
  220.     }
  221.  
  222.     if (!(user_id = geteuid())) /* we are a root process.. */
  223.     {
  224.         (void) umask(027); /* set general mask to private modes. */
  225.         real_user_id = getuid();
  226.         if (cii)    /* only set this if we are in checkin mode. */
  227.             get_final_id(); /* go get the final user ID for file creation. */
  228.         else
  229.             user_id = real_user_id;    /* otherwise, use owners real one. */
  230.         (void) setuid(user_id); /* and, fix up the user id now. */
  231. #ifdef DEBUG
  232.         (void) printf("Using Uid:%d:\n", user_id);
  233. #endif
  234.     }
  235.     else
  236.         real_user_id = user_id;    /* else they should match. */
  237.  
  238.     /* prepare for disasters */
  239.     (void) signal(SIGINT, (int (*)()) sigcleanup);
  240.     (void) signal(SIGQUIT, (int (*)()) sigcleanup);
  241.  
  242.     cp = cioopt;
  243.     title[0] = '\0';  /* make sure no titles are required. */
  244.     for (in = 1; argv[in][0] == '-'; in++) /* while chars here.. */
  245.     {
  246.         switch ((int) argv[in][1])    /* what option? */
  247.         {
  248.         case '?':        /* print program usage */
  249.         case '-':
  250.             usage();
  251.             return(0);
  252.         case 'A':        /* copy all files */
  253.             allfiles = !allfiles;
  254.             break;
  255. #ifdef INTERACTIVE
  256.         case 'I':        /* interactive mode */
  257.             interactive = !interactive;
  258.             break;
  259. #endif
  260.         case 'H':        /* RCS/SCCS header */
  261.             insertheader = !insertheader;
  262.             break;
  263.         case 'N':        /* do not execute */
  264.             noexec = !noexec;
  265.             break;
  266.         case 'R':        /* Recursion flag. */
  267.             recurse = !recurse;
  268.             break;
  269.         case 'T':        /* get Title flag. */
  270.             if(cii)    /* get a title file and name. */
  271.                 (void) getfinput(title, TYPE_TITLE);
  272.             break;
  273.         case 'U':        /* update working directory */
  274.             updsrcdir = !updsrcdir;
  275.             break;
  276.         case 'V':        /* verbose */
  277.             verbose = !verbose;
  278.             break;
  279. #ifdef V_RCS
  280.         case 'm':        /* they gave us one */
  281. #else
  282.         case 'y':        /* they gave us one */
  283. #endif
  284.             (void) strcpy(logstr, &argv[in][1]); /* copy across. */
  285.             break;
  286.         case 't':        /* user gave us one */
  287.             (void) strcpy(title, &argv[in][1]);
  288.             usertitle = TRUE;
  289.             break;
  290.         default:        /* must be a ci or co command */
  291.             (void) sprintf(cp, " %s", argv[in]); /* append */
  292.             cp += strlen(cp); /* skip to end of string. */
  293.             break;
  294.         }
  295.     }
  296. #ifdef INTERACTIVE
  297.     if (noexec && interactive)    /* resolve conflict of interest */
  298.         interactive = FALSE;
  299. #endif
  300.     doincmds(argv, argc, in);    /* do the check in command. */
  301.     if (title[0] && !usertitle)    /* if file is here. */
  302.         (void) unlink(title);    /* zap it! */
  303.     return(0);            /* we did it good! */
  304. }
  305.  
  306. /*--------------------------------------------------- usage() ---------------
  307. / print program usage information
  308. /---------------------------------------------------------------------------*/
  309. void
  310. usage()
  311. {
  312.     char *inout = cii ? "in" : "out";
  313.  
  314. #ifdef INTERACTIVE
  315.     (void) fprintf(stderr, "Usage: %s [-A] %s[-I] [-N] [-R] %s",
  316. #else
  317.     (void) fprintf(stderr, "Usage: %s [-A] %s[-N] [-R] %s",
  318. #endif
  319.             prognam, cii ? "[-H] " : "",
  320.                  cii ? "[-T] [-U] " : "");
  321.     (void) fprintf(stderr, "[-V] [%s options] [[filename]...]\n",
  322.             cii ? ci_cmd : co_cmd);
  323.     (void) fprintf(stderr, "     -A : check %s all files\n", inout);
  324. #ifdef INTERACTIVE
  325.     (void) fprintf(stderr, "     -I : interactive mode\n");
  326. #endif
  327.     if (cii)
  328. #ifdef V_RCS
  329.         (void) fprintf(stderr, "     -H : attach RCS header\n");
  330. #else
  331.         (void) fprintf(stderr, "     -H : attach SCCS header\n");
  332. #endif
  333.     (void) fprintf(stderr, "     -N : no execute mode\n");
  334.     (void) fprintf(stderr, "     -R : recursive check %s\n", inout);
  335.     if (cii)
  336.     {
  337.         (void) fprintf(stderr, "     -T : create title file\n");
  338.         (void) fprintf(stderr, "     -U : update source directory\n");
  339.     }
  340.     (void) fprintf(stderr, "     -V : verbose mode\n");
  341. }
  342.  
  343. /*--------------------------------------------------- sigcleanup() ----------
  344. / cleanup before exiting. 
  345. /---------------------------------------------------------------------------*/
  346. void
  347. sigcleanup()
  348. {
  349.     (void) printf("\n[%s: Interrupted]\n", prognam);
  350.     if (title[0] && !usertitle)    /* remove title file, if here. */
  351.         (void) unlink(title);
  352.     if (editfile[0])        /* if a temp file might be here. */
  353.         (void) unlink(editfile);    /* attempt to remove it.*/
  354.     exit(0);
  355. }
  356.  
  357. /*--------------------------------------------------- doincmds() ------------
  358. / actual do routine
  359. /---------------------------------------------------------------------------*/
  360. void
  361. doincmds(argv, argc, in)
  362. char *argv[];
  363. int argc, in;
  364. {
  365.     register char *cp;
  366.     char entry[400];
  367.  
  368.     if (cii)
  369.     {
  370.         if (argc == in)        /* no arguments given */
  371.         {
  372.             do_ciodir(currentdir);
  373.             return /* void */;
  374.         }
  375.         for ( ; in < argc; in++)
  376.         {
  377.             (void) sprintf(entry, "%s/%s", currentdir, argv[in]);
  378. #ifdef DEBUG
  379.             (void) printf("Processing %s\n", entry);
  380. #endif
  381.             if (stat(entry, &filestat))
  382.             {
  383. #ifdef DEBUG
  384.                 (void) printf("Unable to stat(2) %s\n", entry);
  385. #endif
  386.                 continue;
  387.             }
  388.             do_unlink = (filestat.st_uid == real_user_id);
  389.             if ((filestat.st_mode & S_IFMT) == S_IFDIR)
  390.                 do_ciodir(entry);
  391.             else
  392.                 (void) cio(entry);
  393.         }
  394.     }
  395.     else /* coo */
  396.     {
  397.         char *ep;
  398.  
  399.         getrcsdir(entry, currentdir);
  400.         if (argc == in)
  401.         {
  402.             do_ciodir(entry);
  403.             return /* void */;
  404.         }
  405.         for (ep = entry; *ep; ep++)
  406.             ;
  407.         for ( ; in < argc; in++)
  408.         {
  409.             (void) sprintf(ep, "/%s", argv[in]);
  410. #ifdef DEBUG
  411.             (void) printf("Processing %s\n", entry);
  412. #endif
  413.             if (stat(entry, &filestat))
  414.             {
  415.                 if (!addrcsext(entry))
  416.                     continue;
  417.                 if (stat(entry, &filestat))
  418.                     continue;
  419.             }
  420.             do_unlink = (filestat.st_uid == real_user_id);
  421.             if ((filestat.st_mode & S_IFMT) == S_IFDIR)
  422.                 do_ciodir(entry);
  423.             else
  424.                 (void) cio(entry);
  425.         }
  426.             
  427.     }
  428.     return /* void */;
  429. }
  430.  
  431. /*--------------------------------------------------- do_ciodir() -----------
  432. / actual do routine - Warning: RECURSIVE
  433. /---------------------------------------------------------------------------*/
  434. void
  435. do_ciodir(dir)
  436. char *dir;
  437. {
  438.     FILE *pp;
  439.     char *cmd, *entry;
  440.  
  441.     (void) sprintf(cmd = memalloc(strlen(dir) + 4), "ls %s", dir);
  442.     pp = popen(cmd, "r");
  443.     free(cmd);
  444.     if (!pp)
  445.         return /* void */;
  446.     entry = memalloc(strlen(dir) + 30);
  447.     while (strrd(d_ent, 80, pp) != -1)
  448.     {
  449.         (void) sprintf(entry, "%s/%s", dir, d_ent);
  450.         if (stat(entry, &filestat))
  451.         {
  452. #ifdef DEBUG
  453.             (void) printf("Unable to stat(2) %s\n", entry);
  454. #endif
  455.             continue;
  456.         }
  457.         do_unlink = (filestat.st_uid == real_user_id);
  458.         if ((filestat.st_mode & S_IFMT) == S_IFDIR)
  459.         {
  460.             if (recurse)
  461.                 do_ciodir(entry);
  462.             else
  463.                 continue;
  464.         }
  465.         else
  466.             (void) cio(entry);
  467.     }
  468.     free(entry);
  469.     (void) pclose(pp);
  470.     return /* void */;
  471. }
  472.  
  473. /*--------------------------------------------------- cio() -----------------
  474. / do this to file, if we can.
  475. /---------------------------------------------------------------------------*/
  476. int
  477. cio(filename)
  478. char *filename;
  479. {
  480.     register char *cp, *st;        /* some char pointers */
  481.     int do_copy = FALSE;        /* do cp(1) */
  482.     char titlest[100];        /* for file names. */
  483.  
  484.     if (cii)
  485.     {
  486.         if (!asciifile(filename))    /* if not ascii file */
  487.         {
  488.             if (!allfiles)
  489.                 return(FALSE);    /* don't if not forced copy */
  490.             do_copy = TRUE;
  491.         }
  492.         else if (insertheader)        /* inserting default header */
  493.         {
  494.             inshdr(filename);
  495.         }
  496.     }
  497.     else if (!rcsfile(filename))        /* not an RCS file */
  498.     {
  499.         if (!allfiles)
  500.             return(FALSE);
  501.         do_copy = TRUE;            /* simply cp(1) it */
  502.     }
  503.  
  504.     if (cii && !do_copy && !logstr[0]) /* we don't have a log entry yet. */
  505.     {
  506.         if (noexec)
  507.         {
  508.             (void) printf("Logfile entry bypassed.\n");
  509.             (void) strcpy(logstr, " ");    /* fake it */
  510.         }
  511.         else if (!getfinput(titlest, TYPE_LOG)) /* if we entered anything */
  512.         {
  513.             FILE *fp;
  514.             char buf[100];
  515.             register char *bp;
  516.             register int numchars = 0;
  517.  
  518.             if (fp = fopen(titlest, "r"))
  519.             {
  520.                 st = logstr;
  521. #ifdef V_RCS
  522.                 (void) sprintf(st, " -m\"");
  523. #else
  524.                 (void) sprintf(st, " -y\"");
  525. #endif
  526.                 st += strlen(st); /* skip to end of string. */
  527.                 while (strrd(buf, 80, fp) != -1)
  528.                 {    /*
  529.                      * escape quotes (") characters
  530.                      */
  531.                     bp = buf;
  532.                     while (*bp)
  533.                     {
  534.                         if (*bp == '"')
  535.                             if (numchars < MAX_LOG)
  536.                             {
  537.                                 numchars++;
  538.                                 *st++ = '\\'; /*escape*/
  539.                             }
  540.                         if (numchars < MAX_LOG)
  541.                         {
  542.                             numchars++;
  543.                             *st++ = *bp++; /* append */
  544.                         }
  545.                     }
  546.                     *st++ = '\n'; /* add end of line now. */
  547.                 }
  548.                 if (*(st - 1) == '\n')
  549.                     *(st - 1) = '"';
  550.                 else
  551.                     *st++ = '"';
  552.                 *st = '\0';
  553.                 (void) fclose(fp);
  554.                 if (numchars >= MAX_LOG)
  555.                     (void) printf("Log entry truncated.\n");
  556.             }
  557.             (void) unlink(titlest); /* cleanup. */
  558.         }
  559.     }
  560.  
  561.     titlest[0] = '\0'; /* cleanup string reference. */
  562.     if (cii)
  563.         getrcsdir(final, filename);
  564.     else
  565.         getworkdir(final, filename);
  566.  
  567.     if (st = strrchr(final, '/'))    /* if this has a sub dir. */
  568.     {
  569.         *st = '\0';    /* terminate it at the directoy level. */
  570.         if (access(final, 0))    /* it's not here... */
  571.             if (!makedir(final))    /* so try and make it. */
  572.             {
  573.                 (void) printf("Could not create directory :%s:\n", final);
  574.                 return(FALSE);
  575.             }
  576.         *st = '/';    /* restore the rest of the file name. */
  577.     }
  578.  
  579.     if (cii)
  580.     {
  581.         if (!do_copy)    /* not in binary mode. */
  582.         {    /*
  583.              * make the new file name and check if it's here
  584.              */
  585. #ifdef V_SCCS
  586.             (void) strcpy(finalfile, final);
  587. #endif
  588.             (void) addrcsext(final);
  589.             if (!noexec && access(final, 0))
  590.             {
  591.                 /*
  592.                  * if not, need to get a title message for it
  593.                  */
  594.                 if (!title[0]) /* no title file yet. */
  595.                     (void) getfinput(title, TYPE_TITLE); /* get one */
  596.                 if (title[0])  /* if we've one, build command */
  597.                     (void) sprintf(titlest, " -t%s ", title);
  598. #ifdef V_SCCS
  599.                 do_admin = TRUE;    /* 1st check-in */
  600. #endif
  601.             }
  602. #ifdef V_SCCS
  603.             else
  604.                 do_admin = FALSE;
  605. #endif
  606.         }
  607.         else if (noexec)
  608.             (void) strcpy(titlest, " -tfile_name ");
  609.     }
  610.     else    /* check out mode. */
  611.     {    /*
  612.          * find a comma to make the new name.
  613.          * if found one, strip it to make a new name.
  614.          */
  615.         (void) rmrcsext(final);
  616.     }
  617.  
  618.     /*
  619.      * Build command string
  620.      */
  621.     if (do_copy)    /* we want to just copy the file now. */
  622.     {
  623.         if (!interactive && !noexec && !access(final, 0))
  624.         {
  625.             (void) printf("%s already exists.  Overwrite? (yes) ", final);
  626.             (void) strrd(cmdbuf, 20, stdin);
  627.             (void) strlwr(cmdbuf);
  628.             if (strncmp(cmdbuf, "yes", strlen(cmdbuf)))
  629.                 return(TRUE);
  630.         }
  631.         (void) sprintf(cmdbuf, "cp %s %s", filename, final); /* copy command */
  632.     }
  633.     else if (cii)
  634.     {
  635. #ifdef V_RCS
  636.         (void) sprintf(cmdbuf, "%s%s%s%s%s %s %s",
  637.             ci_cmd, cioopt, titlest, logstr,
  638.             do_unlink ? "" : " -l", filename, final);
  639. #else
  640.         st = cmdbuf;
  641.         (void) sprintf(st, "cp %s %s; ", filename, finalfile);
  642.         st += strlen(st); /* skip to end of string. */
  643.         if (cp = strrchr(final, '/'))
  644.         {
  645.             *cp = '\0';
  646.             (void) sprintf(st, "cd %s; ", final);
  647.             st += strlen(st); /* skip to end of string. */
  648.             *cp = '/';
  649.         }
  650.         if (do_admin)
  651.         {
  652.             (void) sprintf(st, "%s -i%s%s%s%s %s",
  653.                 ici_cmd, justname(finalfile),
  654.                 cioopt, titlest, logstr, justname(final));
  655.             st += strlen(st); /* skip to end of string. */
  656.         }
  657.         else
  658.         {
  659.             (void) sprintf(st, "%s%s%s %s",
  660.                 ci_cmd, cioopt, logstr, justname(final));
  661.             st += strlen(st); /* skip to end of string. */
  662.         }
  663.         if (do_unlink)
  664.         {
  665.             (void) sprintf(st, "; rm %s", filename);
  666.             st += strlen(st); /* skip to end of string. */
  667.         }
  668. #endif
  669.     }
  670.     else    /* coo */
  671.     {
  672. #ifdef V_RCS
  673.         (void) sprintf(cmdbuf, "%s%s %s %s",
  674.                 co_cmd, cioopt, filename, final);
  675. #else
  676.         st = cmdbuf;
  677.         (void) sprintf(st, "rm -f %s; ", final);
  678.         st += strlen(st); /* skip to end of string. */
  679.         if (cp = strrchr(filename, '/'))
  680.         {
  681.             *cp = '\0';
  682.             (void) sprintf(st, "cd %s; ", filename);
  683.             st += strlen(st); /* skip to end of string. */
  684.             *cp = '/';
  685.         }
  686.         addrcsext(filename);
  687.         (void) sprintf(st, "%s%s %s", co_cmd, cioopt,
  688.             justname(filename));
  689.         st += strlen(st); /* skip to end of string. */
  690.         if (cp = strrchr(final, '/'))
  691.         {
  692.             cp++;
  693.             (void) sprintf(st, "; mv %s %s", cp, final);
  694.             st += strlen(st); /* skip to end of string. */
  695.         }
  696. #endif
  697.     }
  698.  
  699. #ifdef INTERACTIVE
  700.     if (interactive)
  701.     {
  702.         char ans[20];
  703.         int done = FALSE;
  704.  
  705.         do
  706.         {
  707.             (void) printf("%s\nExecute? (yes) ", cmdbuf);
  708.             (void) strrd(ans, 20, stdin);
  709.             st = ans;
  710.             while (isspace(*st))
  711.                 st++;
  712.             if (!*st)
  713.                 (void) strcpy(st, "yes");
  714.             (void) strlwr(st);
  715.             if (!strncmp(st, "yes", strlen(st))) {
  716.                 done = TRUE;
  717.                 (void) system(cmdbuf);    /* do it. */
  718.             }
  719.             else if (*st == '?')
  720.             {
  721.                 (void) printf("\n");
  722.                 (void) printf("yes     Do it\n");
  723.                 (void) printf("no      Don't do it\n");
  724.                 (void) printf("view    View current file\n");
  725.                 (void) printf("?       Print this message\n");
  726.                 (void) printf("\n");
  727.             }
  728.             else if (!strncmp(st, "view", strlen(st)))
  729.             {
  730.                 (void) printf("Not implemented yet!\n\n");
  731.             }
  732.             else /* take it as "no" */
  733.                 done = TRUE;
  734.         } while (!done);
  735.     }
  736.     else
  737.     {
  738. #endif /* INTERACTIVE */
  739.         if (verbose)
  740.             (void) printf("%s command :%s:\n", prognam, cmdbuf);
  741.         if (!noexec)
  742.         {
  743.             if (do_copy && !verbose)
  744.                 (void) printf("%s\n", cmdbuf);
  745.             (void) system(cmdbuf);    /* do it. */
  746.         }
  747.         else if (!verbose)    /* if don't want to exec, don't */
  748.             (void) printf("%s\n", cmdbuf);
  749. #ifdef INTERACTIVE
  750.     }
  751. #endif /* INTERACTIVE */
  752.     if (updsrcdir)            /* update source directory */
  753.     {
  754.         getsrcdir(final, filename);
  755.         (void) sprintf(cmdbuf, "cp %s %s", filename, final);
  756.         if (noexec)        /* don't actual do it */
  757.             (void) printf("%s\n", cmdbuf);
  758.         else
  759.         {
  760.             if (verbose)    /* speak, yo wise one! */
  761.                 (void) printf("%s command :%s:\n", prognam, cmdbuf);
  762.             if (!strcmp(filename, final))
  763.                 (void) printf("Source and destination identical.  Not updated.\n");
  764.             else
  765.             {
  766.                 (void) chmod(final, 0640);
  767.                 (void) system(cmdbuf);    /* do it! */
  768.             }
  769.         }
  770.     }
  771.     return(TRUE);
  772. }
  773.  
  774. /*--------------------------------------------------- addrcsext() -----------
  775. / add RCS file extension ",v" if there isn't one already
  776. /---------------------------------------------------------------------------*/
  777. int
  778. addrcsext(fname)
  779. char *fname;
  780. {
  781.     register char *cp;
  782.  
  783. #ifdef V_RCS
  784.     for (cp = fname; *cp; cp++)
  785.         ;
  786.     if (*--cp == 'v' && *(cp - 1) == ',')
  787.         return(0);        /* already there */
  788.  
  789.     *++cp = ',';            /* add ",v" */
  790.     *++cp = 'v';
  791.     *++cp = '\0';
  792. #else
  793.     char t_name[20];
  794.  
  795.     cp = justname(fname);
  796.     if (*cp == 's' && *(cp + 1) == '.')
  797.         return(0);
  798.     (void) strcpy(t_name, cp);
  799.     *cp++ = 's';            /* add "s." in front of file name */
  800.     *cp++ = '.';
  801.     (void) strcpy(cp, t_name);
  802. #endif
  803.     return(1);
  804. }
  805.  
  806. /*--------------------------------------------------- rmrcsext() ------------
  807. / remove RCS extension if there is one; returns 1 if remove, else 0
  808. /---------------------------------------------------------------------------*/
  809. int
  810. rmrcsext(fname)
  811. char *fname;
  812. {
  813.     register char *cp;
  814.  
  815. #ifdef V_RCS
  816.     for (cp = fname; *cp; cp++)
  817.         ;
  818.     if (*--cp == 'v' && *--cp == ',')
  819.     {
  820.         *cp = '\0';
  821.         return(1);
  822.     }
  823. #else
  824.     cp = justname(fname);
  825.     if (*cp == 's' && *(cp + 1) == '.')
  826.     {
  827.         (void) strcpy(cp, cp + 2);
  828.         return(1);
  829.     }
  830. #endif
  831.     return(0);
  832. }
  833.  
  834. /*--------------------------------------------------- inshdr() --------------
  835. / insert RCS header if none exists already
  836. /---------------------------------------------------------------------------*/
  837. void
  838. inshdr(t_name)
  839. char *t_name;
  840. {
  841. #    define FTYPE_C        0    /* C program text */
  842. #    define FTYPE_S        1    /* assembly program text */
  843. #    define FTYPE_SH        2    /* shell script */
  844. #    define FTYPE_ROFF    3    /* nroff, tbl, eqn, etc */
  845. #    define FTYPE_F        4    /* Fortran program text */
  846. #    define FTYPE_DEFAULT    5    /* don't know */
  847. #    define FTYPE_MK        6    /* makefile script */
  848. #    define FTYPE_H        7    /* C header file text */
  849.  
  850.     static struct _ftype {
  851.         char *keyword;        /* phrase may exist in file(1) output */
  852.         char *header;        /* header template file name. */
  853.     } ftype[] = {
  854. #ifdef V_RCS
  855.         { "c program",    ".rcshead.c"    },    /* FTYPE_C */
  856.         { "assembler",    ".rcshead.s"    },    /* FTYPE_S */
  857.         { "command",    ".rcshead.sh"    },    /* FTYPE_SH */
  858.         { "roff, tbl",    ".rcshead.roff"    },    /* FTYPE_ROFF */
  859.         { "fortran",    ".rcshead.f"    },    /* FTYPE_F */
  860.         { 0,        ".rcshead"    },    /* FTYPE_DEFAULT */
  861.         { 0,        ".rcshead.mk"    },    /* FTYPE_MK */
  862.         { 0,        ".rcshead.h"    } };    /* FTYPE_H */
  863. #else
  864.         { "c program",    ".sccshead.c"    },    /* FTYPE_C */
  865.         { "assembler",    ".sccshead.s"    },    /* FTYPE_S */
  866.         { "command",    ".sccshead.sh"    },    /* FTYPE_SH */
  867.         { "roff, tbl",    ".sccshead.roff"},    /* FTYPE_ROFF */
  868.         { "fortran",    ".sccshead.f"    },    /* FTYPE_F */
  869.         { 0,        ".sccshead"    },    /* FTYPE_DEFAULT */
  870.         { 0,        ".sccshead.mk"    },    /* FTYPE_MK */
  871.         { 0,        ".sccshead.h"    } };    /* FTYPE_H */
  872. #endif /* V_RCS */
  873.     static struct _fext {
  874.         char *name;
  875.         int type;
  876.     } fext[] = {
  877.         { ".c",        FTYPE_C        },
  878.         { ".h",        FTYPE_H        },
  879.         { ".s",        FTYPE_S        },
  880.         { ".f",        FTYPE_F        },
  881.         { ".man",    FTYPE_ROFF    },
  882.         { ".mk",    FTYPE_MK    },
  883.         { ".1",        FTYPE_ROFF    },
  884.         { ".2",        FTYPE_ROFF    },
  885.         { ".3",        FTYPE_ROFF    },
  886.         { ".4",        FTYPE_ROFF    },
  887.         { ".5",        FTYPE_ROFF    },
  888.         { ".6",        FTYPE_ROFF    },
  889.         { ".7",        FTYPE_ROFF    },
  890.         { ".8",        FTYPE_ROFF    },
  891.         { ".9",        FTYPE_ROFF    },
  892.         { 0,        0        } };
  893.     FILE *ifp, *ofp;
  894.     char buf[4096], headfile[128], tempfile[20], fname[40];
  895.     register int i, c, ft = FTYPE_DEFAULT, err=0;
  896.     register char *ext;
  897.  
  898.     strcpy(fname, justname(t_name)); /* copy over only name. */
  899.  
  900.     if (!(ifp = fopen(t_name, "r")))    /* quickly check for RCS header */
  901.         return;
  902.                     /* we are looking for "$Header" */
  903.                     /* within first 50 lines of the file */
  904.     for (i = 0; strrd(buf, 128, ifp) > 0 && i < 50; i++)
  905. #ifdef V_RCS
  906.         if (strstr(buf, "$Header"))
  907. #else
  908.         if (strstr(buf, "#ident"))
  909. #endif
  910.         {
  911.             (void) fclose(ifp);
  912.             if (verbose)
  913. #ifdef V_RCS
  914.                 (void) printf("%s already has a RCS header.\n",
  915. #else
  916.                 (void) printf("%s already has a SCCS header.\n",
  917. #endif /* V_RCS */
  918.                         t_name);
  919.             return;
  920.         }
  921.  
  922.     (void) fclose(ifp);
  923.                     /* examine file(1) output */
  924.     for (i = 0; ftype[i].keyword; i++)
  925.         if (strstr(ftypestr, ftype[i].keyword))
  926.         {
  927.             ft = i;
  928.             break;        /* found one */
  929.         }
  930.  
  931.     if (!ftype[i].keyword)        /* file(1) didn't help */
  932.     {                /* examine file extension */
  933.         if (ext = strrchr(fname, '.'))
  934.         {
  935.             for (i = 0; fext[i].name; i++)
  936.                 if (!strcmp(ext, fext[i].name))
  937.                     ft = fext[i].type;
  938.         }
  939.         else
  940.         {            /* check if makefile script */
  941.             (void) strcpy(buf, fname);
  942.             (void) strlwr(buf);
  943.             if (!strcmp(buf, "makefile") || !strcmp(buf, "Makefile"))
  944.                 ft = FTYPE_MK; /* If either of two fixed names.. */
  945.         }
  946.     }
  947.     else if (ft == FTYPE_C)        /* see if source or header */
  948.     {
  949.         if ((ext = strrchr(fname, '.')) && !strcmp(ext, ".h"))
  950.             ft = FTYPE_H;
  951.     }
  952.     if (verbose)            /* is this necessary */
  953.         (void) printf("%s is type [%d]\n", fname, ft);
  954.     if (noexec)            /* no execution mode */
  955.         return;
  956.  
  957.     (void) sprintf(headfile, "%s%s", headdir, ftype[ft].header);
  958.     if (!(ifp = fopen(headfile, "r")))
  959.     {
  960.         (void) printf("Unable to open header template file [%s]\n",
  961.                 headfile);
  962.         return;
  963.     }
  964.     /* build a tmp file in the same directory as old file. */
  965.     strcpy(tempfile, t_name);
  966.     ext = justname(tempfile); /* find end of path. */
  967.     *ext = '\0'; /* and terminate path there. */
  968.     (void) strcat(tempfile, "ciotmp._XXXXXX"); /* add tmp name */
  969.     (void) mktemp(tempfile);        /* generate temp file name */
  970.     if (!(ofp = fopen(tempfile, "w")))    /* open temp file */
  971.     {
  972.         (void) printf("Unable to open temporary file [%s]\n", tempfile);
  973.         (void) fclose(ifp);
  974.         return;
  975.     }
  976.     while ((c = fgetc(ifp)) != EOF)        /* copy header first */
  977.         (void) fputc(c, ofp);
  978.     (void) fclose(ifp);
  979.     if (!(ifp = fopen(t_name, "r")))        /* open check-in file */
  980.     {
  981.         (void) printf("Unable to open [%s] for read\n", t_name);
  982.         (void) fclose(ofp);
  983.         (void) unlink(tempfile);
  984.         return;
  985.     }
  986.     while ((c = fgetc(ifp)) != EOF)        /* append to temp file */
  987.         if(fputc(c, ofp) == EOF)
  988.             err=1; /* couldn't write error. */
  989.     (void) fclose(ifp);            /* done */
  990.     (void) fclose(ofp);
  991.  
  992. /* ok.  It's hard to make sure that everthing has gone well; if we unlink
  993.    the src file and can't link the temp file, we could lose everthing.
  994.    So, if copy fails, leave temp file alone, as it may be the only copy
  995.    we have left!  If the unlinking the original fails, we can remove the
  996.    copy, as we don't need it.
  997. */
  998.     if(!err && !unlink(t_name))    /* 'mv tempfile fname' */
  999.     {
  1000.         if(!link(tempfile, t_name)) /* 'cp tempfile t_name' */
  1001.             (void) unlink(tempfile);        /* 'rm tempfile' */
  1002.         else
  1003.             (void) printf("Link of %s and %s failed after removing %s.\n%s not removed.\n",
  1004.                 tempfile, t_name, t_name, tempfile);
  1005.     }
  1006.     else
  1007.     {
  1008.         (void) unlink(tempfile);        /* 'rm tempfile' */
  1009.         (void) printf("Could not insert header into %s.  Copy failed.\n", t_name);
  1010.     }
  1011. }
  1012.  
  1013. /*--------------------------------------------------- makedir() -------------
  1014. / make a directory path, with recursion.
  1015. / returns TRUE if successful, FALSE otherwise.
  1016. /
  1017. / This really needs to be re-written.  It works, but that's all I can really
  1018. / say for it...  Hey, *I* don't have mkdir() calls!
  1019. /---------------------------------------------------------------------------*/
  1020. int
  1021. makedir(newpath)
  1022. char *newpath;        /* path name to make. */
  1023. {
  1024.     register char *st, *cp;
  1025.  
  1026.     if(!*newpath) return(FALSE); /* skip last directory attempt. */
  1027.     cp = memalloc(strlen(newpath) + 24);
  1028.     (void) sprintf(cp, "/bin/mkdir %s 2>/dev/null", newpath);
  1029.     if(verbose)
  1030.         (void) printf("calling mkdir: %s\n", cp);
  1031.     if (noexec)
  1032.     {
  1033.         (void) printf("%s\n", cp);
  1034.         free(cp);
  1035.         return(TRUE);
  1036.     }
  1037.     if (system(cp))            /* it failed.. */
  1038.     {
  1039.         (void) strcpy(cp, newpath);    /* get current one. */
  1040.         st = strrchr(cp, '/'); /* remove one more layer.. */
  1041.         *st = '\0';        /* terminate here. */
  1042.         if (makedir(cp) == FALSE)    /* try and build next level back. */
  1043.         {
  1044.             free(cp);
  1045.             return(FALSE);
  1046.         } /* ok, so.. it passed on back. Try this again. */
  1047.         else if(makedir(newpath) == FALSE)
  1048.         {
  1049.             free(cp);
  1050.             return(FALSE);
  1051.         }
  1052.     }
  1053.     free(cp);
  1054.     return(TRUE);
  1055. }
  1056.  
  1057. /*--------------------------------------------------- strstr() --------------
  1058. / find a substring within a string
  1059. /---------------------------------------------------------------------------*/
  1060. char *
  1061. strstr(s1, s2)
  1062. register char *s1, *s2;
  1063. {
  1064.     register int l;
  1065.  
  1066.     if (l = strlen(s2))
  1067.         for ( ; s1 = strchr(s1, s2[0]); s1++)
  1068.             if (memcmp(s1, s2, l) == 0)
  1069.                 break;
  1070.     return(s1);
  1071. }
  1072.  
  1073. /*--------------------------------------------------- strlwr() ---------------
  1074. / strlwr.c - convert passed string to its equivalent lowercases
  1075. /----------------------------------------------------------------------------*/
  1076. char *
  1077. strlwr(s)
  1078. register char *s;
  1079. {
  1080.     char *op;
  1081.  
  1082.     for (op = s; *s; s++)
  1083.         if (isupper(*s))
  1084.             *s = _tolower(*s);
  1085.     return(op);
  1086. }
  1087.  
  1088. /*--------------------------------------------------- asciifile() -----------
  1089. / check if passed file is an ascii file using file(1) command
  1090. /---------------------------------------------------------------------------*/
  1091. int
  1092. asciifile(fn)
  1093. char *fn;
  1094. {
  1095.     char cmdstr[256];
  1096.     register FILE *fp;
  1097.  
  1098.     (void) sprintf(cmdstr, "file %s", fn);
  1099.     if (!(fp = popen(cmdstr, "r")))
  1100.         return(FALSE);
  1101.     (void) strrd(ftypestr, 80, fp); /* get a line. */
  1102.     (void) pclose(fp);    /* and done. */
  1103. #ifdef DEBUG
  1104.     (void) printf("%s\n", cmdstr);
  1105. #endif
  1106.     if (strstr(ftypestr, "text"))
  1107.         return(TRUE);
  1108.     return(FALSE);
  1109. }
  1110.  
  1111. /*--------------------------------------------------- rcsfile() -------------
  1112. / check if passed file is an RCS file using file(1) command
  1113. /---------------------------------------------------------------------------*/
  1114. int
  1115. rcsfile(fn)
  1116. char *fn;
  1117. {
  1118.     char cmdstr[256];
  1119.     register FILE *fp;
  1120.  
  1121.     (void) sprintf(cmdstr, "file %s", fn);
  1122.     if (!(fp = popen(cmdstr, "r")))
  1123.         return(FALSE);
  1124.     (void) strrd(ftypestr, 80, fp); /* get a line. */
  1125.     (void) pclose(fp);    /* and done. */
  1126. #ifdef DEBUG
  1127.     (void) printf("%s\n", cmdstr);
  1128. #endif
  1129. #ifdef V_RCS
  1130.     if (strstr(ftypestr, "text"))
  1131. #else
  1132.     if (strstr(ftypestr, "sccs"))
  1133. #endif
  1134.         return(TRUE);
  1135.     return(FALSE);
  1136. }
  1137.  
  1138. /*--------------------------------------------------- strrd() ----------------
  1139. / read from given file pointer until a line separator or end-of-file or
  1140. / (len) characters excluding the terminator.
  1141. /---------------------------------------------------------------------------*/
  1142. int
  1143. strrd(buf, len, fle)
  1144. char *buf;
  1145. int len;
  1146. FILE *fle;
  1147. {
  1148.     int c0, i0 = 0;
  1149.  
  1150.     while (((c0 = getc(fle)) != EOF) && c0 && c0 != '\n')
  1151.         if(i0 < len)  /* if room in buffer..*/
  1152.             buf[i0++] = (char) c0;  /* save it. */
  1153.     buf[i0] = 0;
  1154.     if (i0 == 0 && c0 == EOF)
  1155.         return(-1);
  1156.     return(i0);
  1157. }
  1158.  
  1159. /*--------------------------------------------------- getdir() --------------    
  1160. / get and readin variables for later.
  1161. /---------------------------------------------------------------------------*/
  1162. void
  1163. getdir()
  1164. {
  1165.     register char *cp;
  1166.  
  1167.     if(cp = getenv("HOME"))        /* get user's home dir. */
  1168.     {
  1169.         (void) strcpy(homedir = memalloc(strlen(cp) + 2), cp);
  1170.         if (!(s_homedir = fix_envstr(homedir)))
  1171.         {
  1172.             free(homedir);
  1173.             homedir = (char *) 0;
  1174.         }
  1175.     }
  1176.     else                /* this should NEVER happen */
  1177.     {
  1178.         (void) fprintf(stderr, "No home directory???\n");
  1179.         exit(-1);
  1180.     }
  1181.  
  1182. #ifdef V_RCS
  1183.     if(cp = getenv("RCSDIR"))    /* RCS directory */
  1184. #else
  1185.     if(cp = getenv("SCCSDIR"))    /* SCCS directory */
  1186. #endif
  1187.         (void) strcpy(rcsdir = memalloc(strlen(cp) + 2), cp);
  1188.     else                /* RCS is $HOME/RCS */
  1189.         (void) sprintf(rcsdir = memalloc(s_homedir + 6),
  1190. #ifdef V_RCS
  1191.                 "%s/RCS", homedir);
  1192. #else
  1193.                 "%s/SCCS", homedir);
  1194. #endif
  1195.     if (!(s_rcsdir = fix_envstr(rcsdir)))
  1196.     {
  1197.         free(rcsdir);
  1198.         rcsdir = (char *) 0;
  1199.     }
  1200.  
  1201. #ifdef V_RCS
  1202.     if(cp = getenv("RCSWORK"))    /* user's working directory */
  1203. #else
  1204.     if(cp = getenv("SCCSWORK"))    /* user's working directory */
  1205. #endif
  1206.     {
  1207.         (void) strcpy(rcswrk = memalloc(strlen(cp) + 2), cp);
  1208.         if (!(s_rcswrk = fix_envstr(rcswrk)))
  1209.         {
  1210.             free(rcswrk);
  1211.             rcswrk = (char *) 0;
  1212.         }
  1213.     }
  1214.  
  1215. #ifdef V_RCS
  1216.     if (cp = getenv("RCSSRC"))    /* master source directory */
  1217. #else
  1218.     if (cp = getenv("SCCSSRC"))    /* master source directory */
  1219. #endif
  1220.     {
  1221.         (void) strcpy(srcdir = memalloc(strlen(cp) + 2), cp);
  1222.         if (!(s_srcdir = fix_envstr(srcdir)))
  1223.         {
  1224.             free(srcdir);
  1225.             srcdir = (char *) 0;
  1226.         }
  1227.     }
  1228.  
  1229. #ifdef V_RCS
  1230.     if (cp = getenv("RCSHEAD"))    /* RCS header file directory */
  1231. #else
  1232.     if (cp = getenv("SCCSHEAD"))    /* SCCS header file directory */
  1233. #endif
  1234.     {
  1235.         (void) strcpy(headdir = memalloc(strlen(cp) + 2), cp);
  1236.         if (!(s_headdir = fix_envstr(headdir)))
  1237.         {
  1238.             free(headdir);
  1239.             headdir = homedir;
  1240.         }
  1241.     }
  1242.     else
  1243.         headdir = homedir;
  1244.  
  1245. #ifdef V_RCS
  1246.     if (cp = getenv("RCSOWN"))    /* the owner of RCS files */
  1247. #else
  1248.     if (cp = getenv("SCCSOWN"))    /* the owner of SCCS files */
  1249. #endif
  1250.     {
  1251.         s_rcsown = strlen(cp);
  1252.         (void) strcpy(rcsown = memalloc(s_rcsown + 1), cp);
  1253.     }
  1254.  
  1255.     if(cp = getenv("PATH"))        /* current path, ie. $PATH */
  1256.     {
  1257.         s_path = strlen(cp);
  1258.         (void) strcpy(path = memalloc(s_path + 1), cp);
  1259.     }
  1260.  
  1261.     if((currentdir = getcwd((char *)NULL, 200)) == NULL)
  1262.     {
  1263.         (void) fprintf(stderr, "Cannot get working dir.\n");
  1264.         exit(-1);
  1265.     }
  1266.     s_currentdir = strlen(currentdir);
  1267. }
  1268.  
  1269. /*--------------------------------------------------- fix_envstr() ----------
  1270. / fix environment variable to avoid problems later.
  1271. / 1. strip leading/trailing white spaces
  1272. / 2. strip duplicate slashes
  1273. / 3. add trailing slash
  1274. / 4. return string length
  1275. /---------------------------------------------------------------------------*/
  1276. int
  1277. fix_envstr(cs)
  1278. char *cs;
  1279. {
  1280.     register char *cp, *dp;
  1281.     register int was_slash = FALSE;
  1282.  
  1283.     cp = dp = cs;
  1284.     while (isspace(*cp))        /* remove leading white spaces */
  1285.         cp++;
  1286.  
  1287.     if (!*cp)            /* string was a full of blanks */
  1288.         return(0);
  1289.  
  1290.     while (*cp)
  1291.     {
  1292.         if (*cp == '/')
  1293.         {
  1294.             if (was_slash)
  1295.             {
  1296.                 cp++;    /* strip duplicate slashes */
  1297.                 continue;
  1298.             }
  1299.             else
  1300.                 was_slash = TRUE;
  1301.         }
  1302.         else
  1303.             was_slash = FALSE;
  1304.         *dp++ = *cp++;
  1305.     }
  1306.  
  1307.     do                /* remove trailing while spaces */
  1308.     {
  1309.         dp--;
  1310.     } while (isspace(*dp));
  1311.  
  1312.     if (*dp != '/')            /* add trailing slash */
  1313.         *++dp = '/';
  1314.     *++dp = '\0';            /* null terminate */
  1315.     return(strlen(cs));
  1316. }
  1317.  
  1318. /*--------------------------------------------------- getrcsdir() -----------
  1319. / get $RCSDIR + tail directory
  1320. /---------------------------------------------------------------------------*/
  1321. void
  1322. getrcsdir(tdir, sdir)
  1323. char *tdir, *sdir;
  1324. {
  1325.     register char *cp = sdir;
  1326.  
  1327.     if(rcswrk && !strncmp(rcswrk, cp, s_rcswrk))
  1328.         cp += s_rcswrk;
  1329.     if(homedir && !strncmp(homedir, cp, s_homedir))
  1330.         cp += s_homedir;
  1331.     /*
  1332.      * build the final directory name
  1333.      */
  1334.     (void) sprintf(tdir, "%s%s", rcsdir, cp);
  1335. }
  1336.  
  1337. /*--------------------------------------------------- getworkdir() ----------
  1338. / get $RCSWORK or $HOME + tail
  1339. /---------------------------------------------------------------------------*/
  1340. void
  1341. getworkdir(tdir, sdir)
  1342. char *tdir, *sdir;
  1343. {
  1344.     register char *cp = sdir;
  1345.  
  1346.     if (rcsdir && !strncmp(rcsdir, cp, s_rcsdir))
  1347.         cp += s_rcsdir;
  1348.     (void) sprintf(tdir, "%s%s", rcswrk ? rcswrk : homedir, cp);
  1349. }
  1350.  
  1351. /*--------------------------------------------------- getsrcdir() -----------
  1352. / get $RCSSRC + tail
  1353. /---------------------------------------------------------------------------*/
  1354. void
  1355. getsrcdir(tdir, sdir)
  1356. char *tdir, *sdir;
  1357. {
  1358.     register char *cp = sdir;
  1359.  
  1360.     if (rcsdir && !strncmp(rcsdir, cp, s_rcsdir))
  1361.         cp += s_rcsdir;
  1362.     if(homedir && !strncmp(homedir, cp, s_homedir))
  1363.         cp += s_homedir;
  1364.     (void) sprintf(tdir, "%s%s", srcdir ? srcdir : homedir, cp);
  1365. }
  1366.  
  1367. /*--------------------------------------------------- getfinput() ------------
  1368. / get a title file.
  1369. /---------------------------------------------------------------------------*/
  1370. int
  1371. getfinput(name, type)
  1372. char *name;        /* buffer to put file name into. */
  1373. int type;        /* what data we want. */
  1374. {
  1375.     int stat_loc;
  1376.  
  1377.     (void) strcpy(name, tmpnam(editfile));
  1378.     if (fork())
  1379.     {    /* parent just waits for the child to finish */
  1380.         (void) wait(&stat_loc);
  1381.     }
  1382.     else
  1383.     {    /* child does his/her stuff */
  1384.         (void) signal(SIGINT, SIG_DFL);
  1385.         (void) signal(SIGQUIT, SIG_DFL);
  1386.         exit(child_getfinput(name, type) == TRUE ? 0 : -1);
  1387.     }
  1388.     return((stat_loc >> 8) & 0xff);
  1389. }
  1390.  
  1391. /*--------------------------------------------------- child_getfinput() -----
  1392. / actual get title file.
  1393. /---------------------------------------------------------------------------*/
  1394. static int
  1395. child_getfinput(name, type)
  1396. char *name;        /* buffer to put file name into. */
  1397. int type;        /* what data we want. */
  1398. {
  1399.     static char *input_type[2] = { "log", "title" };
  1400.     FILE *fp, *xfp;
  1401.     register char *st;
  1402.     int c, done = FALSE;
  1403.     char buf[82];            /* just larger than input buffer. */
  1404.  
  1405.     (void) setuid(real_user_id);        /* user's real user id */
  1406.     if((fp = fopen(name, "w")) == NULL) /* failed open. */
  1407.     {
  1408.         (void) unlink(name);        /* remove it. */
  1409.         name[0] = '\0';
  1410.         (void) printf("Unable to create tmp file.\n");
  1411.         return(FALSE);
  1412.     }
  1413.     (void) printf("Enter %s message, <ret>.<ret> or Control-D to end:\n",
  1414.         input_type[type]);
  1415.     while (!done)
  1416.     {
  1417.         (void) printf(">>");
  1418.         if(strrd(buf, 80, stdin) == -1) /* read in one line. */
  1419.         {
  1420.             /* ok, read somewhere that this is possible.  By doing this,
  1421.                we should be able to continue after a control-D.
  1422.             */
  1423.             clearerr(stdin);
  1424.             break;
  1425.         }
  1426.         if(!strcmp(".", buf))    /* end of message */
  1427.             break;
  1428.         if (buf[0] == '~')    /* special command */
  1429.         {
  1430.             switch (buf[1])    /* command character */
  1431.             {
  1432.             case '?':    /* print usage, help message */
  1433.                 (void) printf("\n");
  1434.                 (void) printf("~.    End of input\n");
  1435.                 (void) printf("~!    Invoke shell\n");
  1436.                 (void) printf("~e    Edit message using an editor\n");
  1437.                 (void) printf("~p    Print message buffer\n");
  1438.                 (void) printf("~r    Read in a file\n");
  1439.                 (void) printf("~w    Write message to a file\n");
  1440.                 (void) printf("~?    Print this message\n");
  1441.                 (void) printf("\n");
  1442.                 break;
  1443.             case '!':    /* shell */
  1444.                 st = getenv("SHELL");
  1445.                 (void) system(st ? st : "/bin/sh");
  1446.                 (void) printf("[Press RETURN to continue]");
  1447.                 (void) strrd(buf, 20, stdin);
  1448.                 break;
  1449.             case 'p':    /* print message buffer content */
  1450.                 (void) fclose(fp);
  1451.                 fp = fopen(name, "r");
  1452.                 while ((c = fgetc(fp)) != EOF)
  1453.                     (void) fputc(c, stdout);
  1454.                 (void) fclose(fp);
  1455.                 fp = fopen(name, "a");
  1456.                 (void) printf("Continue entering %s message.\n",
  1457.                     input_type[type]);
  1458.                 break;
  1459.             case 'e':    /* editor */
  1460.             case 'v':    /* visual */
  1461.                 (void) fclose(fp);
  1462.                 st = getenv(buf[1] == 'e' ?"EDITOR":"VISUAL");
  1463.                 (void) sprintf(buf, "%s %s",
  1464.                     st ? st : "/usr/bin/vi", name);
  1465.                 (void) system(buf);
  1466.                 fp = fopen(name, "a");
  1467.                 (void) printf("Continue entering %s message.\n",
  1468.                     input_type[type]);
  1469.                 break;
  1470.             case 'r':    /* read in a file */
  1471.                 st = &buf[2];
  1472.                 while (isspace(*st))
  1473.                     st++;
  1474.                 if (!*st)
  1475.                 {
  1476.                     (void) printf("File name missing!\n");
  1477.                     break;
  1478.                 }
  1479.                 if (xfp = fopen(st, "r"))
  1480.                 {
  1481.                     while ((c = fgetc(xfp)) != EOF)
  1482.                         (void) fputc(c, fp);
  1483.                     (void) fclose(xfp);
  1484.                 }
  1485.                 else
  1486.                     (void) printf("Unable to open %s.\n",
  1487.                             st);
  1488.                 break;
  1489.             case 'w':    /* write message to a file */
  1490.                 st = &buf[2];
  1491.                 while (isspace(*st))
  1492.                     st++;
  1493.                 if (!*st)
  1494.                     (void) printf("File name missing!\n");
  1495.                 else
  1496.                 {
  1497.                     if (xfp = fopen(st, "a"))
  1498.                     {
  1499.                         (void) fclose(fp);
  1500.                         fp = fopen(name, "r");
  1501.                         while ((c = fgetc(fp)) != EOF)
  1502.                             (void) fputc(c, xfp);
  1503.                         (void) fclose(xfp);
  1504.                         (void) fclose(fp);
  1505.                         fp = fopen(name, "a");
  1506.                     }
  1507.                     else
  1508.                         (void) printf("Unable to open %s.\n",
  1509.                             st);
  1510.                 }
  1511.                 break;
  1512.             case '.':    /* end of message */
  1513.                 done = TRUE;
  1514.                 break;
  1515.             default:    /* user doesn't know */
  1516.                 (void) printf("Unrecognized command %c -- ignored\n",
  1517.                     buf[1] & 0x7f);
  1518.                 break;
  1519.             }
  1520.             continue;
  1521.         }
  1522.         (void) fprintf(fp, "%s\n", buf);
  1523.     }
  1524.     (void) fclose(fp);
  1525.     (void) printf("\n");
  1526.     return(TRUE);
  1527. }
  1528.  
  1529. /*--------------------------------------------------- justname() ------------
  1530. / extract just filename from a full path
  1531. /---------------------------------------------------------------------------*/
  1532. char *
  1533. justname(fpath)
  1534. char *fpath;
  1535. {
  1536.     register char *cp;
  1537.  
  1538.     if (cp = strrchr(fpath, '/'))
  1539.         return(++cp);
  1540.  
  1541.     return(fpath);
  1542. }
  1543.  
  1544. /*--------------------------------------------------- memalloc() ------------
  1545. / allocate specified amount of memory.  If not successful, exit.
  1546. /---------------------------------------------------------------------------*/
  1547. char *
  1548. memalloc(size)
  1549. register int size;
  1550. {
  1551.     register char *cp;
  1552.  
  1553.     if (!(cp = malloc((unsigned)size)))
  1554.     {
  1555.         perror(prognam);
  1556.         exit(99);
  1557.     }
  1558.     return(cp);
  1559. }
  1560.  
  1561. /*--------------------------------------------------- get_final_id() --------
  1562. / Get the RCSOWN user id to create files with.  If none found, use user id.
  1563. /---------------------------------------------------------------------------*/
  1564. void
  1565. get_final_id()
  1566. {
  1567.     FILE *fp;
  1568.  
  1569.     if(!rcsown)            /* if there isn't one of these. */
  1570. #ifdef V_RCS
  1571.         rcsown = "rcsfiles";    /* default name. */
  1572. #else
  1573.         rcsown = "sccsfiles";    /* default name. */
  1574. #endif
  1575.  
  1576.     if ((fp = fopen (pwdfile, "r")) == NULL)
  1577.     {
  1578. #ifdef DEBUG
  1579.         (void) fprintf(stderr, "setpwent: %s non-existant or unreadable.\n",
  1580.                 pwdfile);
  1581. #endif
  1582.         user_id = real_user_id;        /* make sure it's owners id now. */
  1583.         return;        /* couldn't do it. */
  1584.     }
  1585.     while (nextent(fp))        /* while entries in file.. */
  1586.         if (!strcmp(pwdname, rcsown))   /* If name matches. */
  1587.             break;
  1588.     (void) fclose(fp);    /* close the file. */
  1589.     return;            /* found it or not, return. */
  1590. }
  1591.  
  1592. /*--------------------------------------------------- nextent() -------------
  1593. / get one entry from a password file.  Return TRUE if there is one found,
  1594. / FALSE otherwise.
  1595. /---------------------------------------------------------------------------*/
  1596. int
  1597. nextent(fle)
  1598. FILE *fle;     /* file pointer. */
  1599. {
  1600.     register char *cp, *pwp;
  1601.     char savbuf[200];    /* usually large enough for a password entry. */
  1602.  
  1603.     while (strrd(savbuf, (int) (sizeof (savbuf)), fle) != -1)
  1604.     {
  1605.         pwp = pwdname;
  1606.         cp = savbuf;        /* get user name */
  1607.         while (*cp && *cp != ':')
  1608.             *pwp++ = *cp++;
  1609.         *pwp = '\0';        /* terminate name. */
  1610.         for (cp++; *cp && *cp != ':'; cp++)
  1611.             ;        /* skip over password. */
  1612.         user_id = atoi(++cp);    /* ok, save this users id number. */
  1613.         return (TRUE);
  1614.     }
  1615.     user_id = real_user_id;        /* make sure it's owners id now. */
  1616.     return (FALSE);
  1617. }
  1618.  
  1619. /*----------------------------- End of cio.c -------------------------------*/
  1620.