Esempio n. 1
0
void test1(){
	Acquire(0);
	MyWrite("Thread 1: Attempting to Wait on CV -1\n", sizeof("Thread 1: Attempting to Wait on CV -1\n"), 0,0);
	Wait(-1, 0);
	MyWrite("Thread 1: Attempting to Wait on CV 0\n", sizeof("Thread 1: Attempting to Wait on CV 0\n"), 0,0);
	Wait(0,0);
	Exit(0);
}
Esempio n. 2
0
File: inet.c Progetto: bbusse/netris
ExtFunc void SendPacket(NetPacketType type, int size, void *data)
{
	netint2 header[2];

	header[0] = hton2(type);
	header[1] = hton2(size + HEADER_SIZE);
	if (MyWrite(sock, header, HEADER_SIZE) != HEADER_SIZE)
		die("write");
	if (size > 0 && data && MyWrite(sock, data, size) != size)
		die("write");
}
Esempio n. 3
0
int     MyCopyAll(FILE *srcfh, PFILE dstfh)
{
    PCHAR  buffer = MyAlloc(BUFSIZE);

    UINT n;

    while ((n = fread(buffer, 1, BUFSIZE, srcfh)) != 0)
        MyWrite(dstfh, buffer, n);

    MyFree(buffer);

    return TRUE;
}
Esempio n. 4
0
UINT MyAlign(PFILE fh)
{
    DWORD   t0 = 0;
    DWORD   ib;

    /* align file to dword */
    ib = MySeek(fh, 0L, SEEK_CUR);
    if (ib % 4)
    {
        ib = 4 - ib % 4;
        MyWrite(fh, (PVOID)&t0, (UINT)ib);
        return(ib);
    }
    return(0);
}
Esempio n. 5
0
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);
}
Esempio n. 6
0
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;
}
Esempio n. 7
0
File: rc.c Progetto: mingpen/OpenNT
BOOL RC_PreProcess(PCHAR szname)
{
    PFILE fhout;        /* fhout: is temp file with rcincluded stuff */
    PFILE fhin;
    CHAR nm[_MAX_PATH*2];
    PCHAR pch;
    PWCHAR pwch;
    PWCHAR pfilename;
    WCHAR readszbuf[READ_MAX];
    WCHAR szT[ MAXSTR ];
    UINT iLine = 0;
    int fBlanks = TRUE;
    INT fFileType;

    /* Open the .RC source file. */
    MultiByteToWideChar(uiCodePage, MB_PRECOMPOSED, szname, -1, Filename, MED_BUFFER+1);
    fhin = fopen(szname, "rb");
    if (!fhin) {
        SET_MSG(Msg_Text, sizeof(Msg_Text), GET_MSG(1110), szname);
        return(FALSE);
    }

    /* Open the temporary output file. */
    fhout = fopen(szTempFileName, "w+b");
    if (!fhout) {
        strcpy(Msg_Text, GET_MSG(2180));
        return(FALSE);
    }

    /* output the current filename for RCPP messages */
    for (pch=nm ; *szname ; szname = CharNextA(szname)) {
        *pch++ = *szname;
        if (IsDBCSLeadByteEx(uiCodePage, *szname))
            *pch++ = *(szname + 1);
        /* Hack to fix bug #8786: makes '\' to "\\" */
        else if (*szname == '\\')
            *pch++ = '\\';
    }
    *pch++ = '\000';

    /* Output the current filename for RCPP messages */
    wcscpy(szT, L"#line 1\"");
    // hack - strlen("#line 1\"") is 8.
    MultiByteToWideChar(uiCodePage, MB_PRECOMPOSED, nm, -1, szT+8, MAXSTR+1-8);
    wcscat(szT, L"\"\r\n");
    MyWrite(fhout, szT, wcslen(szT) * sizeof(WCHAR));

    /* Determine if the input file is Unicode */
    fFileType = DetermineFileType (fhin);

    /* Process each line of the input file. */
    while (fgetl(readszbuf, READ_MAX, fFileType == DFT_FILE_IS_16_BIT, fhin)) {

        /* keep track of the number of lines read */
        Linenumber = iLine++;

        if ((iLine & RC_PREPROCESS_UPDATE) == 0)
            UpdateStatus(1, iLine);

        /* Skip the Byte Order Mark and the leading bytes. */
        pwch = readszbuf;
        while (*pwch && (iswspace(*pwch) || *pwch == 0xFEFF))
            pwch++;

        /* if the line is a rcinclude line... */
        if (strpre(L"rcinclude", pwch)) {
            /* Get the name of the rcincluded file. */
            pfilename = skipblanks(pwch + 9, TRUE);

            MyWrite(fhout, L"#include \"", 10 * sizeof(WCHAR));
            MyWrite(fhout, pfilename, wcslen(pfilename) * sizeof(WCHAR));
            MyWrite(fhout, L"\"\r\n", 3 * sizeof(WCHAR));

        }
        else if (strpre(L"#pragma", pwch)) {
            WCHAR cSave;

            pfilename = skipblanks(pwch + 7, FALSE);
            if (strpre(L"code_page", pfilename)) {
                pfilename = skipblanks(pfilename + 9, FALSE);
                if (*pfilename == L'(') {
                    ULONG cp = 0;

                    pfilename = skipblanks(pfilename + 1, FALSE);
                    // BUGBUG really should allow hex/octal, but ...
                    if (iswdigit(*pfilename)) {
                        while (iswdigit(*pfilename)) {
                            cp = cp * 10 + (*pfilename++ - L'0');
                        }
                        pfilename = skipblanks(pfilename, FALSE);
                    }
                    else if (strpre(L"default", pfilename)) {
                        cp = uiDefaultCodePage;
                        pfilename = skipblanks(pfilename + 7, FALSE);
                    }

                    if (cp == 0) {
                        wsprintfA(Msg_Text, "%s%ws", GET_MSG(4212), pfilename);
                        error(4212);
                    }
                    else if (*pfilename != L')') {
                        strcpy (Msg_Text, GET_MSG (4211));
                        error(4211);
                    }
                    else if (cp == CP_WINUNICODE) {
                        strcpy (Msg_Text, GET_MSG (4213));
                        warning(4213);
                    }
                    else if (!IsValidCodePage(cp)) {
                        strcpy (Msg_Text, GET_MSG (4214));
                        warning(4214);
                    }
                    else {
                        uiCodePage = cp;
                        /* Copy the #pragma line to the temp file. */
                        MyWrite(fhout, pwch, wcslen(pwch) * sizeof(WCHAR));
                        MyWrite(fhout, L"\r\n", 2 * sizeof(WCHAR));
                    }
                }
                else {
                    strcpy (Msg_Text, GET_MSG (4210));
                    error(4210);
                }
            }
        }
        else if (!*pwch)
            fBlanks = TRUE;
        else {
            if (fBlanks) {
                swprintf(szT, L"#line %d\r\n", iLine);
                MyWrite(fhout, szT, wcslen(szT) * sizeof(WCHAR));
                fBlanks = FALSE;
            }
            /* Copy the .RC line to the temp file. */
            MyWrite(fhout, pwch, wcslen(pwch) * sizeof(WCHAR));
            MyWrite(fhout, L"\r\n", 2 * sizeof(WCHAR));
        }
    }

    lCPPTotalLinenumber = iLine;
    Linenumber = 0;

    uiCodePage = uiDefaultCodePage;

    fclose(fhout);
    fclose(fhin);

    return(TRUE);
}
Esempio n. 8
0
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;
}
Esempio n. 9
0
void connection::handle_read(const boost::system::error_code& error, std::size_t bytes_transferred)
{
	status_ = READING;

	// data read, no need for timeouts (RK, note: race condition)
	cancel_read_timeout();

	if (!error && bytes_transferred > 0)
	{
		// ensure written bytes in the buffer
		_buf.commit(bytes_transferred);
		boost::tribool result;

		// http variables
		/// The incoming request.
		request request_;
		/// our response
		reply reply_;
		const char *begin;
		// websocket variables
		size_t bytes_consumed;

		switch (connection_type)
		{
		case connection_http:
			begin = boost::asio::buffer_cast<const char*>(_buf.data());
			try
			{
				request_parser_.reset();
				boost::tie(result, boost::tuples::ignore) = request_parser_.parse(
					request_, begin, begin + _buf.size());
			}
			catch (...)
			{
				_log.Log(LOG_ERROR, "Exception parsing http request.");
			}

			if (result) {
				size_t sizeread = begin - boost::asio::buffer_cast<const char*>(_buf.data());
				_buf.consume(sizeread);
				reply_.reset();
				const char *pConnection = request_.get_req_header(&request_, "Connection");
				keepalive_ = pConnection != NULL && boost::iequals(pConnection, "Keep-Alive");
				request_.keep_alive = keepalive_;
				request_.host_address = host_endpoint_address_;
				request_.host_port = host_endpoint_port_;
				if (request_.host_address.substr(0, 7) == "::ffff:") {
					request_.host_address = request_.host_address.substr(7);
				}
				request_handler_.handle_request(request_, reply_);

				if (reply_.status == reply::switching_protocols) {
					// this was an upgrade request
					connection_type = connection_websocket;
					// from now on we are a persistant connection
					keepalive_ = true;
					websocket_parser.Start();
					websocket_parser.GetHandler()->store_session_id(request_, reply_);
					// todo: check if multiple connection from the same client in CONNECTING state?
				}

				if (request_.keep_alive && ((reply_.status == reply::ok) || (reply_.status == reply::no_content) || (reply_.status == reply::not_modified))) {
					// Allows request handler to override the header (but it should not)
					reply::add_header_if_absent(&reply_, "Connection", "Keep-Alive");
					std::stringstream ss;
					ss << "max=" << default_max_requests_ << ", timeout=" << read_timeout_;
					reply::add_header_if_absent(&reply_, "Keep-Alive", ss.str());
				}

				MyWrite(reply_.to_string(request_.method));
				if (reply_.status == reply::switching_protocols) {
					// this was an upgrade request, set this value after MyWrite to allow the 101 response to go out
					connection_type = connection_websocket;
				}

				if (keepalive_) {
					read_more();
				}
				status_ = WAITING_WRITE;
			}
			else if (!result)
			{
				keepalive_ = false;
				reply_ = reply::stock_reply(reply::bad_request);
				MyWrite(reply_.to_string(request_.method));
				if (keepalive_) {
					read_more();
				}
			}
			else
			{
				read_more();
			}
			break;
		case connection_websocket:
		case connection_closing:
			begin = boost::asio::buffer_cast<const char*>(_buf.data());
			result = websocket_parser.parse((const unsigned char *)begin, _buf.size(), bytes_consumed, keepalive_);
			_buf.consume(bytes_consumed);
			if (result) {
				// we received a complete packet (that was handled already)
				if (keepalive_) {
					read_more();
				}
				else {
					// a connection close control packet was received
					// todo: wait for writeQ to flush?
					connection_type = connection_closing;
				}
			}
			else // if (!result)
			{
				read_more();
			}
			break;
		}
	}
	else if (error != boost::asio::error::operation_aborted)
	{
		connection_manager_.stop(shared_from_this());
	}
}
Esempio n. 10
0
void connection::WS_Write(const std::string &packet_data)
{
	MyWrite(CWebsocketFrame::Create(opcode_text, packet_data, false));
}
Esempio n. 11
0
WORD FAR SaveDIB(HDIB hDib, LPSTR lpFileName)
{
   BITMAPFILEHEADER bmfHdr; // Header for Bitmap file
   LPBITMAPINFOHEADER lpBI;   // Pointer to DIB info structure
   int fh;     // file handle for opened file
   OFSTRUCT of;     // OpenFile structure
   DWORD dwDIBSize;
   DWORD dwError;   // Error return from MyWrite

   if (!hDib)
      return ERR_INVALIDHANDLE;
   fh = OpenFile(lpFileName, &of, OF_CREATE | OF_READWRITE);
   if (fh == -1)
      return ERR_OPEN;

   /*
    * Get a pointer to the DIB memory, the first of which contains
    * a BITMAPINFO structure
    */
   lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDib);
   if (!lpBI)
      return ERR_LOCK;

   // Check to see if we're dealing with an OS/2 DIB.  If so, don't
   // save it because our functions aren't written to deal with these
   // DIBs.

   if (lpBI->biSize != sizeof(BITMAPINFOHEADER))
   {
     GlobalUnlock(hDib);
     return ERR_NOT_DIB;
   }

   /*
    * Fill in the fields of the file header
    */

   /* Fill in file type (first 2 bytes must be "BM" for a bitmap) */
   bmfHdr.bfType = DIB_HEADER_MARKER;  // "BM"

   // Calculating the size of the DIB is a bit tricky (if we want to
   // do it right).  The easiest way to do this is to call GlobalSize()
   // on our global handle, but since the size of our global memory may have
   // been padded a few bytes, we may end up writing out a few too
   // many bytes to the file (which may cause problems with some apps,
   // like HC 3.0).
   //
   // So, instead let's calculate the size manually.
   //
   // To do this, find size of header plus size of color table.  Since the
   // first DWORD in both BITMAPINFOHEADER and BITMAPCOREHEADER conains
   // the size of the structure, let's use this.

   dwDIBSize = *(LPDWORD)lpBI + PaletteSize((LPSTR)lpBI);  // Partial Calculation

   // Now calculate the size of the image

   if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression == BI_RLE4)) {

      // It's an RLE bitmap, we can't calculate size, so trust the
      // biSizeImage field

      dwDIBSize += lpBI->biSizeImage;
      }
   else {
      DWORD dwBmBitsSize;  // Size of Bitmap Bits only

      // It's not RLE, so size is Width (DWORD aligned) * Height

      dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) * lpBI->biHeight;

      dwDIBSize += dwBmBitsSize;
      
      // Now, since we have calculated the correct size, why don't we
      // fill in the biSizeImage field (this will fix any .BMP files which 
      // have this field incorrect).

      lpBI->biSizeImage = dwBmBitsSize;
      }


   // Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
                   
   bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
   bmfHdr.bfReserved1 = 0;
   bmfHdr.bfReserved2 = 0;

   /*
    * Now, calculate the offset the actual bitmap bits will be in
    * the file -- It's the Bitmap file header plus the DIB header,
    * plus the size of the color table.
    */
   bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize +
                      PaletteSize((LPSTR)lpBI);

   /* Write the file header */
   _lwrite(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));

   /*
    * Write the DIB header and the bits -- use local version of
    * MyWrite, so we can write more than 32767 bytes of data
    */
   dwError = MyWrite(fh, (LPSTR)lpBI, dwDIBSize);
   GlobalUnlock(hDib);
   _lclose(fh);

   if (dwError == 0)
     return ERR_OPEN; // oops, something happened in the write
   else
     return 0; // Success code
}