file reorg, pathnames.h, paths.h
[unix-history] / usr / src / usr.bin / ex / ex3.7recover / ex3.7recover.c
index 218e65a..5437dd4 100644 (file)
@@ -1,23 +1,36 @@
-/* Copyright (c) 1979 Regents of the University of California */
-#include "ex.h"
-#include "ex_temp.h"
-#include "ex_tty.h"
-#include "local/uparm.h"
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char *copyright =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
 
 
-#undef BUFSIZ
-#undef EOF
+#ifndef lint
+static char *sccsid = "@(#)ex3.7recover.c      7.10 (Berkeley) %G%";
+#endif not lint
+
+#include <stdio.h>     /* mjm: BUFSIZ: stdio = 512, VMUNIX = 1024 */
+#undef BUFSIZ          /* mjm: BUFSIZ different */
+#undef EOF             /* mjm: EOF and NULL effectively the same */
 #undef NULL
 
 #undef NULL
 
-#include <stdio.h>
+#include "ex.h"
+#include "ex_temp.h"
+#include "ex_tty.h"
 #include <sys/dir.h>
 
 #include <sys/dir.h>
 
+char xstr[1];          /* make loader happy */
+short tfile = -1;      /* ditto */
+
 /*
 /*
- * Ex recovery program
- *     exrecover dir name
- *     exrecover -r
  *
  * This program searches through the specified directory and then
  *
  * This program searches through the specified directory and then
- * the directory usrpath(preserve) looking for an instance of the specified
+ * the directory _PATH_USRPRESERVE looking for an instance of the specified
  * file from a crashed editor or a crashed system.
  * If this file is found, it is unscrambled and written to
  * the standard output.
  * file from a crashed editor or a crashed system.
  * If this file is found, it is unscrambled and written to
  * the standard output.
 #define        ignorl(a)       a
 #endif
 
 #define        ignorl(a)       a
 #endif
 
-/*
- * This directory definition also appears (obviously) in expreserve.c.
- * Change both if you change either.
- */
-char   mydir[] =       usrpath(preserve);
-
 /*
  * Limit on the number of printed entries
  * when an, e.g. ``ex -r'' command is given.
 /*
  * Limit on the number of printed entries
  * when an, e.g. ``ex -r'' command is given.
@@ -72,7 +79,7 @@ main(argc, argv)
         * If given only a -r argument, then list the saved files.
         */
        if (argc == 2 && eq(argv[1], "-r")) {
         * If given only a -r argument, then list the saved files.
         */
        if (argc == 2 && eq(argv[1], "-r")) {
-               listfiles(mydir);
+               listfiles(_PATH_PRESERVE);
                exit(0);
        }
        if (argc != 3)
                exit(0);
        }
        if (argc != 3)
