home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / pgp2 / src / c / fileio < prev    next >
Encoding:
Text File  |  1995-06-08  |  36.1 KB  |  1,465 lines

  1. /*      fileio.c  - I/O routines for PGP.
  2.    PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.
  3.  
  4.    (c) Copyright 1990-1994 by Philip Zimmermann.  All rights reserved.
  5.    The author assumes no liability for damages resulting from the use
  6.    of this software, even if the damage results from defects in this
  7.    software.  No warranty is expressed or implied.
  8.  
  9.    Note that while most PGP source modules bear Philip Zimmermann's
  10.    copyright notice, many of them have been revised or entirely written
  11.    by contributors who frequently failed to put their names in their
  12.    code.  Code that has been incorporated into PGP from other authors
  13.    was either originally published in the public domain or is used with
  14.    permission from the various authors.
  15.  
  16.    PGP is available for free to the public under certain restrictions.
  17.    See the PGP User's Guide (included in the release package) for
  18.    important information about licensing, patent restrictions on
  19.    certain algorithms, trademarks, copyrights, and export controls.
  20.  
  21.    Modified 16 Apr 92 - HAJK
  22.    Mods for support of VAX/VMS file system
  23.  
  24.    Modified 17 Nov 92 - HAJK
  25.    Change to temp file stuff for VMS.
  26.  */
  27.  
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #ifdef UNIX
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #ifdef _BSD
  38. #include <sys/param.h>
  39. #endif
  40. extern int errno;
  41. #endif                /* UNIX */
  42. #ifdef VMS
  43. #include <file.h>
  44. #include <assert.h>
  45. #endif
  46. #include "random.h"
  47. #include "mpilib.h"
  48. #include "mpiio.h"
  49. #include "fileio.h"
  50. #include "language.h"
  51. #include "pgp.h"
  52. #include "exitpgp.h"
  53. #include "charset.h"
  54. #include "system.h"
  55. #if defined(MSDOS) || defined(OS2)
  56. #include <io.h>
  57. #include <fcntl.h>
  58. #endif
  59.  
  60. #ifndef F_OK
  61. #define F_OK    0
  62. #define X_OK    1
  63. #define W_OK    2
  64. #define R_OK    4
  65. #endif                /* !F_OK */
  66.  
  67. /*
  68.  * DIRSEPS is a string of possible directory-separation characters
  69.  * The first one is the preferred one, which goes in between
  70.  * PGPPATH and the file name if PGPPATH is not terminated with a
  71.  * directory separator.
  72.  */
  73.  
  74. #if defined(MSDOS) || defined(__MSDOS__) || defined(OS2)
  75. static char const DIRSEPS[] = "\\/:";
  76. #define BSLASH
  77.  
  78. #elif defined(ATARI)
  79. static char const DIRSEPS[] = "\\/:";
  80. #define BSLASH
  81.  
  82. #elif defined(UNIX)
  83. static char const DIRSEPS[] = "/";
  84. #define MULTIPLE_DOTS
  85.  
  86. #elif defined(AMIGA)
  87. static char const DIRSEPS[] = "/:";
  88. #define MULTIPLE_DOTS
  89.  
  90. #elif defined(VMS)
  91. static char const DIRSEPS[] = "]:";    /* Any more? */
  92.  
  93. #elif defined(RISC_OS)
  94. static char const DIRSEPS[]=".:";    /* yes -- GJM */
  95.  
  96. #else
  97. /* #error is not portable, this has the same effect */
  98. #include "Unknown OS"
  99. #endif
  100.  
  101.  
  102. /* 1st character of temporary file extension */
  103. #ifdef RISC_OS
  104. #define TMP_EXT '_'        /* '/_##' on RISC OS -- GJM */
  105. #else
  106. #define    TMP_EXT    '$'        /* extensions are '.$##' */
  107. #endif
  108.  
  109. /* Since RISC OS is unique (just about) in not leaving the '.' char
  110.  * available for extensions, we add the following. Wherever 'EXT_CHR'
  111.  * appears below, the original source had '.' .
  112.  * There is one reference that doesn't quite fit that pattern:
  113.  * a printf format string had the '.' hard-wired.
  114.  * -- GJM
  115.  */
  116. #ifdef RISC_OS
  117. #define EXT_CHR '/'
  118. #else
  119. #define EXT_CHR '.'
  120. #endif
  121.  
  122. /* The PGPPATH environment variable */
  123.  
  124. static char PGPPATH[] = "PGPPATH";
  125.  
  126. /* Disk buffers, used here and in crypto.c */
  127. byte textbuf[DISKBUFSIZE];
  128. static unsigned textbuf2[2 * DISKBUFSIZE / sizeof(unsigned)];
  129.  
  130. boolean file_exists(char *filename)
  131. /*      Returns TRUE iff file exists. */
  132. {
  133.     return access(filename, F_OK) == 0;
  134. }                /* file_exists */
  135.  
  136. static boolean is_regular_file(char *filename)
  137. {
  138. #ifdef S_ISREG
  139.     struct stat st;
  140.     return stat(filename, &st) != -1 && S_ISREG(st.st_mode);
  141. #else
  142.     return TRUE;
  143. #endif
  144. }
  145.  
  146.  
  147. /*
  148.  * This wipes a file with pseudo-random data.  The purpose of this is to
  149.  * make sure no sensitive information is left on the disk.  The use
  150.  * of pseudo-random data is to defeat disk compression drivers (such as
  151.  * Stacker and dblspace) so that we are guaranteed that the entire file
  152.  * has been overwritten.
  153.  *
  154.  * Note that the file MUST be open for read/write.
  155.  *
  156.  * It may not work to eliminate everything from non-volatile storage
  157.  * if the OS you're using does its own paging or swapping.  Then
  158.  * it's an issue of how the OS's paging device is wiped, and you can
  159.  * only hope that the space will be reused within a few seconds.
  160.  *
  161.  * Also, some file systems (in particular, the Logging File System
  162.  * for Sprite) do not write new data in the same place as old data,
  163.  * defeating this wiping entirely.  Fortunately, such systems
  164.  * usually don't need a swap file, and for small temp files, they
  165.  * do write-behind, so if you create and delete a file fast enough,
  166.  * it never gets written to disk at all.
  167.  */
  168.  
  169. /*
  170.  * The data is randomly generated with the size of the file as a seed.
  171.  * The data should be random and not leak information.  If someone is
  172.  * examining deleted files, presumably they can reconstruct the file size,
  173.  * so that's not a secret.  H'm... this wiping algorithm makes it easy to,
  174.  * given a block of data, find the size of the file it came from
  175.  * and the offset of this block within it.  That in turn reveals
  176.  * something about the state of the disk's allocation tables when the
  177.  * file was used, possibly making it easier to find other files created
  178.  * at neaby times - such as plaintext files.  Is this acceptable?
  179.  */
  180.  
  181. /*
  182.  * Theory of operation: We use the additive congruential RNG
  183.  * r[i] = r[i-24] + r[i-55], from Knuth, Vol. 2.  This is fast
  184.  * and has a long enough period that there should be no repetitions
  185.  * in even a huge file.  It is seeded with r[-55] through r[-1]
  186.  * using another polynomial-based RNG.  We seed a linear feedback
  187.  * shift register (CRC generator) with the size of the swap file,
  188.  * and clock in 0 bits.  Each 32 bits, the value of the generator is
  189.  * taken as the next integer.  This is just to ensure a reasonably
  190.  * even mix of 1's and 0's in the initialization vector.
  191.  */
  192.  
  193. /*
  194.  * This is the CRC-32 polynomial, which should be okay for random
  195.  * number generation.
  196.  * x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
  197.  * = 1 0000 0100 1100 0001 0001 1101 1011 0111
  198.  * = 0x04c11db7
  199.  */
  200. #define POLY 0x04c11db7
  201.  
  202. static void wipeout(FILE * f)
  203. {
  204.     unsigned *p1, *p2, *p3;
  205.     unsigned long len;
  206.     unsigned long t;
  207.     int i;
  208.  
  209.     /* Get the file size */
  210.     fseek(f, 0L, SEEK_END);
  211.     len = ftell(f);
  212.     rewind(f);
  213.  
  214.     /* Seed of first RNG.  Inverted to get more 1 bits */
  215.     t = ~len;
  216.  
  217.     /* Initialize first 55 words of buf with pseudo-random stuff */
  218.     p1 = (unsigned *) textbuf2 + 55;
  219.     do {
  220.     for (i = 0; i < 32; i++)
  221.         t = (t & 0x80000000) ? t << 1 ^ POLY : t << 1;
  222.     *--p1 = (unsigned) t;
  223.     } while (p1 > (unsigned *) textbuf2);
  224.  
  225.     while (len) {
  226.     /* Fill buffer with pseudo-random integers */
  227.  
  228.     p3 = (unsigned *) textbuf2 + 55;
  229.     p2 = (unsigned *) textbuf2 + 24;
  230.     p1 = (unsigned *) textbuf2 + sizeof(textbuf2) / sizeof(*p1);
  231.     do {
  232.         *--p1 = *--p2 + *--p3;
  233.     } while (p2 > (unsigned *) textbuf2);
  234.  
  235.     p2 = (unsigned *) textbuf2 + sizeof(textbuf2) / sizeof(*p1);
  236.     do {
  237.         *--p1 = *--p2 + *--p3;
  238.     } while (p3 > (unsigned *) textbuf2);
  239.  
  240.     p3 = (unsigned *) textbuf2 + sizeof(textbuf2) / sizeof(*p3);
  241.     do {
  242.         *--p1 = *--p2 + *--p3;
  243.     } while (p1 > (unsigned *) textbuf2);
  244.  
  245.     /* Write it out - yes, we're ignoring errors */
  246.     if (len > sizeof(textbuf2)) {
  247.         fwrite((char const *) textbuf2, sizeof(textbuf2), 1, f);
  248.         len -= sizeof(textbuf2);
  249.     } else {
  250.         fwrite((char const *) textbuf2, len, 1, f);
  251.         len = 0;
  252.     }
  253.     }
  254. }
  255.  
  256.  
  257. /*
  258.  * Completely overwrite and erase file, so that no sensitive
  259.  * information is left on the disk.
  260.  */
  261. int wipefile(char *filename)
  262. {
  263.     FILE *f;
  264.     /* open file f for read/write, in binary (not text) mode... */
  265.     if ((f = fopen(filename, FOPRWBIN)) == NULL)
  266.     return -1;        /* error - file can't be opened */
  267.     wipeout(f);
  268.     fclose(f);
  269.     return 0;            /* normal return */
  270. }                /* wipefile */
  271.  
  272. /*
  273.  * Returns the part of a filename after all directory specifiers.
  274.  */
  275. char *file_tail(char *filename)
  276. {
  277.     char *p;
  278.     char const *s = DIRSEPS;
  279.  
  280.     while (*s) {
  281.     p = strrchr(filename, *s);
  282.     if (p)
  283.         filename = p + 1;
  284.     s++;
  285.     }
  286.  
  287.     return filename;
  288. }
  289.  
  290.  
  291. /* return TRUE if extension matches the end of filename */
  292. boolean has_extension(char *filename, char *extension)
  293. {
  294.     int lf = strlen(filename);
  295.     int lx = strlen(extension);
  296.  
  297.     if (lf <= lx)
  298.     return FALSE;
  299.     return !strcmp(filename + lf - lx, extension);
  300. }
  301.  
  302. /* return TRUE if path is a filename created by tempfile() */
  303. /* Filename matches "*.$[0-9][0-9]" */
  304. boolean is_tempfile(char *path)
  305. {
  306.     char *p = strrchr(path, EXT_CHR);    /* '.' -- GJM */
  307.  
  308.     return p != NULL && p[1] == TMP_EXT &&
  309.     isdigit(p[2]) && isdigit(p[3]) && p[4] == '\0';
  310. }
  311.  
  312. /*
  313.  * Returns TRUE if user left off file extension, allowing default.
  314.  * Note that the name is misleading if multiple dots are allowed.
  315.  * not_pgp_extension or something would be better.
  316.  */
  317. boolean no_extension(char *filename)
  318. {
  319. #ifdef MULTIPLE_DOTS        /* filename can have more than one dot */
  320.     if (has_extension(filename, ASC_EXTENSION) ||
  321.     has_extension(filename, PGP_EXTENSION) ||
  322.     has_extension(filename, SIG_EXTENSION) ||
  323.     is_tempfile(filename))
  324.     return FALSE;
  325.     else
  326.     return TRUE;
  327. #else
  328.     filename = file_tail(filename);
  329.  
  330.     return strrchr(filename, EXT_CHR) == NULL;    /* '.' -- GJM */
  331. #endif
  332. }                /* no_extension */
  333.  
  334.  
  335. /* deletes trailing ".xxx" file extension after the period. */
  336. void drop_extension(char *filename)
  337. {
  338.     if (!no_extension(filename))
  339.     *strrchr(filename, EXT_CHR) = '\0';    /* '.' -- GJM */
  340. }                /* drop_extension */
  341.  
  342.  
  343. /* append filename extension if there isn't one already. */
  344. void default_extension(char *filename, char *extension)
  345. {
  346.     if (no_extension(filename))
  347.     strcat(filename, extension);
  348. }                /* default_extension */
  349.  
  350. #ifndef MAX_NAMELEN
  351. #if defined(AMIGA) || defined(NeXT) || (defined(BSD) && BSD > 41) || (defined(sun) && defined(i386))
  352. #define    MAX_NAMELEN    255
  353. #else
  354. #include <limits.h>
  355. #endif
  356. #endif
  357.  
  358. /* truncate the filename so that an extension can be tacked on. */
  359. static void truncate_name(char *path, int ext_len)
  360. {
  361. #ifdef UNIX            /* for other systems this is a no-op */
  362.     char *p;
  363. #ifdef MAX_NAMELEN        /* overrides the use of pathconf() */
  364.     int namemax = MAX_NAMELEN;
  365. #else
  366.     int namemax;
  367. #ifdef _PC_NAME_MAX
  368.     char dir[MAX_PATH];
  369.  
  370.     strcpy(dir, path);
  371.     if ((p = strrchr(dir, '/')) == NULL) {
  372.     strcpy(dir, ".");
  373.     } else {
  374.     if (p == dir)
  375.         ++p;
  376.     *p = '\0';
  377.     }
  378.     if ((namemax = pathconf(dir, _PC_NAME_MAX)) <= ext_len)
  379.     return;
  380. #else
  381. #ifdef NAME_MAX
  382.     namemax = NAME_MAX;
  383. #else
  384.     namemax = 14;
  385. #endif                /* NAME_MAX */
  386. #endif                /* _PC_NAME_MAX */
  387. #endif                /* MAX_NAMELEN */
  388.  
  389.     if ((p = strrchr(path, '/')) == NULL)
  390.     p = path;
  391.     else
  392.     ++p;
  393.     if (strlen(p) > namemax - ext_len) {
  394.     if (verbose)
  395.         fprintf(pgpout, "Truncating filename '%s' ", path);
  396.     p[namemax - ext_len] = '\0';
  397.     if (verbose)
  398.         fprintf(pgpout, "to '%s'\n", path);
  399.     }
  400. #elif defined(RISC_OS)        /* GJM */
  401.     char *p,*q;
  402.     char all[MAX_PATH];
  403.     strcpy(all,path);
  404.     p=strrchr(all,'.'); q=strrchr(all,':');
  405.     if (p) { if (q && q-p>0) p=q+1; else ++p; }
  406.       else { if (q)          p=q+1; else p=all; }
  407.     if (strlen(p) > 10-ext_len) {
  408.       if (verbose) fprintf(pgpout,"Truncating filename '%s' ",all);
  409.       p[10-ext_len]='\0';
  410.       if (verbose) fprintf(pgpout,"to '%s'\n",all);
  411.     }
  412. #endif                /* UNIX / RISC_OS */
  413. }
  414.  
  415. /* change the filename extension. */
  416. void force_extension(char *filename, char *extension)
  417. {
  418.     drop_extension(filename);    /* out with the old */
  419.     truncate_name(filename, strlen(extension));
  420.     strcat(filename, extension);    /* in with the new */
  421. }                /* force_extension */
  422.  
  423.  
  424. /*
  425.  * Get yes/no answer from user, returns TRUE for yes, FALSE for no.
  426.  * First the translations are checked, if they don't match 'y' and 'n'
  427.  * are tried.
  428.  */
  429. boolean getyesno(char default_answer)
  430. {
  431.     char buf[8];
  432.     static char yes[8], no[8];
  433.  
  434.     if (yes[0] == '\0') {
  435.     strncpy(yes, LANG("y"), 7);
  436.     strncpy(no, LANG("n"), 7);
  437.     }
  438.     if (!batchmode) {        /* return default answer in batchmode */
  439.     getstring(buf, 6, TRUE);    /* echo keyboard input */
  440.     strlwr(buf);
  441.     if (!strncmp(buf, no, strlen(no)))
  442.         return FALSE;
  443.     if (!strncmp(buf, yes, strlen(yes)))
  444.         return TRUE;
  445.     if (buf[0] == 'n')
  446.         return FALSE;
  447.     if (buf[0] == 'y')
  448.         return TRUE;
  449.     }
  450.     return default_answer == 'y' ? TRUE : FALSE;
  451. }                /* getyesno */
  452.  
  453. /* if user consents to it, change the filename extension. */
  454. char *maybe_force_extension(char *filename, char *extension)
  455. {
  456.     static char newname[MAX_PATH];
  457.     if (!has_extension(filename, extension)) {
  458.     strcpy(newname, filename);
  459.     force_extension(newname, extension);
  460.     if (!file_exists(newname)) {
  461.         fprintf(pgpout, LANG("\nShould '%s' be renamed to '%s' [Y/n]? "),
  462.             filename, newname);
  463.         if (getyesno('y'))
  464.         return newname;
  465.     }
  466.     }
  467.     return NULL;
  468. }                /* maybe_force_extension */
  469.  
  470. /*
  471.  * Add a trailing directory separator to a name, if absent.
  472.  */
  473. static void addslash(char *name)
  474. {
  475.     int i = strlen(name);
  476.  
  477.     if (i != 0 && !strchr(DIRSEPS, name[i - 1])) {
  478.     name[i] = DIRSEPS[0];
  479.     name[i + 1] = '\0';
  480.     }
  481. }
  482.  
  483. /*
  484.  * Builds a filename with a complete path specifier from the environmental
  485.  * variable PGPPATH.
  486.  */
  487. char *buildfilename(char *result, char *fname)
  488. {
  489.     char const *s = getenv(PGPPATH);
  490.  
  491.     result[0] = '\0';
  492.  
  493.     if (s && strlen(s) <= 50) {
  494.     strcpy(result, s);
  495.     }
  496. #ifdef UNIX
  497.     /* On Unix, default to $HOME/.pgp, otherwise, current directory. */
  498.     else {
  499.     s = getenv("HOME");
  500.     if (s && strlen(s) <= 50) {
  501.         strcpy(result, s);
  502.         addslash(result);
  503.         strcat(result, ".pgp");
  504.     }
  505.     }
  506. #endif                /* UNIX */
  507.  
  508.     addslash(result);
  509.     strcat(result, fname);
  510.     return result;
  511. }                /* buildfilename */
  512.  
  513. char *buildsysfilename(char *result, char *fname)
  514. {
  515.     buildfilename(result, fname);
  516. #ifdef PGP_SYSTEM_DIR
  517.     if (file_exists(result))
  518.     return result;
  519.     strcpy(result, PGP_SYSTEM_DIR);
  520.     strcat(result, fname);
  521.     if (file_exists(result))
  522.     return result;
  523.     buildfilename(result, fname);    /* Put name back for error */
  524. #endif
  525.     return result;
  526. }
  527.  
  528.  
  529. /* Convert filename to canonical form, with slashes as separators */
  530. void file_to_canon(char *filename)
  531. {
  532. #ifdef BSLASH
  533.     while (*filename) {
  534.     if (*filename == '\\')
  535.         *filename = '/';
  536.     ++filename;
  537.     }
  538. #endif
  539. }
  540.  
  541.  
  542. int write_error(FILE * f)
  543. {
  544.     fflush(f);
  545.     if (ferror(f)) {
  546. #ifdef ENOSPC
  547.     if (errno == ENOSPC)
  548.         fprintf(pgpout, LANG("\nDisk full.\n"));
  549.     else
  550. #endif
  551.         fprintf(pgpout, LANG("\nFile write error.\n"));
  552.     return -1;
  553.     }
  554.     return 0;
  555. }
  556.  
  557. /* copy file f to file g, for longcount bytes */
  558. int copyfile(FILE * f, FILE * g, word32 longcount)
  559. {
  560.     int count, status = 0;
  561.     do {            /* read and write the whole file... */
  562.     if (longcount < (word32) DISKBUFSIZE)
  563.         count = (int) longcount;
  564.     else
  565.         count = DISKBUFSIZE;
  566.     count = fread(textbuf, 1, count, f);
  567.     if (count > 0) {
  568.         if (CONVERSION != NO_CONV) {
  569.         int i;
  570.         for (i = 0; i < count; i++)
  571.             textbuf[i] = (CONVERSION == EXT_CONV) ?
  572.             EXT_C(textbuf[i]) :
  573.             INT_C(textbuf[i]);
  574.         }
  575.         if (fwrite(textbuf, 1, count, g) != count) {
  576.         /* Problem: return error value */
  577.         status = -1;
  578.         break;
  579.         }
  580.         longcount -= count;
  581.     }
  582.     /* if text block was short, exit loop */
  583.     } while (count == DISKBUFSIZE);
  584.     burn(textbuf);        /* burn sensitive data on stack */
  585.     return status;
  586. }                /* copyfile */
  587.  
  588. /*
  589.  * Like copyfile, but takes a position for file f.  Returns with
  590.  * f and g pointing just past the copied data.
  591.  */
  592. int copyfilepos(FILE * f, FILE * g, word32 longcount, word32 fpos)
  593. {
  594.     fseek(f, fpos, SEEK_SET);
  595.     return copyfile(f, g, longcount);
  596. }
  597.  
  598.  
  599. /* copy file f to file g, for longcount bytes.  Convert to
  600.  * canonical form as we go.  f is open in text mode.  Canonical
  601.  * form uses crlf's as line separators.
  602.  */
  603. int copyfile_to_canon(FILE * f, FILE * g, word32 longcount)
  604. {
  605.     int count, status = 0;
  606.     byte c, *tb1, *tb2;
  607.     int i, nbytes;
  608.     int nspaces = 0;
  609.     do {            /* read and write the whole file... */
  610.     if (longcount < (word32) DISKBUFSIZE)
  611.         count = (int) longcount;
  612.     else
  613.         count = DISKBUFSIZE;
  614.     count = fread(textbuf, 1, count, f);
  615.     if (count > 0) {
  616.         /* Convert by adding CR before LF */
  617.         tb1 = textbuf;
  618.         tb2 = (byte *) textbuf2;
  619.         for (i = 0; i < count; ++i) {
  620.         switch (CONVERSION) {
  621.         case EXT_CONV:
  622.             c = EXT_C(*tb1++);
  623.             break;
  624.         case INT_CONV:
  625.             c = INT_C(*tb1++);
  626.             break;
  627.         default:
  628.             c = *tb1++;
  629.         }
  630.         if (strip_spaces) {
  631.             if (c == ' ') {
  632.             /* Don't output spaces yet */
  633.             nspaces += 1;
  634.             } else {
  635.             if (c == '\n') {
  636.                 *tb2++ = '\r';
  637.                 nspaces = 0;    /* Delete trailing spaces */
  638.             }
  639.             if (nspaces) {
  640.                 /* Put out spaces now */
  641.                 do
  642.                 *tb2++ = ' ';
  643.                 while (--nspaces);
  644.             }
  645.             *tb2++ = c;
  646.             }
  647.         } else {
  648.             if (c == '\n')
  649.             *tb2++ = '\r';
  650.             *tb2++ = c;
  651.         }
  652.         }
  653.         nbytes = tb2 - (byte *) textbuf2;
  654.         if (fwrite(textbuf2, 1, nbytes, g) != nbytes) {
  655.         /* Problem: return error value */
  656.         status = -1;
  657.         break;
  658.         }
  659.         longcount -= count;
  660.     }
  661.     /* if text block was short, exit loop */
  662.     } while (count == DISKBUFSIZE);
  663.     burn(textbuf);        /* burn sensitive data on stack */
  664.     burn(textbuf2);
  665.     return status;
  666. }                /* copyfile_to_canon */
  667.  
  668.  
  669. /* copy file f to file g, for longcount bytes.  Convert from
  670.  * canonical to local form as we go.  g is open in text mode.  Canonical
  671.  * form uses crlf's as line separators.
  672.  */
  673. int copyfile_from_canon(FILE * f, FILE * g, word32 longcount)
  674. {
  675.     int count, status = 0;
  676.     byte c, *tb1, *tb2;
  677.     int i, nbytes;
  678.     do {            /* read and write the whole file... */
  679.     if (longcount < (word32) DISKBUFSIZE)
  680.         count = (int) longcount;
  681.     else
  682.         count = DISKBUFSIZE;
  683.     count = fread(textbuf, 1, count, f);
  684.     if (count > 0) {
  685.         /* Convert by removing CR's */
  686.         tb1 = textbuf;
  687.         tb2 = (byte *) textbuf2;
  688.         for (i = 0; i < count; ++i) {
  689.         switch (CONVERSION) {
  690.         case EXT_CONV:
  691.             c = EXT_C(*tb1++);
  692.             break;
  693.         case INT_CONV:
  694.             c = INT_C(*tb1++);
  695.             break;
  696.         default:
  697.             c = *tb1++;
  698.         }
  699.         if (c != '\r')
  700.             *tb2++ = c;
  701.         }
  702.         nbytes = tb2 - (byte *) textbuf2;
  703.         if (fwrite(textbuf2, 1, nbytes, g) != nbytes) {
  704.         /* Problem: return error value */
  705.         status = -1;
  706.         break;
  707.         }
  708.         longcount -= count;
  709.     }
  710.     /* if text block was short, exit loop */
  711.     } while (count == DISKBUFSIZE);
  712.     burn(textbuf);        /* burn sensitive data on stack */
  713.     burn(textbuf2);
  714.     return status;
  715. }                /* copyfile_from_canon */
  716.  
  717. /*      Copy srcFile to destFile  */
  718. int copyfiles_by_name(char *srcFile, char *destFile)
  719. {
  720.     FILE *f, *g;
  721.     int status = 0;
  722.     long fileLength;
  723.  
  724.     f = fopen(srcFile, FOPRBIN);
  725.     if (f == NULL)
  726.     return -1;
  727.     g = fopen(destFile, FOPWBIN);
  728.     if (g == NULL) {
  729.     fclose(f);
  730.     return -1;
  731.     }
  732.     /* Get file length and copy it */
  733.     fseek(f, 0L, SEEK_END);
  734.     fileLength = ftell(f);
  735.     rewind(f);
  736.     status = copyfile(f, g, fileLength);
  737.     fclose(f);
  738.     if (write_error(g))
  739.     status = -1;
  740.     fclose(g);
  741.     return status;
  742. }                /* copyfiles_by_name */
  743.  
  744. /* Copy srcFile to destFile, converting to canonical text form  */
  745. int make_canonical(char *srcFile, char *destFile)
  746. {
  747.     FILE *f, *g;
  748.     int status = 0;
  749.     long fileLength;
  750.  
  751.     if (((f = fopen(srcFile, FOPRTXT)) == NULL) ||
  752.     ((g = fopen(destFile, FOPWBIN)) == NULL))
  753.     /* Can't open files */
  754.     return -1;
  755.  
  756.     /* Get file length and copy it */
  757.     fseek(f, 0L, SEEK_END);
  758.     fileLength = ftell(f);
  759.     rewind(f);
  760.     CONVERSION = INT_CONV;
  761.     status = copyfile_to_canon(f, g, fileLength);
  762.     CONVERSION = NO_CONV;
  763.     fclose(f);
  764.     if (write_error(g))
  765.     status = -1;
  766.     fclose(g);
  767.     return status;
  768. }                /* make_canonical */
  769.  
  770. /*
  771.  * Like rename() but will try to copy the file if the rename fails.
  772.  * This is because under OS's with multiple physical volumes if the
  773.  * source and destination are on different volumes the rename will fail
  774.  */
  775. int rename2(char *srcFile, char *destFile)
  776. {
  777.     FILE *f, *g;
  778.     int status = 0;
  779.     long fileLength;
  780.  
  781. #ifdef VMS
  782.     if (rename(srcFile, destFile) != 0)
  783. #else
  784.     if (rename(srcFile, destFile) == -1)
  785. #endif
  786.     {
  787.     /* Rename failed, try a copy */
  788.     if (((f = fopen(srcFile, FOPRBIN)) == NULL) ||
  789.         ((g = fopen(destFile, FOPWBIN)) == NULL))
  790.         /* Can't open files */
  791.         return -1;
  792.  
  793.     /* Get file length and copy it */
  794.     fseek(f, 0L, SEEK_END);
  795.     fileLength = ftell(f);
  796.     rewind(f);
  797.     status = copyfile(f, g, fileLength);
  798.     if (write_error(g))
  799.         status = -1;
  800.  
  801.     /* Zap source file if the copy went OK, otherwise zap the (possibly
  802.        incomplete) destination file */
  803.     if (status >= 0) {
  804.         wipeout(f);        /* Zap source file */
  805.         fclose(f);
  806.         remove(srcFile);
  807.         fclose(g);
  808.     } else {
  809.         if (is_regular_file(destFile)) {
  810.         wipeout(g);    /* Zap destination file */
  811.         fclose(g);
  812.         remove(destFile);
  813.         } else {
  814.         fclose(g);
  815.         }
  816.         fclose(f);
  817.     }
  818.     }
  819.     return status;
  820. }
  821.  
  822. /* read the data from stdin to the phantom input file */
  823. int readPhantomInput(char *filename)
  824. {
  825.     FILE *outFilePtr;
  826.     byte buffer[512];
  827.     int bytesRead, status = 0;
  828.  
  829.     if (verbose)
  830.     fprintf(pgpout, "writing stdin to file %s\n", filename);
  831.     if ((outFilePtr = fopen(filename, FOPWBIN)) == NULL)
  832.     return -1;
  833.  
  834. #if defined(MSDOS) || defined(OS2)
  835.     /* Under DOS must set input stream to binary mode to avoid data mangling */
  836.     setmode(fileno(stdin), O_BINARY);
  837. #endif                /* MSDOS || OS2 */
  838.     while ((bytesRead = fread(buffer, 1, 512, stdin)) > 0)
  839.     if (fwrite(buffer, 1, bytesRead, outFilePtr) != bytesRead) {
  840.         status = -1;
  841.         break;
  842.     }
  843.     if (write_error(outFilePtr))
  844.     status = -1;
  845.     fclose(outFilePtr);
  846. #if defined(MSDOS) || defined(OS2)
  847.     setmode(fileno(stdin), O_TEXT);    /* Reset stream */
  848. #endif                /* MSDOS || OS2 */
  849.     return status;
  850. }
  851.  
  852. /* write the data from the phantom output file to stdout */
  853. int writePhantomOutput(char *filename)
  854. {
  855.     FILE *outFilePtr;
  856.     byte buffer[512];
  857.     int bytesRead, status = 0;
  858.  
  859.     if (verbose)
  860.     fprintf(pgpout, "writing file %s to stdout\n", filename);
  861.     /* this can't fail since we just created the file */
  862.     outFilePtr = fopen(filename, FOPRBIN);
  863.  
  864. #if defined(MSDOS) || defined(OS2)
  865.     setmode(fileno(stdout), O_BINARY);
  866. #endif                /* MSDOS || OS2 */
  867.     while ((bytesRead = fread(buffer, 1, 512, outFilePtr)) > 0)
  868.     if (fwrite(buffer, 1, bytesRead, stdout) != bytesRead) {
  869.         status = -1;
  870.         break;
  871.     }
  872.     fclose(outFilePtr);
  873.     fflush(stdout);
  874.     if (ferror(stdout)) {
  875.     status = -1;
  876.     fprintf(pgpout, LANG("\007Write error on stdout.\n"));
  877.     }
  878. #if defined(MSDOS) || defined(OS2)
  879.     setmode(fileno(stdout), O_TEXT);
  880. #endif                /* MSDOS || OS2 */
  881.  
  882.     return status;
  883. }
  884.  
  885. /* Return the size from the current position of file f to the end */
  886. word32 fsize(FILE * f)
  887. {
  888.     long fpos = ftell(f);
  889.     long fpos2;
  890.  
  891.     fseek(f, 0L, SEEK_END);
  892.     fpos2 = ftell(f);
  893.     fseek(f, fpos, SEEK_SET);
  894.     return (word32) (fpos2 - fpos);
  895. }
  896.  
  897. /* Return TRUE if file filename looks like a pure text file */
  898. int is_text_file(char *filename)
  899. {
  900.     FILE *f = fopen(filename, "r");    /* FOPRBIN gives problem with VMS */
  901.     int i, n, bit8 = 0;
  902.     unsigned char buf[512];
  903.     unsigned char *bufptr = buf;
  904.     unsigned char c;
  905.  
  906.     if (!f)
  907.     return FALSE;        /* error opening it, so not a text file */
  908.     i = n = fread(buf, 1, sizeof(buf), f);
  909.     fclose(f);
  910.     if (n <= 0)
  911.     return FALSE;        /* empty file or error, not a text file */
  912.     if (compressSignature(buf) >= 0)
  913.     return FALSE;
  914.     while (i--) {
  915.     c = *bufptr++;
  916.     if (c & 0x80)
  917.         ++bit8;
  918.     else        /* allow BEL BS HT LF VT FF CR EOF control characters */
  919.       if (c < '\007' || (c > '\r' && c < ' ' && c != '\032'))
  920.         return FALSE;    /* not a text file */
  921.     }
  922.     if (strcmp(language, "ru") == 0)
  923.     return TRUE;
  924.     /* assume binary if more than 1/4 bytes have 8th bit set */
  925.     return bit8 < n / 4;
  926. }                /* is_text_file */
  927.  
  928. VOID *xmalloc(unsigned size)
  929. {
  930.     VOID *p;
  931.     if (size == 0)
  932.     ++size;
  933.     p = malloc(size);
  934.     if (p == NULL) {
  935.     fprintf(stderr, LANG("\n\007Out of memory.\n"));
  936.     exitPGP(1);
  937.     }
  938.     return p;
  939. }
  940.  
  941. /*----------------------------------------------------------------------
  942.  *    temporary file routines
  943.  */
  944.  
  945.  
  946. #define MAXTMPF 8
  947.  
  948. #define    TMP_INUSE    2
  949.  
  950. static struct {
  951.     char path[MAX_PATH];
  952.     int flags;
  953.     int num;
  954. } tmpf[MAXTMPF];
  955.  
  956. static char tmpdir[256];    /* temporary file directory */
  957. static char outdir[256];    /* output directory */
  958. static char tmpbasename[64] = "pgptemp";    /* basename for
  959.                            temporary files */
  960.  
  961.  
  962. /*
  963.  * set directory for temporary files.  path will be stored in
  964.  * tmpdir[] with an appropriate trailing path separator.
  965.  */
  966. void settmpdir(char *path)
  967. {
  968.     char *p;
  969.  
  970.     if (path == NULL || *path == '\0') {
  971.     tmpdir[0] = '\0';
  972.     return;
  973.     }
  974.     strcpy(tmpdir, path);
  975.     p = tmpdir + strlen(tmpdir) - 1;
  976. #ifdef RISC_OS
  977.     /* append path separator, namely '.' -- GJM */
  978.     if (*p != '.' && *p != ':') strcat(tmpdir,".");
  979. #else
  980.     if (*p != '/' && *p != '\\' && *p != ']' && *p != ':') {
  981.     /* append path separator, either / or \ */
  982.     if ((p = strchr(tmpdir, '/')) == NULL &&
  983.         (p = strchr(tmpdir, '\\')) == NULL)
  984.         p = "/";        /* path did not contain / or \, use / */
  985.     strncat(tmpdir, p, 1);
  986.     }
  987. #endif
  988. }
  989.  
  990. /*
  991.  * set output directory to avoid a file copy when temp file is renamed to
  992.  * output file.  the argument filename must be a valid path for a file, not
  993.  * a directory.
  994.  */
  995. void setoutdir(char *filename)
  996. {
  997.     char *p;
  998.  
  999.     if (filename == NULL) {
  1000.     strcpy(outdir, tmpdir);
  1001.     return;
  1002.     }
  1003.     strcpy(outdir, filename);
  1004.     p = file_tail(outdir);
  1005.     strcpy(tmpbasename, p);
  1006.     *p = '\0';
  1007.     drop_extension(tmpbasename);
  1008. #if !defined(BSD42) && !defined(BSD43) && !defined(sun)
  1009.     /*
  1010.      *  we don't depend on pathconf here, if it returns an incorrect value
  1011.      * for NAME_MAX (like Linux 0.97 with minix FS) finding a unique name
  1012.      * for temp files can fail.
  1013.      */
  1014.     tmpbasename[10] = '\0';    /* 14 char limit */
  1015. #endif
  1016. }
  1017.  
  1018. /*
  1019.  * return a unique temporary file name
  1020.  */
  1021. char *tempfile(int flags)
  1022. {
  1023.     int i, j;
  1024.     int num;
  1025.     int fd;
  1026. #ifndef UNIX
  1027.     FILE *fp;
  1028. #endif
  1029.  
  1030.     for (i = 0; i < MAXTMPF; ++i)
  1031.     if (tmpf[i].flags == 0)
  1032.         break;
  1033.  
  1034.     if (i == MAXTMPF) {
  1035.     /* message only for debugging, no need for LANG */
  1036.     fprintf(stderr, "\n\007Out of temporary files\n");
  1037.     return NULL;
  1038.     }
  1039.   again:
  1040.     num = 0;
  1041.     do {
  1042.     for (j = 0; j < MAXTMPF; ++j)
  1043.         if (tmpf[j].flags && tmpf[j].num == num)
  1044.         break;
  1045.     if (j < MAXTMPF)
  1046.         continue;        /* sequence number already in use */
  1047. #ifdef RISC_OS
  1048.     sprintf(tmpf[i].path, "%s%.6s%c%c%02d",    /* limit length -- GJM */
  1049. #else
  1050.     sprintf(tmpf[i].path, "%s%s%c%c%02d",    /* & 3rd %c was a . -- GJM */
  1051. #endif
  1052.         ((flags & TMP_TMPDIR) && *tmpdir ? tmpdir : outdir),
  1053.         tmpbasename, EXT_CHR, TMP_EXT, num);    /* GJM */
  1054.     if (!file_exists(tmpf[i].path))
  1055.         break;
  1056.     }
  1057.     while (++num < 100);
  1058.  
  1059.     if (num == 100) {
  1060.     fprintf(pgpout, "\n\007tempfile: cannot find unique name\n");
  1061.     return NULL;
  1062.     }
  1063. #if defined(UNIX) || defined(VMS)
  1064.     if ((fd = open(tmpf[i].path, O_EXCL | O_RDWR | O_CREAT, 0600)) != -1)
  1065.     close(fd);
  1066. #else
  1067.     if ((fp = fopen(tmpf[i].path, "w")) != NULL)
  1068.     fclose(fp);
  1069.     fd = (fp == NULL ? -1 : 0);
  1070. #endif
  1071.  
  1072.     if (fd == -1) {
  1073.     if (!(flags & TMP_TMPDIR)) {
  1074.         flags |= TMP_TMPDIR;
  1075.         goto again;
  1076.     }
  1077. #ifdef UNIX
  1078.     else if (tmpdir[0] == '\0') {
  1079.         strcpy(tmpdir, "/tmp/");
  1080.         goto again;
  1081.     }
  1082. #elif defined(RISC_OS)
  1083.     /* Might as well do this -- GJM */
  1084.     else if (tmpdir[0] == '\0') {
  1085.         strcpy(tmpdir,"$.tmp.");
  1086.         goto again;
  1087.     }
  1088. #endif
  1089.     }
  1090.     if (fd == -1) {
  1091.     fprintf(pgpout, LANG("\n\007Cannot create temporary file '%s'\n"),
  1092.         tmpf[i].path);
  1093.     user_error();
  1094.     }
  1095. #ifdef VMS
  1096.     remove(tmpf[i].path);
  1097. #endif
  1098.  
  1099.     tmpf[i].num = num;
  1100.     tmpf[i].flags = flags | TMP_INUSE;
  1101.     if (verbose)
  1102.     fprintf(pgpout, "tempfile: created '%s'\n", tmpf[i].path);
  1103.     return tmpf[i].path;
  1104. }                /* tempfile */
  1105.  
  1106. /*
  1107.  * remove temporary file, wipe if necessary.
  1108.  */
  1109. void rmtemp(char *name)
  1110. {
  1111.     int i;
  1112.  
  1113.     for (i = 0; i < MAXTMPF; ++i)
  1114.     if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0)
  1115.         break;
  1116.  
  1117.     if (i < MAXTMPF) {
  1118.     if (strlen(name) > 3 && name[strlen(name) - 3] == TMP_EXT) {
  1119.         /* only remove file if name hasn't changed */
  1120.         if (verbose)
  1121.         fprintf(pgpout, "rmtemp: removing '%s'\n", name);
  1122.         if (tmpf[i].flags & TMP_WIPE)
  1123.         wipefile(name);
  1124.         if (!remove(name)) {
  1125.         tmpf[i].flags = 0;
  1126.         } else if (verbose) {
  1127.         fprintf(stderr, "\nrmtemp: Failed to remove %s", name);
  1128.         perror("\nError");
  1129.         }
  1130.     } else if (verbose)
  1131.         fprintf(pgpout, "rmtemp: not removing '%s'\n", name);
  1132.     }
  1133. }                /* rmtemp */
  1134.  
  1135. /*
  1136.  * make temporary file permanent, returns the new name.
  1137.  */
  1138. char *savetemp(char *name, char *newname)
  1139. {
  1140.     int i, overwrite;
  1141.     static char buf[MAX_PATH];
  1142.  
  1143.     if (strcmp(name, newname) == 0)
  1144.     return name;
  1145.  
  1146.     for (i = 0; i < MAXTMPF; ++i)
  1147.     if (tmpf[i].flags && strcmp(tmpf[i].path, name) == 0)
  1148.         break;
  1149.  
  1150.     if (i < MAXTMPF) {
  1151.     if (strlen(name) < 4 || name[strlen(name) - 3] != TMP_EXT) {
  1152.         if (verbose)
  1153.         fprintf(pgpout, "savetemp: not renaming '%s' to '%s'\n",
  1154.             name, newname);
  1155.         return name;    /* return original file name */
  1156.     }
  1157.     }
  1158.     while (file_exists(newname)) {
  1159.     if (batchmode && !force_flag) {
  1160.         fprintf(pgpout, LANG("\n\007Output file '%s' already exists.\n"),
  1161.             newname);
  1162.         return NULL;
  1163.     }
  1164.     if (is_regular_file(newname)) {
  1165.         if (force_flag) {
  1166.         /* remove without asking */
  1167.         remove(newname);
  1168.         break;
  1169.         }
  1170.         fprintf(pgpout,
  1171.        LANG("\n\007Output file '%s' already exists.  Overwrite (y/N)? "),
  1172.             newname);
  1173.         overwrite = getyesno('n');
  1174.     } else {
  1175.         fprintf(pgpout,
  1176.             LANG("\n\007Output file '%s' already exists.\n"), newname);
  1177.         if (force_flag)    /* never remove special file */
  1178.         return NULL;
  1179.         overwrite = FALSE;
  1180.     }
  1181.  
  1182.     if (!overwrite) {
  1183.         fprintf(pgpout, LANG("\nEnter new file name: "));
  1184.         getstring(buf, MAX_PATH - 1, TRUE);
  1185.         if (buf[0] == '\0')
  1186.         return NULL;
  1187.         newname = buf;
  1188.     } else {
  1189.         remove(newname);
  1190.     }
  1191.     }
  1192.     if (verbose)
  1193.     fprintf(pgpout, "savetemp: renaming '%s' to '%s'\n", name, newname);
  1194.     if (rename2(name, newname) < 0) {
  1195.     /* errorLvl = UNKNOWN_FILE_ERROR; */
  1196.     fprintf(pgpout, LANG("Can't create output file '%s'\n"), newname);
  1197.     return NULL;
  1198.     }
  1199.     if (i < MAXTMPF)
  1200.     tmpf[i].flags = 0;
  1201.     return newname;
  1202. }                /* savetemp */
  1203.  
  1204. /*
  1205.  * like savetemp(), only make backup of destname if it exists
  1206.  */
  1207. int savetempbak(char *tmpname, char *destname)
  1208. {
  1209.     char bakpath[MAX_PATH];
  1210. #ifdef UNIX
  1211.     int mode = -1;
  1212. #endif
  1213.  
  1214.     if (is_tempfile(destname)) {
  1215.     remove(destname);
  1216.     } else {
  1217.     if (file_exists(destname)) {
  1218. #ifdef UNIX
  1219.         struct stat st;
  1220.         if (stat(destname, &st) != -1)
  1221.         mode = st.st_mode & 07777;
  1222. #endif
  1223.         strcpy(bakpath, destname);
  1224.         force_extension(bakpath, BAK_EXTENSION);
  1225.         remove(bakpath);
  1226. #ifdef VMS
  1227.         if (rename(destname, bakpath) != 0)
  1228. #else
  1229.         if (rename(destname, bakpath) == -1)
  1230. #endif
  1231.         return -1;
  1232.     }
  1233.     }
  1234.     if (savetemp(tmpname, destname) == NULL)
  1235.     return -1;
  1236. #ifdef UNIX
  1237.     if (mode != -1)
  1238.     chmod(destname, mode);
  1239. #endif
  1240.     return 0;
  1241. }
  1242.  
  1243. /*
  1244.  * remove all temporary files and wipe them if necessary
  1245.  */
  1246. void cleanup_tmpf(void)
  1247. {
  1248.     int i;
  1249.  
  1250.     for (i = 0; i < MAXTMPF; ++i)
  1251.     if (tmpf[i].flags)
  1252.         rmtemp(tmpf[i].path);
  1253. }                /* cleanup_tmpf */
  1254.  
  1255. /*
  1256.  * Routines to search for the manuals.
  1257.  *
  1258.  * Why all this code?
  1259.  *
  1260.  * Some people may object to PGP insisting on finding the manual somewhere
  1261.  * in the neighborhood to generate a key.  They bristle against this
  1262.  * seemingly authoritarian attitude.  Some people have even modified PGP
  1263.  * to defeat this feature, and redistributed their hotwired version to
  1264.  * others.  That creates problems for me (PRZ).
  1265.  *
  1266.  * Here is the problem.  Before I added this feature, there were maimed
  1267.  * versions of the PGP distribution package floating around that lacked
  1268.  * the manual.  One of them was uploaded to Compuserve, and was
  1269.  * distributed to countless users who called me on the phone to ask me why
  1270.  * such a complicated program had no manual.  It spread out to BBS systems
  1271.  * around the country.  And a freeware distributor got hold of the package
  1272.  * from Compuserve and enshrined it on CD-ROM, distributing thousands of
  1273.  * copies without the manual.  What a mess.
  1274.  *
  1275.  * Please don't make my life harder by modifying PGP to disable this
  1276.  * feature so that others may redistribute PGP without the manual.  If you
  1277.  * run PGP on a palmtop with no memory for the manual, is it too much to
  1278.  * ask that you type one little extra word on the command line to do a key
  1279.  * generation, a command that is seldom used by people who already know
  1280.  * how to use PGP?  If you can't stand even this trivial inconvenience,
  1281.  * can you suggest a better method of reducing PGP's distribution without
  1282.  * the manual?
  1283.  */
  1284.  
  1285. static unsigned ext_missing(char *prefix)
  1286. {
  1287.     static char const *const extensions[] =
  1288. #ifdef VMS
  1289.     { ".doc", ".txt", ".man", ".tex", ".", 0 };
  1290. #elif defined(RISC_OS)
  1291.     /* Sigh. -- GJM */
  1292.     { "/d", "/t", "/doc", "/txt", "", 0 };
  1293. #else
  1294.     { ".doc", ".txt", ".man", ".tex", "", 0 };
  1295. #endif
  1296.     char const *const *p;
  1297.     char *end = prefix + strlen(prefix);
  1298.  
  1299.     for (p = extensions; *p; p++) {
  1300.     strcpy(end, *p);
  1301. #if 0                /* Debugging code */
  1302.     fprintf(pgpout, "Looking for \"%s\"\n", prefix);
  1303. #endif
  1304.     if (file_exists(prefix))
  1305.         return 0;
  1306.     }
  1307.     return 1;
  1308. }
  1309.  
  1310. /*
  1311.  * Returns mask of files missing
  1312.  */
  1313. static unsigned files_missing(char *prefix)
  1314. {
  1315.     static char const *const names[] =
  1316.     {"pgpdoc1", "pgpdoc2", 0};
  1317.     char const *const *p;
  1318.     unsigned bit, mask = 3;
  1319.     int len = strlen(prefix);
  1320.  
  1321. #ifndef VMS
  1322.     /*
  1323.      * Optimization: if directory doesn't exist, stop.  But access()
  1324.      * (used internally by file_exists()) doesn't work on dirs under VMS.
  1325.      */
  1326.     if (prefix[0] && !file_exists(prefix))    /* Directory doesn't exist? */
  1327.     return mask;
  1328. #endif /* VMS */
  1329.     if (len && strchr(DIRSEPS, prefix[len - 1]) == 0)
  1330.     prefix[len++] = DIRSEPS[0];
  1331.     for (p = names, bit = 1; *p; p++, bit <<= 1) {
  1332.     strcpy(prefix + len, *p);
  1333.     if (!ext_missing(prefix))
  1334.         mask &= ~bit;
  1335.     }
  1336.  
  1337.     return mask;        /* Bitmask of which files exist */
  1338. }
  1339.  
  1340. /*
  1341.  * Search prefix directory and doc subdirectory.
  1342.  */
  1343. static unsigned doc_missing(char *prefix)
  1344. {
  1345.     unsigned mask;
  1346.     int len = strlen(prefix);
  1347.  
  1348.     mask = files_missing(prefix);
  1349.     if (!mask)
  1350.     return 0;
  1351. #ifdef VMS
  1352.     if (len && prefix[len - 1] == ']') {
  1353.     strcpy(prefix + len - 1, ".doc]");
  1354.     } else {
  1355.     assert(!len || prefix[len - 1] == ':');
  1356.     strcpy(prefix + len, "[doc]");
  1357.     }
  1358. #else
  1359.     if (len && prefix[len - 1] != DIRSEPS[0])
  1360.     prefix[len++] = DIRSEPS[0];
  1361.     strcpy(prefix + len, "doc");
  1362. #endif
  1363.  
  1364.     mask &= files_missing(prefix);
  1365.  
  1366.     prefix[len] = '\0';
  1367.     return mask;
  1368. }
  1369.  
  1370. /*
  1371.  * Expands a leading environment variable.  Returns 0 on success;
  1372.  * <0 if there is an error.
  1373.  */
  1374. static int expand_env(char const *src, char *dest)
  1375. {
  1376.     char const *var, *suffix;
  1377.     unsigned len;
  1378.  
  1379.     if (*src != '$') {
  1380.     strcpy(dest, src);
  1381.     return 0;
  1382.     }
  1383.  
  1384. #ifdef RISC_OS
  1385.     /* Aargh, '$' is the RISC OS name for the root directory.
  1386.      * Special-case this. -- GJM */
  1387.     if (src[1]=='.') {
  1388.       strcpy(dest,src);
  1389.       return 0;
  1390.     }
  1391. #endif
  1392.  
  1393.     /* Find end of variable */
  1394.     if (src[1] == '{') {    /* ${FOO} form */
  1395.     var = src + 2;
  1396.     len = strchr(var, '}') - var;
  1397.     suffix = src + 2 + len + 1;
  1398.     } else {            /* $FOO form - allow $ for VMS */
  1399.     var = src + 1;
  1400.     len = strspn(var, "ABCDEFGHIJKLMNOPQRSTUVWXYZ$_");
  1401.     suffix = src + 1 + len;
  1402.     }
  1403.  
  1404.     memcpy(dest, var, len);    /* Copy name */
  1405.     dest[len] = '\0';        /* Null-terminate */
  1406.  
  1407.     var = getenv(dest);
  1408.     if (!var || !*var)
  1409.     return -1;        /* No env variable */
  1410.  
  1411.     /* Copy expanded form to destination */
  1412.     strcpy(dest, var);
  1413.  
  1414.     /* Add tail */
  1415.     strcat(dest, suffix);
  1416.  
  1417.     return 0;
  1418. }
  1419.  
  1420. /* Don't forget to change 'pgp26' whenever you update rel_version past 2.6 */
  1421. char const *const manual_dirs[] =
  1422. {
  1423. #if defined(VMS)
  1424.     "$PGPPATH", "", "[pgp]", "[pgp26]", "[pgp262]",
  1425.     PGP_SYSTEM_DIR, "SYS$LOGIN:", "SYS$LOGIN:[pgp]",
  1426.     "SYS$LOGIN:[pgp26]", "SYS$LOGIN:[pgp262]", "[-]",
  1427. #elif defined(UNIX)
  1428.     "$PGPPATH", "", "pgp", "pgp26", "pgp262", PGP_SYSTEM_DIR,
  1429.     "$HOME/.pgp", "$HOME", "$HOME/pgp", "$HOME/pgp26", "..",
  1430. #elif defined(AMIGA)
  1431.     "$PGPPATH", "", "pgp", "pgp26", ":pgp", ":pgp26", ":pgp262",
  1432.     ":", "/",
  1433. #elif defined(RISC_OS)
  1434.     /* GJM */
  1435.     "$PGPPATH", "", "pgp", "pgp26", "$.pgp", "$.pgp26", "$.pgp262",
  1436.     LIBDIR, "$", "^",
  1437. #else                /* MSDOS or ATARI */
  1438.     "$PGPPATH", "", "pgp", "pgp26", "\\pgp", "\\pgp26", "\\pgp262",
  1439.     "\\", "..", "c:\\pgp", "c:\\pgp26",
  1440. #endif
  1441.     0};
  1442.  
  1443. unsigned manuals_missing(void)
  1444. {
  1445.     char buf[256];
  1446.     unsigned mask = ~((unsigned)0);
  1447.     char const *const *p;
  1448.  
  1449.     for (p = manual_dirs; *p; p++) {
  1450.     if (expand_env(*p, buf) < 0)
  1451.         continue;        /* Ignore */
  1452.     mask &= doc_missing(buf);
  1453.     if (!mask)
  1454.         break;
  1455.     }
  1456.  
  1457.     return mask;
  1458. }
  1459.  
  1460. /*
  1461.  * Why all this code?
  1462.  *
  1463.  * See block of comments above.
  1464.  */
  1465.