home *** CD-ROM | disk | FTP | other *** search
- /*
- * MS-DOS file I/O.
- */
- #include <sys/types.h>
- #include "def.h"
- #if UNIX
- #include <fcntl.h>
- #include <sys/stat.h>
- #endif
-
- extern char MSG_cnt_wr[];
- extern char MSG_wr_io_er[];
- extern char MSG_rd_er[];
- extern char MSG_bak[];
- extern char MSG_backup[];
- extern char MSG_back_er[];
- extern char MSG_back_of[];
-
- #include "lintfunc.dec"
-
- #if MSDOS
- static FILE * ffp;
- #endif
-
- #if UNIX
- static int ffp;
- #endif
-
- /*
- * Open a file for reading.
- */
- char ffropen (fn)
- char *fn;
- {
- #if MSDOS
- if ((ffp = fopen (fn, "rb")) == NULL)/* pvr */
- return (FIOERR);;
- return (FIOSUC);
- #endif
- #if UNIX
- if ((ffp = open (fn, O_RDONLY)) == -1)/* pvr */
- return (FIOERR);;
- return (FIOSUC);
- #endif
- }
- /*
- * Get the file length
- */
- A32 file_len ()
- {
- #if MSDOS
- return (filelength (fileno (ffp)));
- #endif
- #if UNIX
- struct stat st;
-
- if (fstat (ffp, &st) == -1)
- return (-1);
- return (st.st_size);
- #endif
- }
-
- /*
- * Open a file for writing.
- * Return TRUE if all is well, and
- * FALSE on error (cannot create).
- */
- char ffwopen (fn)
- char *fn;
- {
- #if MSDOS
- if ((ffp = fopen (fn, "wb")) == NULL)/* pvr */
- {
- writ_echo (MSG_cnt_wr);
- return (FIOERR);
- }
- return (FIOSUC);
- #endif
- #if UNIX
- if ((ffp = open (fn, O_WRONLY | O_CREAT)) == -1)/* pvr */
- return (FIOERR);;
- return (FIOSUC);
- #endif
- }
-
- /*
- * Close a file.
- * Should look at the status.
- */
- char ffclose ()
- {
- #if MSDOS
- fclose (ffp);
- #endif
- #if UNIX
- close (ffp);
- #endif
- return (FIOSUC);
- }
-
- /*
- * Write a line to the already
- * opened file. The "buf" points to the
- * buffer, and the "nbuf" is its length. pvr
- * Return the status.
- */
- char ffputline (buf, nbuf)
- register char buf[];
- int nbuf;
- {
- register int i;
-
- #if MSDOS
- i = fwrite (buf, 1, nbuf, ffp);
- #endif
- #if UNIX
- i = write (ffp, buf, nbuf);
- #endif
-
- if ((i != nbuf)
- #if MSDOS
- || (ferror (ffp) != FALSE))
- #else
- )
- #endif
- {
- writ_echo (MSG_wr_io_er);
- return (FIOERR);
- }
- return (FIOSUC);
- }
-
- /*
- * Read a line from a file, and store the bytes
- * in the supplied buffer. Stop on end of file or after 'nbuf' characters. pvr
- * the first byte in the buffer is the length in bytes.
- */
- char ffgetline (buf, nbuf, rbuf)
- register char *buf;
- register int *rbuf, nbuf;
- {
-
- register int c;
- register int i;
-
- #if MSDOS
- *rbuf = fread (buf, 1, nbuf, ffp);
- #endif
-
- #if UNIX
- *rbuf = read (ffp, buf, nbuf);
- #endif
-
- /* End of file. */
- #if MSDOS
- if (ferror (ffp) != FALSE)
- {
- writ_echo (MSG_rd_er);
- return (FIOERR);
- }
- #endif
- if (*rbuf == 0)
- return (FIOEOF);
-
- return (FIOSUC);
- }
-
- /*
- * Seek to specified position in file.
- * Return the actual position in the file.
- */
- A32 ffseek (posn)
- A32 posn;
- {
- #if MSDOS
- fseek (ffp, posn, SEEK_SET);
- return (ftell (ffp));
- #endif
- #if UNIX
- return (lseek (ffp, posn, 0));
- #endif
- }
-
- /*
- * Some backup user on MS-DOS might want
- * to determine some rule for doing backups on that
- * system, and fix this. I don't use MS-DOS, so I don't
- * know what the right rules would be. Return TRUE so
- * the caller does not abort a write.
- */
- #if BACKUP
- bool fbackupfile (fname)
- char *fname;
- {
- FILE * backupfile;
- char backname[128];
- char *source,
- *backup;
- extern char time_string[];
- char buf[80];
-
- source = fname;
- backup = backname;
- while ((*source > 0) && (*source != '.'))
- {
- *backup = *source;
- backup++;
- source++;
- *backup = 0;
- }
- strcat (backname, MSG_bak);
- sprintf (buf, MSG_backup, fname, backname);
- writ_echo (buf);
- unlink (backname);
- #if MSDOS
- if (rename (backname, fname) > 0)
- #else
- if ((link (fname, backname) != 0) || (unlink (fname) != 0))
- #endif
- {
- sprintf (buf, MSG_back_er, fname, backname);
- writ_echo (buf);
- return (FALSE);
- }
- return (TRUE); /* Hack. */
- }
-
- #endif
-
- /*
- * The string "fn" is a file name.
- * Perform any required case adjustments. All systems
- * we deal with so far have case insensitive file systems.
- * We zap everything to lower case. The problem we are trying
- * to solve is getting 2 buffers holding the same file if
- * you visit one of them with the "caps lock" key down.
- * On UNIX file names are dual case, so we leave
- * everything alone.
- */
- void adjustcase (fn)
- register char *fn;
- {
- register int c;
-
- while ((c = *fn) != 0)
- {
- if (c >= 'A' && c <= 'Z')
- *fn = c + 'a' - 'A';
- ++fn;
- }
- }
-
-
-