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