static int readMap(const char *fileName, keyMap *map) { int status = 0; fileStream theStream; memset(&theStream, 0, sizeof(fileStream)); status = fileStreamOpen(fileName, OPENMODE_READ, &theStream); if (status < 0) { error(_("Couldn't open file %s"), fileName); return (status); } status = fileStreamRead(&theStream, sizeof(keyMap), (char *) map); fileStreamClose(&theStream); if (status < 0) { error(_("Couldn't read file %s"), fileName); return (status); } // Check the magic number if (strncmp(map->magic, KEYMAP_MAGIC, sizeof(KEYMAP_MAGIC))) return (status = ERR_BADDATA); return (status = 0); }
static int saveMap(const char *fileName) { int status = 0; disk mapDisk; fileStream theStream; int count; memset(&mapDisk, 0, sizeof(disk)); memset(&theStream, 0, sizeof(fileStream)); // Find out whether the file is on a read-only filesystem if (!fileGetDisk(fileName, &mapDisk) && mapDisk.readOnly) { error(_("Can't write %s:\nFilesystem is read-only"), fileName); return (status = ERR_NOWRITE); } if (graphics && nameField) // Get the map name windowComponentGetData(nameField, selectedMap->name, 32); for (count = 0; count < KEYBOARD_SCAN_CODES; count ++) { selectedMap->regMap[count] = keyArray[count].regMap; selectedMap->shiftMap[count] = keyArray[count].shiftMap; selectedMap->controlMap[count] = keyArray[count].controlMap; selectedMap->altGrMap[count] = keyArray[count].altGrMap; } status = fileStreamOpen(fileName, (OPENMODE_CREATE | OPENMODE_WRITE | OPENMODE_TRUNCATE), &theStream); if (status < 0) { error(_("Couldn't open file %s"), fileName); return (status); } status = fileStreamWrite(&theStream, sizeof(keyMap), (char *) selectedMap); fileStreamClose(&theStream); if (status < 0) error(_("Couldn't write file %s"), fileName); return (status); }
static void ChibiOSBFSClose(GFILE *f) { fileStreamClose(((FileStream *)f->obj)); }
FILE *fopen(const char *fileName, const char *mode) { // Excerpted from the GNU man page: // // The fopen() function opens the file whose name is the string pointed to // by fileName and associates a stream with it. // // The argument mode points to a string beginning with one of the // following sequences (Additional characters may follow these sequences.): // // r Open text file for reading. The stream is positioned at the // beginning of the file. // // r+ Open for reading and writing. The stream is positioned at the // beginning of the file. // // w Truncate file to zero length or create text file for writing. // The stream is positioned at the beginning of the file. // // w+ Open for reading and writing. The file is created if it does // not exist, otherwise it is truncated. The stream is positioned // at the beginning of the file. // // a Open for appending (writing at end of file). The file is created // if it does not exist. The stream is positioned at the end // of the file. // // a+ Open for reading and appending (writing at end of file). The // file is created if it does not exist. The initial file position // for reading is at the beginning of the file, but output is // always appended to the end of the file. // // N.B.: The a+ argument will always be broken in Visopsys. Reading from // the start of the file and writing to the end of the file are silly // IMO and have no equivalent in Visopsys. int status = 0; fileStream *theStream = NULL; int flags = 0; int append = 0; if (visopsys_in_kernel) { errno = ERR_BUG; return (theStream = NULL); } // Check params if (!fileName || !mode) { errno = ERR_NULLPARAMETER; return (theStream = NULL); } // We have to convert the text string mode arguments to our flags if (strstr(mode, "r+")) flags |= OPENMODE_READWRITE; else if (strchr(mode, 'r')) flags |= OPENMODE_READ; if (strstr(mode, "w+")) flags |= (OPENMODE_READWRITE | OPENMODE_CREATE | OPENMODE_TRUNCATE); else if (strchr(mode, 'w')) flags |= (OPENMODE_WRITE | OPENMODE_CREATE | OPENMODE_TRUNCATE); if (strstr(mode, "a+")) { flags |= (OPENMODE_READWRITE | OPENMODE_CREATE); append = 1; } else if (strchr(mode, 'a')) { flags |= (OPENMODE_WRITE | OPENMODE_CREATE); append = 1; } // Get memory for the file stream theStream = malloc(sizeof(fileStream)); if (!theStream) { errno = ERR_MEMORY; return (theStream); } memset(theStream, 0, sizeof(fileStream)); status = fileStreamOpen(fileName, flags, theStream); if (status < 0) { errno = status; free(theStream); return (theStream = NULL); } // If we're only writing, and not appending, seek to the beginning of the // file, since the fileStreamOpen() call is automatically in 'append' mode // when the mode is write-only. if (OPENMODE_ISWRITEONLY(flags) && !append) { status = fileStreamSeek(theStream, 0); if (status < 0) { errno = status; fileStreamClose(theStream); free(theStream); return (theStream = NULL); } } return ((FILE *) theStream); }