bool IpcCallExtra(handle_t ipc, uint32_t code, const wnd_params_t *params, const void *extra, size_t extra_length) { ipc_packet_t packet = { 0 }; if (ipc == NULL) ipc = IpcGetDefault(); packet.code = code; if (params != NULL) packet.params = *params; packet.extra_length = extra_length; errno = 0; if (!FsWrite(ipc, &packet, 0, sizeof(packet), NULL)) { _wdprintf(L"%s: IpcCallExtra: FsWrite(1) failed: %s\n", ProcGetProcessInfo()->module_first->name, _wcserror(errno)); //return false; } if (extra_length > 0) { if (!FsWrite(ipc, extra, 0, extra_length, NULL)) { _wdprintf(L"%s: IpcCallExtra: FsWrite(2) failed: %s\n", ProcGetProcessInfo()->module_first->name, _wcserror(errno)); //return false; } } return true; }
int DiskCreate() { char *filename = NULL; int fsfd = -1; disk_block b; int i; // Check that you remembered to rename the filename for your group filename = DISK_FILENAME; if (filename[11] == 'X') { printf("DiskCreate: you didn't change the filesystem filename in include/os/disk.h. Cowardly refusing to do anything.\n"); GracefulExit(); } // Open the hard disk file if ((fsfd = FsOpen(DISK_FILENAME, FS_MODE_WRITE)) < 0) { printf ("DiskCreate: File system %s cannot be opened!\n", DISK_FILENAME); return DISK_FAIL; } // Write all zeros to the hard disk file to make sure it is the right size. // You need to do this because the writeblock/readblock operations are allowed in // random order. bzero(b.data, DISK_BLOCKSIZE); for(i=0; i<DISK_NUMBLOCKS; i++) { FsWrite(fsfd, b.data, DISK_BLOCKSIZE); } // Close the hard disk file if (FsClose(fsfd) < 0) { printf("DiskCreate: unable to close open file!\n"); return DISK_FAIL; } return DISK_SUCCESS; }
//============================================================== void MakeBackupFile(const char *fname) //-------------------------------------------------------------- // PATH_BACKUP にバックアップをとる //-------------------------------------------------------------- // in: file = バックアップを取るファイル名 //-------------------------------------------------------------- // out: なし //============================================================== { char d_name[FNAME_MAXLEN]; void *tmp_buf; sFILE *fd_src, *fd_dst; size_t len, size; // オリジナルのファイルを開く //---------------------------- fd_src = FsOpen(fname); if(!fd_src) { return; } // バックアップファイルの作成 //---------------------------- sprintf(d_name, PATH_BACKUP"/%s.%s", GetFileBaseName(fname), StrMakeUniqueName()); fd_dst = FsCreate(d_name); if (!fd_dst) { FsClose(fd_src); return; } // 作業バッファを確保 //-------------------- tmp_buf = devMalloc(TMPBUF_LEN, "MakeBackupFile"); len = FsGetSizeOrig(fd_src); while(len) { size = (len >= TMPBUF_LEN) ? TMPBUF_LEN : len; FsRead(fd_src, tmp_buf, size); FsWrite(fd_dst, tmp_buf, size); len -= size; } FsClose(fd_src); FsClose(fd_dst); Free(tmp_buf); // SYSINFODSP("Make Backup file"); // SYSINFODSP("%s", MSG_FILE(d_name)); // SYSINFO("Make Backup file"); // SYSINFO("%s", MSG_FILE(d_name)); // SYSINFOCNS("Make Backup file"); // SYSINFOCNS("%s", MSG_FILE(d_name)); }
void InputRecordWrite(sFILE *fp) { if(record && record_ofs) { FileBuffer buffer; buffer.ptr = (u_char *)fsMalloc(256, "byteValue"); buffer.ofs = 0; buffer.size = 256; sInputRecord *ptr = record; for(int i = 0; i < record_ofs; i += 1) { putIntValue(&buffer, (u_int)ptr->x); putIntValue(&buffer, (u_int)ptr->y); putIntValue(&buffer, ptr->btn_p); putIntValue(&buffer, ptr->btn_td); putIntValue(&buffer, ptr->btn_tu); RecKey *recKey = ptr->recKey; for(int h = 0; h < KEY_INPUT_BUFFER * 2; h += 1) { putCharValue(&buffer, recKey->key); putCharValue(&buffer, recKey->push); recKey += 1; } putIntValue(&buffer, ptr->recNum); ptr += 1; } u_int header; header = htonl(record_ofs); FsWrite(fp, &header, sizeof(u_int)); void *zptr = ZlibEncode(buffer.ptr, buffer.ofs); FsWrite(fp, zptr, ZlibEncodeSize(zptr)); Free(zptr); Free(buffer.ptr); } }
//============================================================== static void PNGAPI writeFunc(png_structp png_ptr, png_bytep data, png_size_t length) //-------------------------------------------------------------- // //-------------------------------------------------------------- // in: //-------------------------------------------------------------- // out: //============================================================== { sFILE *fp; fp = (sFILE *)png_get_io_ptr(png_ptr); FsWrite(fp, data, length); }
int fflush(FILE *f) { char *base; int n, rn; if (f == NULL) { int e = errno; errno = 0; _fwalk((void (*)(FILE *))fflush); if (errno) return EOF; errno = e; return 0; } f->_flag &= ~_IOUNGETC; if ((f->_flag&(_IONBF|_IOWRT))==_IOWRT && (base = f->_base) != NULL && (rn = n = f->_ptr - base) > 0) { f->_ptr = base; f->_cnt = (f->_flag&(_IOLBF|_IONBF)) ? 0 : f->_bufsiz; do { if (!FsWrite(fileno(f), base, f->_offset, rn, &n) || n <= 0) { f->_flag |= _IOERR; return EOF; } rn -= n; base += n; f->_offset += n; } while (rn > 0); } if (f->_flag & _IORW) { f->_cnt = 0; f->_flag &= ~(_IOWRT|_IOREAD); f->_ptr = f->_base; } return 0; }
//============================================================== void MmFileWriteB(const char *file, void *ptr, size_t len) //-------------------------------------------------------------- // ファイルを書き出す // ※ブロックタイプ //-------------------------------------------------------------- // in: file = ファイル名 // ptr = データポインタ // len = データサイズ //-------------------------------------------------------------- // out: なし //============================================================== { sFILE *fp; fp = FsCreate(file); ASSERT(fp); FsWrite(fp, ptr, len); FsClose(fp); }
int DiskWriteBlock (uint32 blocknum, disk_block *b) { int fsfd = -1; uint32 intrvals = 0; char *filename = NULL; if (blocknum >= DISK_NUMBLOCKS) { printf("DiskWriteBlock: cannot write to block larger than filesystem size\n"); return DISK_FAIL; } // Check that you remembered to rename the filename for your group filename = DISK_FILENAME; if (filename[11] == 'X') { printf("DiskWriteBlock: you didn't change the filesystem filename in include/os/disk.h. Cowardly refusing to do anything.\n"); GracefulExit(); } intrvals = DisableIntrs(); // Open the hard disk file if ((fsfd = FsOpen(DISK_FILENAME, FS_MODE_RW)) < 0) { printf ("DiskWriteBlock: File system %s cannot be opened!\n", DISK_FILENAME); return DISK_FAIL; } /* printf("DiskWriteBlock: fsfd = %d\n", fsfd); */ // Write data to virtual disk FsSeek(fsfd, blocknum * DISK_BLOCKSIZE, FS_SEEK_SET); if (FsWrite(fsfd, b->data, DISK_BLOCKSIZE) != DISK_BLOCKSIZE) { printf ("DiskWriteBlock: Block %d could not be written!\n", blocknum); FsClose (fsfd); return DISK_FAIL; } // Close the hard disk file FsClose (fsfd); RestoreIntrs(intrvals); return DISK_BLOCKSIZE; }
//---------------------------------------------------------------------- // // main // // This routine is called when the OS starts up. It allocates a // PCB for the first process - the one corresponding to the initial // thread of execution. Note that the stack pointer is already // set correctly by _osinit (assembly language code) to point // to the stack for the 0th process. This stack isn't very big, // though, so it should be replaced by the system stack of the // currently running process. // //---------------------------------------------------------------------- void main (int argc, char *argv[]) { int i,j; int n; char buf[120]; char *userprog = (char *)0; int base=0; int numargs=0; char allargs[SIZE_ARG_BUFF]; int allargs_offset = 0; debugstr[0] = '\0'; printf ("Got %d arguments.\n", argc); printf ("Available memory: 0x%x -> 0x%x.\n", (int)lastosaddress, MemoryGetSize ()); printf ("Argument count is %d.\n", argc); for (i = 0; i < argc; i++) { printf ("Argument %d is %s.\n", i, argv[i]); } FsModuleInit (); for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D': dstrcpy (debugstr, argv[++i]); break; case 'i': n = dstrtol (argv[++i], (void *)0, 0); ditoa (n, buf); printf ("Converted %s to %d=%s\n", argv[i], n, buf); break; case 'f': { int start, codeS, codeL, dataS, dataL, fd, j; int addr = 0; static unsigned char buf[200]; fd = ProcessGetCodeInfo (argv[++i], &start, &codeS, &codeL, &dataS, &dataL); printf ("File %s -> start=0x%08x\n", argv[i], start); printf ("File %s -> code @ 0x%08x (size=0x%08x)\n", argv[i], codeS, codeL); printf ("File %s -> data @ 0x%08x (size=0x%08x)\n", argv[i], dataS, dataL); while ((n = ProcessGetFromFile (fd, buf, &addr, sizeof (buf))) > 0) { for (j = 0; j < n; j += 4) { printf ("%08x: %02x%02x%02x%02x\n", addr + j - n, buf[j], buf[j+1], buf[j+2], buf[j+3]); } } close (fd); break; } case 'u': userprog = argv[++i]; base = i; // Save the location of the user program's name break; default: printf ("Option %s not recognized.\n", argv[i]); break; } if(userprog) break; } } dbprintf ('i', "About to initialize queues.\n"); AQueueModuleInit (); dbprintf ('i', "After initializing queues.\n"); MemoryModuleInit (); dbprintf ('i', "After initializing memory.\n"); ProcessModuleInit (); dbprintf ('i', "After initializing processes.\n"); SynchModuleInit (); dbprintf ('i', "After initializing synchronization tools.\n"); KbdModuleInit (); dbprintf ('i', "After initializing keyboard.\n"); ClkModuleInit (); dbprintf ('i', "After initializing clock.\n"); for (i = 0; i < 100; i++) { buf[i] = 'a'; } i = FsOpen ("vm", FS_MODE_WRITE); dbprintf ('i', "VM Descriptor is %d\n", i); FsSeek (i, 0, FS_SEEK_SET); FsWrite (i, buf, 80); FsClose (i); // Setup command line arguments if (userprog != (char *)0) { numargs=0; allargs_offset = 0; // Move through each of the argv addresses for(i=0; i<argc-base; i++) { // At each argv address, copy the string into allargs, including the '\0' for(j=0; allargs_offset < SIZE_ARG_BUFF; j++) { allargs[allargs_offset++] = argv[i+base][j]; if (argv[i+base][j] == '\0') break; // end of this string } numargs++; } allargs[SIZE_ARG_BUFF-1] = '\0'; // set last char to NULL for safety ProcessFork(0, (uint32)allargs, userprog, 1); } else { dbprintf('i', "No user program passed!\n"); } ClkStart(); dbprintf ('i', "Set timer quantum to %d, about to run first process.\n", processQuantum); intrreturn (); // Should never be called because the scheduler exits when there // are no runnable processes left. exitsim(); // NEVER RETURNS! }
//---------------------------------------------------------------------- // // main // // This routine is called when the OS starts up. It allocates a // PCB for the first process - the one corresponding to the initial // thread of execution. Note that the stack pointer is already // set correctly by _osinit (assembly language code) to point // to the stack for the 0th process. This stack isn't very big, // though, so it should be replaced by the system stack of the // currently running process. // //---------------------------------------------------------------------- main (int argc, char *argv[]) { int i, j; int n; char buf[120]; char *userprog = (char *)0; static PCB temppcb; uint32 addr; extern void SysprocCreateProcesses (); char *param[12]={NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; int base; debugstr[0] = '\0'; MyFuncRetZero(); printf ("Got %d arguments.\n", argc); printf ("Available memory: 0x%x -> 0x%x.\n", lastosaddress, MemoryGetSize ()); printf ("Argument count is %d.\n", argc); for (i = 0; i < argc; i++) { printf ("Argument %d is %s.\n", i, argv[i]); } // *((int *)0xfff00100) = 't'; FsModuleInit (); for (i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'D': dstrcpy (debugstr, argv[++i]); break; case 'i': n = dstrtol (argv[++i], (void *)0, 0); ditoa (n, buf); printf ("Converted %s to %d=%s\n", argv[i], n, buf); break; case 'f': { int start, codeS, codeL, dataS, dataL, fd, j; int addr = 0; static unsigned char buf[200]; fd = ProcessGetCodeInfo (argv[++i], &start, &codeS, &codeL, &dataS, &dataL); printf ("File %s -> start=0x%08x\n", argv[i], start); printf ("File %s -> code @ 0x%08x (size=0x%08x)\n", argv[i], codeS, codeL); printf ("File %s -> data @ 0x%08x (size=0x%08x)\n", argv[i], dataS, dataL); while ((n = ProcessGetFromFile (fd, buf, &addr, sizeof (buf))) > 0) { for (j = 0; j < n; j += 4) { printf ("%08x: %02x%02x%02x%02x\n", addr + j - n, buf[j], buf[j+1], buf[j+2], buf[j+3]); } } close (fd); break; } case 'u': userprog = argv[++i]; base = i; break; default: printf ("Option %s not recognized.\n", argv[i]); break; } if(userprog) break; } } dbprintf ('i', "About to initialize queues.\n"); QueueModuleInit (); dbprintf ('i', "After initializing queues.\n"); MemoryModuleInit (); dbprintf ('i', "After initializing memory.\n"); ProcessModuleInit (); dbprintf ('i', "After initializing processes.\n"); ShareModuleInit (); dbprintf ('i', "After initializing shared memory.\n"); SynchModuleInit (); dbprintf ('i', "After initializing synchronization tools.\n"); KbdModuleInit (); dbprintf ('i', "After initializing keyboard.\n"); for (i = 0; i < 100; i++) { buf[i] = 'a'; } i = FsOpen ("vm", FS_MODE_WRITE); dbprintf ('i', "VM Descriptor is %d\n", i); FsSeek (i, 0, FS_SEEK_SET); FsWrite (i, buf, 80); FsClose (i); if (userprog != (char *)0) { for(i=base;i<argc&&i-base<11; i++) { param[i-base] = argv[i]; } process_create(0,0,param[0], param[1], param[2], param[3], param[4], param[5], param[6], param[7], param[8], param[9], param[10], param[11]); // ProcessFork (0, (uint32)"Help Me man!", userprog, 1); } SysprocCreateProcesses (); dbprintf ('i', "Created processes - about to set timer quantum.\n"); TimerSet (processQuantum); dbprintf ('i', "Set timer quantum to %d, about to run first process.\n", processQuantum); intrreturn (); // Should never be called because the scheduler exits when there // are no runnable processes left. exitsim(); // NEVER RETURNS! }
int ConKeyboardThread(void *param) { uint32_t ch, code; handle_t keyboard; //void *old_buffer; //unsigned i; keyboard = FsOpen(SYS_DEVICES L"/keyboard", FILE_READ); while (FsRead(keyboard, &ch, 0, sizeof(ch), NULL)) { code = ch & ~KBD_BUCKY_ANY; if ((ch & KBD_BUCKY_ALT) != 0 && code >= KEY_F1 && code <= KEY_F12) { /*LmuxAcquire(&lmux_consoles); LmuxAcquire(¤t->lmux); ConDrawCursor(current, false); old_buffer = current->cookie; current = consoles + code - KEY_F1; if (old_buffer != current->buffer) for (i = 0; i < num_consoles; i++) if (consoles[i].buffer == current->buffer) ConRedraw(consoles + i); ConDrawCursor(current, true); LmuxRelease(¤t->lmux); LmuxRelease(&lmux_consoles);*/ } else if (ch == (KBD_BUCKY_CTRL | KBD_BUCKY_ALT | KEY_DEL)) SysShutdown(SHUTDOWN_REBOOT); else if (ch == (KBD_BUCKY_ALT | '\t') || ch == (KBD_BUCKY_ALT | KBD_BUCKY_SHIFT | '\t')) { LmuxAcquire(&lmux_consoles); LmuxAcquire(¤t->lmux); ConDrawCursor(current, false); LmuxRelease(¤t->lmux); if (ch & KBD_BUCKY_SHIFT) { if (current - consoles - 1 < 0) current = consoles + num_consoles - 1; else current--; } else { if (current - consoles + 1 >= num_consoles) current = consoles; else current++; } LmuxAcquire(¤t->lmux); ConDrawCursor(current, true); LmuxRelease(¤t->lmux); LmuxRelease(&lmux_consoles); } else { LmuxAcquire(&lmux_consoles); if (current != NULL && current->client != NULL) FsWrite(current->client, &ch, 0, sizeof(ch), NULL); LmuxRelease(&lmux_consoles); } } _wdprintf(L"console(keyboard): FsRead failed, %s\n", _wcserror(errno)); return errno; }
void ConDoEscape(console_t *console, int code, unsigned num, const unsigned *esc) { unsigned i; char buf[20]; colour_t temp_colour; static const colour_t ansi_to_colour[] = { 0x000000, /* black */ 0x800000, /* red */ 0x008000, /* green */ 0x808000, /* yellow */ 0x000080, /* blue */ 0x800080, /* magenta */ 0x008080, /* cyan */ 0xC0C0C0, /* white */ }; switch (code) { case 'H': /* ESC[PL;PcH - cursor position */ case 'f': /* ESC[PL;Pcf - cursor position */ if (num > 0) console->y = min(esc[0], console->height - 1); if (num > 1) console->x = min(esc[1], console->width - 1); break; case 'A': /* ESC[PnA - cursor up */ if (num > 0 && console->y >= esc[0]) console->y -= esc[0]; break; case 'B': /* ESC[PnB - cursor down */ if (num > 0 && console->y < console->height + esc[0]) console->y += esc[0]; break; case 'C': /* ESC[PnC - cursor forward */ if (num > 0 && console->x < console->width + esc[0]) console->x += esc[0]; break; case 'D': /* ESC[PnD - cursor backward */ if (num > 0 && console->x >= esc[0]) console->x -= esc[0]; break; case 's': /* ESC[s - save cursor position */ console->save_x = console->x; console->save_y = console->y; break; case 'u': /* ESC[u - restore cursor position */ console->x = console->save_x; console->y = console->save_y; break; case 'J': /* ESC[2J - clear screen */ ConClear(console); break; case 'K': /* ESC[K - erase line */ ConClearEol(console); break; case 'm': /* ESC[Ps;...Psm - set graphics mode */ if (num == 0) { console->fg_colour = console->default_fg_colour; console->bg_colour = console->default_bg_colour; } else for (i = 0; i < num; i++) { if (esc[i] <= 8) { switch (esc[i]) { case 0: /* all attributes off */ console->fg_colour = console->default_fg_colour; console->bg_colour = console->default_bg_colour; break; case 1: /* bold (high-intensity) */ console->fg_colour = ConColourLighter(console->default_fg_colour); break; case 4: /* underscore */ break; case 5: /* blink */ console->bg_colour = ConColourLighter(console->default_bg_colour); break; case 7: /* reverse video */ temp_colour = console->bg_colour; console->bg_colour = console->fg_colour; console->fg_colour = temp_colour; break; case 8: /* concealed */ console->fg_colour = console->bg_colour; break; } } else if (esc[i] >= 30 && esc[i] <= 37) /* foreground */ console->default_fg_colour = console->fg_colour = ansi_to_colour[esc[i] - 30]; else if (esc[i] >= 40 && esc[i] <= 47) /* background */ console->default_bg_colour = console->bg_colour = ansi_to_colour[esc[i] - 40]; } break; case 'h': /* ESC[=psh - set mode */ case 'l': /* ESC[=Psl - reset mode */ case 'p': /* ESC[code;string;...p - set keyboard strings */ /* not supported */ break; case 'n': /* ESC[Pnn - DSR - DEVICE STATUS REPORT */ switch (esc[0]) { case 6: /* send CPR - ACTIVE POSITION REPORT */ FsWrite(console->client, buf, 0, sprintf(buf, "\x1b[%u;%uR", console->y, console->x), NULL); break; } break; } }