home *** CD-ROM | disk | FTP | other *** search
- /*
- * Name: MicroEMACS
- * MSDOS file I/O (MSC4.0)
- */
- #include "def.h"
-
- #ifndef F_OK
- #define F_OK 0
- #define X_OK 1
- #define W_OK 2
- #define R_OK 4
- #endif
-
- /*
- * Move bytes, overlaps OK (daveb).
- *
- * MS/C 1.0 provides "memcpy", rather than bcopy.
- */
- bcopy( from, to, cnt )
- char *from;
- char *to;
- int cnt;
- {
- memcpy(to, from, cnt);
- }
-
-
- static FILE *ffp;
-
- /*
- * Open a file for reading.
- */
- ffropen(fn)
- char *fn;
- {
- if ((ffp=fopen(fn, "rb")) == NULL)
- return (FIOFNF);
- return (FIOSUC);
- }
-
- /*
- * Open a file for writing.
- * Return TRUE if all is well, and
- * FALSE on error (cannot create).
- */
- ffwopen(fn)
- char *fn;
- {
- if ((ffp=fopen(fn, "wb")) == NULL) {
- ewprintf("Cannot open file for writing");
- return (FIOERR);
- }
- return (FIOSUC);
- }
-
- /*
- * Close a file.
- * Should look at the status.
- */
- ffclose()
- {
- (VOID) fclose(ffp);
- return (FIOSUC);
- }
-
- /*
- * Write a line to the already
- * opened file. The "buf" points to the
- * buffer, and the "nbuf" is its length, less
- * the free newline. Return the status.
- * Check only at the newline.
- */
- ffputline(buf, nbuf)
- register char buf[];
- {
- register int i;
-
- for (i=0; i<nbuf; ++i)
- putc(buf[i]&0xFF, ffp);
- putc('\r', ffp); /* MSDOS wants \r\n line seperators */
- putc('\n', ffp);
- if (ferror(ffp) != FALSE) {
- ewprintf("Write I/O error");
- return (FIOERR);
- }
- return (FIOSUC);
- }
-
- /*
- * Read a line from a file, and store the bytes
- * in the supplied buffer. Stop on end of file or end of
- * line. Don't get upset by files that don't have an end of
- * line on the last line; this seem to be common on CP/M-86 and
- * MS-DOS (the suspected culprit is VAX/VMS kermit, but this
- * has not been confirmed. If this is sufficiently researched
- * it may be possible to pull this kludge). Delete any CR
- * followed by an LF. This is mainly for runoff documents,
- * both on VMS and on Ultrix (they get copied over from
- * VMS systems with DECnet).
- */
- ffgetline(buf, nbuf)
- register char buf[];
- {
- register int c;
- register int i;
-
- i = 0;
- for (;;) {
- c = getc(ffp);
- if (c == '\r') { /* Delete any non-stray */
- c = getc(ffp); /* carriage returns. */
- if (c != '\n') {
- if (i >= nbuf-1) {
- ewprintf("File has long line");
- return (FIOERR);
- }
- buf[i++] = '\r';
- }
- }
- if (c==EOF || c=='\n') /* End of line. */
- break;
- if (i >= nbuf-1) {
- ewprintf("File has long line");
- return (FIOERR);
- }
- buf[i++] = c;
- }
- if (c == EOF) { /* End of file. */
- if (ferror(ffp) != FALSE) {
- ewprintf("File read error");
- return (FIOERR);
- }
- if (i == 0) /* Don't get upset if */
- return (FIOEOF); /* no newline at EOF. */
- }
- buf[i] = 0;
- return (FIOSUC);
- }
-
- #ifdef BACKUP
- /*
- * Rename the file "fname" into a backup copy.
- * On Unix the backup has the same name as the
- * original file, with a "~" on the end - unfortunately
- * this does not map well to MS-DOS - the old .bak convention
- * is used.
- */
- fbackupfile(fname)
- char *fname;
- {
- register char *nname, *ptr;
- char *strchr();
-
- if ((nname=malloc(strlen(fname)+3+1)) == NULL)
- return (ABORT);
- (void) strcpy(nname, fname);
- if (ptr = strchr(nname, '.'))
- strcpy(ptr, ".bak")
- else
- strcat(ptr, ".bak")
-
- if (strcmp(fname, nname) == 0) {
- free(nname);
- return FALSE;
- }
-
- (void) unlink(nname); /* Ignore errors. */
- (void) rename(fname, nname);
- free(nname);
- return (TRUE);
- }
- #endif
-
- /*
- * The string "fn" is a file name.
- * Perform any required case adjustments. All sustems
- * 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.
- */
- /*ARGSUSED*/
- adjustcase(fn)
- register char *fn;
- {
- register int c;
-
- while ((c = *fn) != 0) {
- if (c>='A' && c<='Z')
- *fn = c + 'a' - 'A';
- ++fn;
- }
- }
-
-
-
- #ifdef STARTUP
- #include <sys/types.h>
- #define STARTUPNAME ".mg"
- /*
- * find the users startup file, and return it's name. Check for
- * if MGSTARTUP is defined, then use that. Otherwise, look
- * for .mg in the current directory, then in the root directory.
- */
- char *
- startupfile()
- {
- register char *file;
- static char temp[NFILEN];
- char *getenv();
-
- if ((file = getenv("MGSTARTUP")) != NULL )
- {
- if (access(file, F_OK ) == 0)
- return file;
- return NULL;
- }
- if (access (STARTUPNAME, F_OK) == 0)
- return STARTUPNAME;
- strcpy(temp, "/");
- strcat(temp, STARTUPNAME);
- if (access (temp, F_OK) == 0)
- return temp;
- return NULL;
- }
- #endif
-