@@ -138,7 +145,7 @@ main(argc, argv)
         */
        if (dol > zero) {
                addr1 = one; addr2 = dol; io = 1;
         */
        if (dol > zero) {
                addr1 = one; addr2 = dol; io = 1;
-               putfile();
+               putfile(0);
        }
 
        /*
        }
 
        /*
@@ -195,24 +202,24 @@ error(str, inf)
 struct svfile {
        char    sf_name[FNSIZE + 1];
        int     sf_lines;
 struct svfile {
        char    sf_name[FNSIZE + 1];
        int     sf_lines;
-       char    sf_entry[DIRSIZ + 1];
+       char    sf_entry[MAXNAMLEN + 1];
        time_t  sf_time;
 };
 
 listfiles(dirname)
        char *dirname;
 {
        time_t  sf_time;
 };
 
 listfiles(dirname)
        char *dirname;
 {
-       register FILE *dir;
-       struct direct dirent;
+       register DIR *dir;
+       struct direct *dirent;
        int ecount, qucmp();
        register int f;
        char *cp;
        struct svfile *fp, svbuf[NENTRY];
 
        /*
        int ecount, qucmp();
        register int f;
        char *cp;
        struct svfile *fp, svbuf[NENTRY];
 
        /*
-        * Open usrpath(preserve), and go there to make things quick.
+        * Open _PATH_PRESERVE, and go there to make things quick.
         */
         */
-       dir = fopen(dirname, "r");
+       dir = opendir(dirname);
        if (dir == NULL) {
                perror(dirname);
                return;
        if (dir == NULL) {
                perror(dirname);
                return;
@@ -223,17 +230,15 @@ listfiles(dirname)
        }
 
        /*
        }
 
        /*
-        * Look at the candidate files in usrpath(preserve).
+        * Look at the candidate files in _PATH_PRESERVE.
         */
        fp = &svbuf[0];
        ecount = 0;
         */
        fp = &svbuf[0];
        ecount = 0;
-       while (fread((char *) &dirent, sizeof dirent, 1, dir) == 1) {
-               if (dirent.d_ino == 0)
-                       continue;
-               if (dirent.d_name[0] != 'E')
+       while ((dirent = readdir(dir)) != NULL) {
+               if (dirent->d_name[0] != 'E')
                        continue;
 #ifdef DEBUG
                        continue;
 #ifdef DEBUG
-               fprintf(stderr, "considering %s\n", dirent.d_name);
+               fprintf(stderr, "considering %s\n", dirent->d_name);
 #endif
                /*
                 * Name begins with E; open it and
 #endif
                /*
                 * Name begins with E; open it and
@@ -241,7 +246,7 @@ listfiles(dirname)
                 * If not, then don't bother with this file, it can't
                 * be ours.
                 */
                 * If not, then don't bother with this file, it can't
                 * be ours.
                 */
-               f = open(dirent.d_name, 0);
+               f = open(dirent->d_name, 0);
                if (f < 0) {
 #ifdef DEBUG
                        fprintf(stderr, "open failed\n");
                if (f < 0) {
 #ifdef DEBUG
                        fprintf(stderr, "open failed\n");
@@ -266,13 +271,13 @@ listfiles(dirname)
                /*
                 * Saved the day!
                 */
                /*
                 * Saved the day!
                 */
-               enter(fp++, dirent.d_name, ecount);
+               enter(fp++, dirent->d_name, ecount);
                ecount++;
 #ifdef DEBUG
                ecount++;
 #ifdef DEBUG
-               fprintf(stderr, "entered file %s\n", dirent.d_name);
+               fprintf(stderr, "entered file %s\n", dirent->d_name);
 #endif
        }
 #endif
        }
-       ignore(fclose(dir));
+       ignore(closedir(dir));
 
        /*
         * If any files were saved, then sort them and print
 
        /*
         * If any files were saved, then sort them and print
@@ -363,7 +368,7 @@ int bestfd;                 /* Keep best file open so it dont vanish */
 
 /*
  * Look for a file, both in the users directory option value
 
 /*
  * Look for a file, both in the users directory option value
- * (i.e. usually /tmp) and in usrpath(preserve).
+ * (i.e. usually /tmp) and in _PATH_PRESERVE.
  * Want to find the newest so we search on and on.
  */
 findtmp(dir)
  * Want to find the newest so we search on and on.
  */
 findtmp(dir)
@@ -377,12 +382,12 @@ findtmp(dir)
        bestfd = -1;
 
        /*
        bestfd = -1;
 
        /*
-        * Search usrpath(preserve) and, if we can get there, /tmp
+        * Search _PATH_PRESERVE and, if we can get there, /tmp
         * (actually the users "directory" option).
         */
        searchdir(dir);
         * (actually the users "directory" option).
         */
        searchdir(dir);
-       if (chdir(mydir) == 0)
-               searchdir(mydir);
+       if (chdir(_PATH_PRESERVE) == 0)
+               searchdir(_PATH_PRESERVE);
        if (bestfd != -1) {
                /*
                 * Gotcha.
        if (bestfd != -1) {
                /*
                 * Gotcha.
@@ -414,7 +419,7 @@ findtmp(dir)
  * Don't chdir here, because the users directory
  * may be ".", and we would move away before we searched it.
  * Note that we actually chdir elsewhere (because it is too slow
  * Don't chdir here, because the users directory
  * may be ".", and we would move away before we searched it.
  * Note that we actually chdir elsewhere (because it is too slow
- * to look around in usrpath(preserve) without chdir'ing there) so we
+ * to look around in _PATH_PRESERVE without chdir'ing there) so we
  * can't win, because we don't know the name of '.' and if the path
  * name of the file we want to unlink is relative, rather than absolute
  * we won't be able to find it again.
  * can't win, because we don't know the name of '.' and if the path
  * name of the file we want to unlink is relative, rather than absolute
  * we won't be able to find it again.
@@ -422,18 +427,15 @@ findtmp(dir)
 searchdir(dirname)
        char *dirname;
 {
 searchdir(dirname)
        char *dirname;
 {
-       struct direct dirent;
-       register FILE *dir;
+       struct direct *dirent;
+       register DIR *dir;
        char dbuf[BUFSIZ];
 
        char dbuf[BUFSIZ];
 
-       dir = fopen(dirname, "r");
+       dir = opendir(dirname);
        if (dir == NULL)
                return;
        if (dir == NULL)
                return;
-       setbuf(dir, dbuf);
-       while (fread((char *) &dirent, sizeof dirent, 1, dir) == 1) {
-               if (dirent.d_ino == 0)
-                       continue;
-               if (dirent.d_name[0] != 'E' || dirent.d_name[DIRSIZ - 1] != 0)
+       while ((dirent = readdir(dir)) != NULL) {
+               if (dirent->d_name[0] != 'E')
                        continue;
                /*
                 * Got a file in the directory starting with E...
                        continue;
                /*
                 * Got a file in the directory starting with E...
@@ -441,7 +443,7 @@ searchdir(dirname)
                 * later, and check that this is really a file
                 * we are looking for.
                 */
                 * later, and check that this is really a file
                 * we are looking for.
                 */
-               ignore(strcat(strcat(strcpy(nb, dirname), "/"), dirent.d_name));
+               ignore(strcat(strcat(strcpy(nb, dirname), "/"), dirent->d_name));
                if (yeah(nb)) {
                        /*
                         * Well, it is the file we are looking for.
                if (yeah(nb)) {
                        /*
                         * Well, it is the file we are looking for.
@@ -464,7 +466,7 @@ searchdir(dirname)
                }
                ignore(close(tfile));
        }
                }
                ignore(close(tfile));
        }
-       ignore(fclose(dir));
+       ignore(closedir(dir));
 }
 
 /*
 }
 
 /*
@@ -760,3 +762,20 @@ syserror()
                error("System error %d", errno);
        exit(1);
 }
                error("System error %d", errno);
        exit(1);
 }
+
+/*
+ * Must avoid stdio because expreserve uses sbrk to do memory
+ * allocation and stdio uses malloc.
+ */
+fprintf(fp, fmt, a1, a2, a3, a4, a5)
+       FILE *fp;
+       char *fmt;
+       char *a1, *a2, *a3, *a4, *a5;
+{
+       char buf[BUFSIZ];
+
+       if (fp != stderr)
+               return;
+       sprintf(buf, fmt, a1, a2, a3, a4, a5);
+       write(2, buf, strlen(buf));
+}