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