💾 Archived View for uscoffings.net › retro-computing › systems › TI994a › assemblers › tiasm › src ›… captured on 2022-06-04 at 01:13:47.

View Raw

More Information

-=-=-=-=-=-=-

/* File utilities, open and close */

#include <stdio.h>
#include <sys/param.h>

/* For mapping purposes */
FILE	*Sdvec[NOFILE];

/* xopen
 * Open a file buffered but make it look like the UNIX "open" sys call.
 *
 * returns:
 *	fd for a successful open.
 *	-1 for a failure
 */
int xopen (file, mode)
char	*file;
int	mode;
{
	char *md;
	int fd;
	extern FILE *fdopen();

	switch (mode) {

	    case 0:	/* read */
		md = "r";
		break;
	    case 1:	/* write */
		md = "w";
		break;
	    case 2:	/* read & write */
		md = "r+";
		break;
	    default:
		return (-1);
	}

	if (file == NULL || (fd = open(file, mode) < 0))
		return (-1);
	if ((Sdvec[fd] = fdopen(fd, md)) == NULL) {
		close (fd);
		return (-1);
	}
 return (fd);
}

/* xclose
 * close a file
 *
 * returns:
 *	0 if succesful
 *	-1 for error
 */
int xclose (fd)
int	fd;
{

	if (fd < 0 || fd >= NOFILE)
		return (-1);
	if (fclose (Sdvec[fd]) == EOF)
		return (-1);
	Sdvec[fd] = NULL;
	return (0);
}

/* xcreat
 * create a file
 *
 * returns:
 *	fildes	for success
 *	-1 on error
 */
int xcreat (file, modes)
char	*file;
int	modes;
{
	int fd;
	FILE *fdopen();

	if (file == NULL || (fd = creat(file, modes)) < 0)
		return (-1);
	if ((Sdvec[fd] = fdopen(fd, "w+")) == NULL)
		return (-1);
	return (fd);
}