int open(far char *name, int flags, mode_t mode) { int if1_filestatus; struct M_CHAN *if1_file; //if (if1_file = malloc(sizeof(struct M_CHAN)) == 0) // return (-1); if1_file = malloc(sizeof(struct M_CHAN)); if (if1_file == 0) return (-1); if1_filestatus = if1_load_record(1, (char *)name, 0, if1_file); switch (mode) { case O_RDONLY: if (if1_filestatus == -1) { // FILE NOT FOUND free(if1_file); return(-1); } /* DEBUGGING: "hdname" could be useful to check if the disk has been changed printf("\nread only : %u - %s\n",if1_file, if1_getname( (char *) ((if1_file)->name)) ); printf ("--%s--",if1_getname( (char*) ((if1_file)->hdname)) ); */ // RESET FILE COUNTER (if1_file)->position=0; (if1_file)->flags=flags; (if1_file)->mode=mode; return(if1_file); break; case O_WRONLY: if (if1_filestatus != -1) { //printf("\nfile already exists\n"); free(if1_file); return(-1); } return(-1); // not still implemented break; case O_APPEND: if (if1_filestatus == -1) { // FILE NOT FOUND free(if1_file); return(-1); } (if1_file)->position=0; (if1_file)->flags=flags; (if1_file)->mode=mode; lseek((if1_file), 0, SEEK_END); break; } return(-1); }
int rename(char *oldname, char *newname) { int blkcount, currblock; struct M_CHAN mybuf; // check if "newname" already exists if (if1_load_record (1, newname, 0, mybuf) != -1 ) return (-1); // load first file record and check for its existence if ((currblock = if1_load_record (1, oldname, 0, mybuf)) == -1 ) return (-1); /* now rename every file record we skip the record no. 255 to avoid loops */ for (blkcount=1; blkcount < 255; blkcount++) { if1_setname(newname,&mybuf->recname[0]); if (if1_write_sector (1, currblock, mybuf) == -1) return (-1); currblock = if1_load_record (1, oldname, blkcount, mybuf); if (currblock == -1) return 0; } return (0); }