int MIONet_Getc (void *pmFilePtr) { NetRecord *myNetRecord = (NetRecord *) pmFilePtr; unsigned char myChar; // It is possible that MIONet_Getc gets called without the appropriate // HasEvent having been called and passed because 'eof' doesn't call // HasEvent first. So, we risk a busy wait... if (MyBytesAvailable (myNetRecord) == 0) { // Do Nothing - the MyBytesAvailable reads the buffer ABORT_WITH_ERRNO (E_NET_DONT_USE_EOF); } if (MyRead (&myChar, 1, myNetRecord) != 1) { return EOF; } if (myNetRecord -> binary) { return myChar; } // If the last character we read was a CR and this is a LF, skip over // it. This should be alright, as the HasEvent also takes the // newline conversion into account if (myNetRecord -> lastCharWasCR && (myChar == '\n')) { if (MyRead (&myChar, 1, myNetRecord) != 1) { return EOF; } } // If the char just read is not a CR, return the char if (myChar != '\r') { myNetRecord -> lastCharWasCR = FALSE; return myChar; } myNetRecord -> lastCharWasCR = TRUE; // We've read a CR. See if the next character is a NL and // clear it off the buffer if it is. if ((!myNetRecord -> seenEOF) && (myNetRecord -> tail != myNetRecord -> head) && (* (char *) (myNetRecord -> buf + myNetRecord -> tail) == '\n')) { // The next char *is* a LF. So read the LF. MyRead (&myChar, 1, myNetRecord); } return '\n'; } // MIONet_Getc
/************************************************************************* Function: ReadDIBFile (int) Purpose: Reads in the specified DIB file into a global chunk of memory. Returns: A handle to a dib (hDIB) if successful. NULL if an error occurs. Comments: BITMAPFILEHEADER is stripped off of the DIB. Everything from the end of the BITMAPFILEHEADER structure on is returned in the global memory handle. History: Date Author Reason 6/1/91 Created 6/27/91 Removed PM bitmap conversion routines. 6/31/91 Removed logic which overallocated memory (to account for bad display drivers). 11/08/91 Again removed logic which overallocated memory (it had creeped back in!) *************************************************************************/ static HANDLE ReadDIBFile (int hFile,int dwBitsSize) { BITMAPFILEHEADER bmfHeader; HANDLE hDIB; LPSTR pDIB; // Go read the DIB file header and check if it's valid. if ((_lread (hFile, (LPSTR) &bmfHeader, sizeof (bmfHeader)) != sizeof (bmfHeader)) || (bmfHeader.bfType != DIB_HEADER_MARKER)) { // ShowDbgMsg("Not a DIB file!"); return NULL; } // Allocate memory for DIB hDIB = GlobalAlloc (GMEM_SHARE|GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize - sizeof(BITMAPFILEHEADER)); if (hDIB == 0) { // ShowDbgMsg("Couldn't allocate memory!"); return NULL; } pDIB = GlobalLock (hDIB); // Go read the bits. if (!MyRead (hFile, pDIB, dwBitsSize - sizeof(BITMAPFILEHEADER))) { GlobalUnlock (hDIB); GlobalFree (hDIB); // ShowDbgMsg("Error reading file!"); return NULL; } GlobalUnlock (hDIB); return hDIB; }
int MyCopy(FILE *srcfh, PFILE dstfh, ULONG nbytes) { PCHAR buffer = MyAlloc(BUFSIZE); UINT n; while (nbytes) { if (nbytes <= BUFSIZE) n = (UINT)nbytes; else n = BUFSIZE; nbytes -= n; MyRead(srcfh, buffer, n); MyWrite( dstfh, buffer, n); } MyFree(buffer); return(n); }
int FileChar() { static WCHAR rgchLine[MAXSTR]; static int ibNext = MAXSTR; int cch, ch; if (ibNext >= MAXSTR) { ibNext = 0; cch = MyRead (inpfh, rgchLine, MAXSTR * sizeof(WCHAR)); if (cch < (MAXSTR * sizeof(WCHAR))) { fclose(inpfh); // NULL terminate the input buffer *(rgchLine + (cch / sizeof(WCHAR))) = L'\0'; } } if ((ch = rgchLine[ibNext]) != 0) ibNext++; return(ch); }
static int read_block ( BPTR file, ULONG offset, APTR buffer, ULONG size, SIPTR *funcarray, struct DosLibrary *DOSBase ) { UBYTE *buf = (UBYTE *)buffer; LONG subsize; if (Seek(file, offset, OFFSET_BEGINNING) < 0) return 0; while (size) { subsize = MyRead(file, buf, size); if (subsize <= 0) { if (subsize == 0) { D(bug("[ELF Loader] Error while reading from file.\n")); D(bug("[ELF Loader] Offset = %ld - Size = %ld\n", offset, size)); SetIoErr(ERROR_BAD_HUNK); } return 0; } buf += subsize; size -= subsize; } return 1; }
static MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event) { int result; short type, size; netint2 data[2]; result = MyRead(sock, netBuf + netBufSize, netBufGoal - netBufSize); if (result < 0) { lostConn = 1; return E_lostConn; } netBufSize += result; if (netBufSize < netBufGoal) return E_none; memcpy(data, netBuf, sizeof(data)); type = ntoh2(data[0]); size = ntoh2(data[1]); if (size >= sizeof(netBuf)) fatal("Received an invalid packet (too large), possibly an attempt\n" " to exploit a vulnerability in versions before 0.52 !"); netBufGoal = size; if (netBufSize < netBufGoal) return E_none; netBufSize = 0; netBufGoal = HEADER_SIZE; event->u.net.type = type; event->u.net.size = size - HEADER_SIZE; event->u.net.data = netBuf + HEADER_SIZE; if (type == NP_endConn) { gotEndConn = 1; return E_lostConn; } else if (type == NP_byeBye) { lostConn = 1; return E_lostConn; } return E_net; }
int main() { int iCount = 1,i=0,bufsize=0,j=0; char iInput[10],iData[20]; while (1) { printf("Enter your choice:\n"); printf("1.create disk\n"); printf("2.Create a file\n"); printf("3.Write data into file\n"); printf("4.Read the written file\n"); printf("5.delete a file\n"); printf("6.Print list of files available\n"); scanf("%d", &i); MyFile *fp; fp = fopen("divFile", "rb+"); switch (i) { case 1: fp = MyCreateFile("divFile"); rewind(fp); for (i = 1; i < 2049; i++) fputc(48, fp); rewind(fp); break; case 2: printf("Enter the file name\n"); scanf("%s",iInput); for (i = 0; iInput[i] != '\0'; i++) bufsize++; fseek(fp,1,SEEK_SET); for (i = 1; i < 2049; i++) { if (fgetc(fp) == 48) break; } rewind(fp); MyWrite(fp, iInput ,"ghj",9,i); fseek(fp, i, SEEK_SET); fputc(49,fp ); rewind(fp); break; case 3: printf("Enter data to be written\n"); scanf("%s",iData); insert_data(fp, iInput, iData, strlen(iData), iCount); break; case 4: printf("Enter filename\n"); scanf("%s",iData); MyRead(fp, iData, strlen(iData),iCount); break; case 5: printf("Enter filename to be deleted\n"); gets(iInput); delete_file(fp, iInput,iCount); iCount--; break; case 6: MyList(fp, iCount); break; } iCount++; bufsize = 0; } scanf("%d", &iCount); return 0; }
int MIONet_Read (void *pmBuffer, int pmSize, void *pmFilePtr) { return MyRead (pmBuffer, pmSize, (NetRecord *) pmFilePtr); } // MIONet_Read
int main(int argc, char *argv[]){ //Variables int sd, fd, rc, returnCode, i, size; struct sockaddr_in s; int problem = sizeof(MFS_Dir_t) + sizeof(MFS_Inode_t) + sizeof(MFS_InodeMap_t) + (2 * sizeof(MFS_DirEnt_t)); //Initialize the client and server messages client = (ClientMessage_t *)malloc(sizeof(ClientMessage_t)); server = (ServerMessage_t *)malloc(sizeof(ServerMessage_t)); //Check for file image /* if(argc == 3){ sd = UDP_Open(atoi(argv[1])); fd = open(argv[2], O_RDWR); assert(sd > -1); assert(fd > -1); } */ if(argc == 3){ printf("Check: %d\n", atoi(argv[1])); //Open the socket and the image sd = UDP_Open(atoi(argv[1])); //sd = UDP_Open(29344); fd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); assert(sd > -1); assert(fd > -1); //Create image imageSize = sizeof(MFS_Checkpoint_t) + (256 * sizeof(MFS_InodeMap_t)) + (4096 * sizeof(MFS_Inode_t)) + (14 * 4096 * 4096); image = malloc(imageSize); if(image == NULL) printf("MALLOC PROBLEM\n"); size = 0; //Create and initialize the checkpoint temp = image; position = image; end = image; checkpoint = (MFS_Checkpoint_t *)temp; position += sizeof(MFS_Checkpoint_t); //Initialize the arrays for(i = 0; i < 256; i++) inodeMap[i] = NULL; for(i = 0; i < 4096; i++) inodes[i] = NULL; //Create the root directory rc = addDirectory(0, "/"); } else{ printf("Usage: server portnum [file-system-image]\n"); exit(-1); } //Start server loop while(1){ //Check to make sure size is okay //Get the message from the Client rc = UDP_Read(sd, &s, (char *)client, sizeof(ClientMessage_t)); //Perform the correct operation if(rc > 0){ if(client->syscallNumber == LOOKUP){ //Lookup the file returnCode = MyLookup(client->inum, client->buffer); printf("RETURNCODE: %d\n", returnCode); //Setup the return message server->returnCode = returnCode; printf("server server return: %d\n", server->returnCode); rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); } else if(client->syscallNumber == STAT){ //Get the Stat info about the specific inum returnCode = MyStat(client->inum); //Setup the return message server->returnCode = returnCode; rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); } else if(client->syscallNumber == WRITE){ printf("ENTERED WRITE 1\n"); printf("here here here: %d\n", client->inum); //Update image returnCode = MyWrite(client->inum, client->buffer, client->block); //Update checkpoint checkpoint->end = end - image; //Write to disk rc = write(fd, image, imageSize); if(rc <= 0) exit(-1); fsync(fd); //Setup the return message server->returnCode = returnCode; rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); } else if(client->syscallNumber == READ){ //Read the specified block of data returnCode = MyRead(client->inum, client->block); printf("server server server buffer: %s\n", server->buffer); //Setup the return message server->returnCode = returnCode; rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); } else if(client->syscallNumber == CREAT){ //Update image returnCode = MyCreat(client->inum, client->type, client->buffer); //Update checkpoint checkpoint->end = end - image; //Write to disk rc = write(fd, image, imageSize); if(rc <= 0) exit(-1); fsync(fd); //Setup the return message server->returnCode = returnCode; rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); } else if(client->syscallNumber == UNLINK){ //Unlink and update message returnCode = MyUnlink(client->inum, client->buffer); //Update checkpoint checkpoint->end = end - image; //Write to disk rc = write(fd, image, imageSize); if(rc <= 0) exit(-1); fsync(fd); //Setup the return message server->returnCode = returnCode; rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); } else{ //Update checkpoint checkpoint->end = end - image; //This is the case for shutdown rc = write(fd, image, imageSize); if(rc <= 0) exit(-1); fsync(fd); //Set up the return message server->returnCode = 0; rc = UDP_Write(sd, &s, (char *)server, sizeof(ServerMessage_t)); exit(0); } } } return 0; }