/** * open unit * * @param file is the filename * @return the unit handle or -1 on error */ int open_unit(const char *file) { int h, i; unit_t u; int uid = -1; char unitname[OS_PATHNAME_SIZE]; char bas_file[OS_PATHNAME_SIZE]; time_t ut, st; int comp_rq = 0; // clean structure please memset(&u, 0, sizeof(unit_t)); // find unit's source file name if (!find_unit_path(file, bas_file)) { return -1; } // create corresponding sbu path version strcpy(unitname, bas_file); unitname[strlen(bas_file) - 4] = 0; strcat(unitname, ".sbu"); if ((ut = sys_filetime(unitname)) == 0L) { // binary not found - compile comp_rq = 1; } else { if ((st = sys_filetime(bas_file))) { // source found if (ut < st) { // executable is older than source - compile comp_rq = 1; } } } // compilation required if (comp_rq) { if (!comp_compile(bas_file)) { return -1; } } // open unit h = open(unitname, O_RDWR | O_BINARY, 0660); if (h == -1) { return -1; } // read file header read(h, &u.hdr, sizeof(unit_file_t)); if (memcmp(&u.hdr.sign, "SBUn", 4) != 0) { close(h); return -2; } // load symbol-table u.symbols = (unit_sym_t *) malloc(u.hdr.sym_count * sizeof(unit_sym_t)); read(h, u.symbols, u.hdr.sym_count * sizeof(unit_sym_t)); // setup the rest strcpy(u.name, unitname); u.status = unit_loaded; // add unit if (unit_count == 0) { // this is the first unit uid = unit_count; unit_count++; units = (unit_t *) malloc(unit_count * sizeof(unit_t)); } else { // search for an empty entry for (i = 0; i < unit_count; i++) { if (units[i].status == unit_undefined) { uid = i; break; } } // resize the table if (uid == -1) { uid = unit_count; unit_count++; units = (unit_t *) realloc(units, unit_count * sizeof(unit_t)); } } // copy unit's data memcpy(&units[uid], &u, sizeof(unit_t)); // cleanup close(h); return uid; }
/** * open unit * * @param file is the filename * @return the unit handle or -1 on error */ int open_unit(const char *file) { int h; unit_t u; int uid = -1; char unitname[OS_PATHNAME_SIZE]; char bas_file[OS_PATHNAME_SIZE]; time_t ut, st; int comp_rq = 0; // clean structure please memset(&u, 0, sizeof(unit_t)); // find unit's source file name if (!find_unit_path(file, bas_file)) { return -1; } // create corresponding sbu path version strcpy(unitname, bas_file); if (strcmp(comp_file_name, bas_file) == 0) { // unit and program are the same return -1; } unitname[strlen(bas_file) - 4] = 0; strcat(unitname, ".sbu"); if ((ut = sys_filetime(unitname)) == 0L) { // binary not found - compile comp_rq = 1; } else { if ((st = sys_filetime(bas_file))) { // source found if (ut < st) { // executable is older than source - compile comp_rq = 1; } } } // compilation required if (comp_rq && !comp_compile(bas_file)) { return -1; } // open unit h = open(unitname, O_RDWR | O_BINARY, 0660); if (h == -1) { return -1; } // read file header int nread = read(h, &u.hdr, sizeof(unit_file_t)); if (nread != sizeof(unit_file_t) || u.hdr.version != SB_DWORD_VER || memcmp(&u.hdr.sign, "SBUn", 4) != 0) { close(h); return -1; } // load symbol-table if (u.hdr.sym_count) { u.symbols = (unit_sym_t *)malloc(u.hdr.sym_count * sizeof(unit_sym_t)); read(h, u.symbols, u.hdr.sym_count * sizeof(unit_sym_t)); } // setup the rest strcpy(u.name, unitname); u.status = unit_loaded; // add unit uid = unit_count; unit_count++; if (units == NULL) { units = (unit_t *)malloc(unit_count * sizeof(unit_t)); } else { units = (unit_t *)realloc(units, unit_count * sizeof(unit_t)); } // copy unit's data memcpy(&units[uid], &u, sizeof(unit_t)); // cleanup close(h); return uid; }