static int read_response( void ) { char buf[10000]; char* cp; int status; for (;;) { (void) alarm( timeout ); if ( fgets( buf, sizeof(buf), sockrfp ) == (char*) 0 ) { (void) fprintf( stderr, "%s: unexpected EOF\n", argv0 ); myError( " unexpected EOF\n", argv0 ); exit( 1 ); } if ( verbose ) (void) fprintf( stderr, "<<<< %s", buf ); for ( status = 0, cp = buf; *cp >= '0' && *cp <= '9'; ++cp ) status = 10 * status + ( *cp - '0' ); if ( *cp == ' ' ) break; if ( *cp != '-' ) { (void) fprintf(stderr, "%s: bogus reply syntax - '%s'\n", argv0, buf ); myError( "ERROR", "reply syntax -" ); exit( 1 ); } } return status; }
HANDLE commOpen() { HANDLE hSerial = CreateFile(pComPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (hSerial == INVALID_HANDLE_VALUE) { myError("Error opening serial port."); return NULL; } DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength = sizeof(dcbSerialParams); if (!GetCommState(hSerial, &dcbSerialParams)) { myError("Error getting serial port parameters."); return NULL; } dcbSerialParams.BaudRate = 9600; dcbSerialParams.ByteSize = 8; dcbSerialParams.StopBits = ONESTOPBIT; dcbSerialParams.Parity = NOPARITY; if(!SetCommState(hSerial, &dcbSerialParams)) { myError("Error setting serial port parameters."); return NULL; } COMMTIMEOUTS timeouts = {0}; timeouts.ReadIntervalTimeout = 100; timeouts.ReadTotalTimeoutMultiplier = 2; timeouts.ReadTotalTimeoutConstant = 100; timeouts.WriteTotalTimeoutMultiplier = 2; timeouts.WriteTotalTimeoutConstant = 100; if(!SetCommTimeouts(hSerial, &timeouts)) { myError("Error setting serial port timeouts."); return NULL; } return hSerial; }
void ResponseState::preRespond(){ if(stat(const_cast<char*>(fileName.c_str()),&sbuf)<0){ throw myError("404","not found","Couldn't find this file"); } if(!(S_ISREG(sbuf.st_mode))||!(S_IRUSR&sbuf.st_mode)){ throw myError("403","Forbidden",buildForbiddenMsg()); } }
// Application Entry Point // int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG Msg; // Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wc)) { myError("Window Registration Failed!"); return 0; } // MessageBox(NULL, lpCmdLine, "Command Line Arguments", MB_ICONINFORMATION | MB_OK); // testing // Creating the Window hWndMain = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "IPPF Fiscal Printer Utility", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 480, 320, NULL, NULL, hInstance, NULL); if (hWndMain == NULL) { myError("Window Creation Failed!"); return 0; } ShowWindow(hWndMain, nCmdShow); UpdateWindow(hWndMain); //////////////////////////// Testing //////////////////////////////// char inbytes[] = "0x2a|N"; makePrintString(inbytes); FILE * hFile = fopen("e:\\fptest.bin", "w"); fprintf(hFile, ">%s", pPrintBuf); HANDLE hSerial = commOpen(); if (hSerial && commRW(hSerial)) fprintf(hFile, "<%s", pComBuf); CloseHandle(hSerial); fclose(hFile); ///////////////////////////////////////////////////////////////////// // The Message Loop while (GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
workingMemory_t * allocWorkingMemory(){ workingMemory_t * workingMemory = (workingMemory_t *)malloc(sizeof(workingMemory_t)); if (workingMemory == NULL) myError("malloc VM"); workingMemory->line = (data_t *)malloc(sizeof(data_t) * N); if( workingMemory->line == NULL) myError("malloc line"); workingMemory->column = (data_t *)malloc(sizeof(data_t) * N); if( workingMemory->column == NULL) myError("malloc column"); return workingMemory; }
bool Socket::initSock(const std::string &add) { struct protoent *pe; if ((pe = getprotobyname("TCP")) == NULL) return (myError("getprotobyname")); if ((this->_sockFd = socket(AF_INET, SOCK_STREAM, pe->p_proto)) < 0) return (myError("init socket")); this->_sock.sin_family = AF_INET; this->_sock.sin_port = htons(this->_port); this->_sock.sin_addr.s_addr = inet_addr(add.c_str()); return (true); }
chessBoard_t * allocChessBoard(){ chessBoard_t * chessBoard = (chessBoard_t*)malloc(sizeof(chessBoard_t)); if(chessBoard == NULL) myError("malloc chessBoard"); chessBoard->state = (char **)malloc(sizeof(char *) * N); if(chessBoard->state == NULL) myError("malloc chessBoard"); int i; for(i = 0; i < N; i++){ chessBoard->state[i] = (char *)malloc(sizeof(char) * N); if(chessBoard->state[i] == NULL) myError("malloc chessBoard"); } return chessBoard; }
void* myRealloc(void* ptr, int size) { register void *value = (void *)realloc (ptr, size); if (value == 0) myError("Virtual memory exhausted!"); return value; }
static char* make_received( char* from, char* username, char* hostname ) { int received_size; char* received; time_t t; struct tm* tmP; char timestamp[100]; t = time( (time_t*) 0 ); tmP = localtime( &t ); (void) strftime( timestamp, sizeof(timestamp), "%a, %d %b %Y %T %Z", tmP ); received_size = 500 + strlen( from ) + strlen( hostname ) * 2 + strlen( VERSION ) + strlen( timestamp ) + strlen( username ); received = (char*) malloc( received_size ); if ( received == (char*) 0 ) { (void) fprintf( stderr, "%s: out of memory\n", argv0 ); myError( "ERROR", "out of memory" ); exit( 1 ); } (void) snprintf( received, received_size, "Received: (from %s)\n\tby %s (%s);\n\t%s\n\t(sender %s@%s)\n", from, hostname, VERSION, timestamp, username, hostname ); return received; }
bool Select::setSelect() { if (select(this->_sockFd + 1, &this->_fdRead, &this->_fdWrite, NULL, &this->_tv) == -1) return (myError("Can't select")); return (true); }
static void add_recipient( char* recipient, int len ) { char buf[1000]; int status; /* Skip leading whitespace. */ while ( len > 0 && ( *recipient == ' ' || *recipient == '\t' ) ) { ++recipient; --len; } /* Strip off any angle brackets. */ while ( len > 0 && *recipient == '<' ) { ++recipient; --len; } while ( len > 0 && recipient[len-1] == '>' ) --len; (void) snprintf( buf, sizeof(buf), "RCPT TO:<%.*s>", len, recipient ); send_command( buf ); status = read_response(); if ( status != 250 && status != 251 ) { (void) fprintf( stderr, "%s: unexpected response %d to RCPT TO command\n",argv0, status ); myError( "ERROR", "unexpected response %d to RCPT TO command" ); exit( 1 ); } got_a_recipient = 1; }
char * initCharArray(char * array){ if(array == NULL) myError("Bad char array access"); int i; for(i = 0; i < N; i++) array[i] = 0; return array; }
static void sigcatch( int sig ) { (void) fprintf( stderr, "%s: timed out\n", argv0 ); myError( "ERROR","Timed out"); exit( 1 ); }
void* myMalloc(int size) { register void *value = (void *)malloc (size); if (value == 0) myError("Virtual memory exhausted!"); return value; }
TemporaryDirectory::TemporaryDirectory(const std::string& aDirPath) { theDirPath = aDirPath; boost::filesystem::path myDirectory(theDirPath); if(!boost::filesystem::exists(myDirectory)) { std::string myError("Directory "); myError += theDirPath; myError += " does not exist"; throw std::runtime_error(myError); } if ( ! boost::filesystem::is_directory(myDirectory) ) { std::string myError = theDirPath; myError += " is not a directory"; throw std::runtime_error(myError); } boost::filesystem::directory_iterator myEndIter; boost::filesystem::directory_iterator myDirIter(myDirectory); for(/*void*/; myDirIter != myEndIter; ++myDirIter) { TemporaryFile * myFile = new TemporaryFile(theDirPath + "/" + myDirIter->leaf() ); theFiles.insert(std::make_pair(myFile->getFileName(), myFile)); } }
int readDisk (u64 new_block) { off_t offset; ssize_t bytesRead; offset = new_block * BlockSize; if (lseek(Disk, offset, 0) == -1) { sysError("lseek"); return -1; } bytesRead = read(Disk, Block, BlockSize); if (bytesRead == 0) { myError("At EOF"); return 0; } if (bytesRead == -1) { sysError("read"); return -1; } if (bytesRead < BlockSize) { bzero( &Block[bytesRead], BlockSize - bytesRead); } BlockNum = new_block; return 0; }
bool Server::onBackendRequest(const BackendContext & ctx, Connection & conn) const { DefaultResponseWriter writer; try { // Setup request object Request request; // Read request. ctx.getRequestHeaderReader().readRequestHeader(conn, request); ctx.getRequestBodyReader().readRequestBody(conn, request); // Route request Response response; if (!_data->router.route(request, response)) { std::ostringstream oss; oss << "Route not found " << request.getPath(); throw Error(StatusCode::NotFound, oss.str().c_str()); } // Enable cors for now. response.setHeader("Access-Control-Allow-Origin", "*"); writer.writeResponse(conn, response); return true; } catch (const Error &error) { Response rep(error.toJson()); writer.writeResponse(conn, rep); return true; } catch (const std::exception &error) { Error myError(StatusCode::InternalServerError, error.what()); Response rep(myError.toJson()); writer.writeResponse(conn, rep); return true; } catch (...) { Error myError(StatusCode::InternalServerError, "Unknown error occurred. That's all we know."); Response rep(myError.toJson()); writer.writeResponse(conn, rep); return true; } return false; }
runState_t Hturn(state_t st, pKind_t player) { //人のターン if((player==NONE)||(player > BK)) myError("undefined player\n"); st->player = player; checkBoard(st); if(st->activePiece == 0) return finish; point_t p = allocPoint(); runState_t rs = cont; movePieace(st, p); return rs; }
static void show_error( char* cause ) { char buf[5000]; (void) snprintf( buf, sizeof(buf), "%s: %s", argv0, cause ); myError( "ERROR", cause ); perror( buf ); exit( 1 ); }
bool Socket::clientConnect() const { if (connect(this->_sockFd, (const struct sockaddr *)&this->_sock, (socklen_t)sizeof(this->_sock)) == -1) { close(this->_sockFd); return (myError("connect")); } return (true); }
bool Select::readIn() { if (FD_ISSET(this->_sockFd, &this->_fdRead)) { if (!readInFd()) return (myError("Server closed")); } if (FD_ISSET(this->_sockFd, &this->_fdWrite)) return (writeInFd()); return (true); }
bool Select::writeInFd() { while (!this->_buff_w.empty()) { if (dprintf(this->_sockFd, "%s\n", this->_buff_w.front().second.c_str()) <= 0) return (myError("Server closed")); this->_buff_w.pop(); } return (true); }
chessBoard_t* initChessBoard(chessBoard_t * chessBoard){ if(chessBoard == NULL) myError("chessBoard == NULL"); int i, j; for(i = 0; i < N; i++){ for(j = 0; j < N; j++){ chessBoard->state[i][j] = 0; } } return chessBoard; }
static char* slurp_message( void ) { char* message; int message_size, message_len; int c; message_size = 5000; message = (char*) malloc( message_size ); if ( message == (char*) 0 ) { (void) fprintf( stderr, "%s: out of memory\n", argv0 ); myError( "ERROR", "out of memory" ); exit( 1 ); } message_len = 0; for (;;) { c = getchar(); if ( c == EOF ) break; if ( message_len + 1 >= message_size ) { message_size *= 2; message = (char*) realloc( (void*) message, message_size ); if ( message == (char*) 0 ) { (void) fprintf( stderr, "%s: out of memory\n", argv0 ); myError( "ERROR", "out of memory" ); exit( 1 ); } } message[message_len++] = c; } message[message_len] = '\0'; return message; }
void separate(char* toparse, char* delim, void f(char*)) { if(strlen(toparse)>512){ myError(); return; } char *rc, *saveptr; rc = strtok_r(toparse,delim, &saveptr); while(rc != NULL){ f(removeleadingspace(rc)); rc = strtok_r(NULL,delim, &saveptr); } }
// Increment line sequence number which is maintained in the registry, and return its value. // DWORD getNextSeq() { LONG lResult; HKEY hKey; DWORD dwValue; DWORD dwTmp; // Open our registry key, creating it if necessary. lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\IPPF"), 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &hKey, NULL); if (lResult != ERROR_SUCCESS) { return myError("Could not open registry key."); } // Get the last sequence number, or assign it as 0 if not there yet. dwTmp = sizeof(DWORD); lResult = RegQueryValueEx(hKey, TEXT("TicketPrinterSeq"), NULL, &dwTmp, (BYTE *) &dwValue, &dwTmp); if (lResult == ERROR_FILE_NOT_FOUND) { dwValue = 0; } else if (lResult != ERROR_SUCCESS) { return myError("Error reading TicketPrinterSeq from registry."); } // Increment and normalize the value to what the printer will accept. ++dwValue; if (dwValue < 0x20 || dwValue > 0x7f) dwValue = 0x20; // Save the updated value to the registry. lResult = RegSetValueEx(hKey, TEXT("TicketPrinterSeq"), 0, REG_DWORD, (BYTE *) &dwValue, sizeof(DWORD)); if (lResult != ERROR_SUCCESS) { return myError("Error writing TicketPrinterSeq to registry."); } RegCloseKey(hKey); return dwValue; }
// The Window Procedure // LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { HFONT hfDefault; HWND hEdit; hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL); if (hEdit == NULL) { myError("Could not create edit box."); } hfDefault = (HFONT) GetStockObject(DEFAULT_GUI_FONT); SendMessage(hEdit, WM_SETFONT, (WPARAM) hfDefault, MAKELPARAM(FALSE, 0)); } break; case WM_SIZE: { HWND hEdit; RECT rcClient; GetClientRect(hwnd, &rcClient); hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER); } break; case WM_CLOSE: { DestroyWindow(hwnd); } break; case WM_DESTROY: { PostQuitMessage(0); } break; default: { return DefWindowProc(hwnd, msg, wParam, lParam); } } return 0; }
void batch(char* fname) { FILE* fp = fopen(fname, "r"); if(fp==NULL) { myError(); exit(0); } char* line = NULL; size_t len = 0; ssize_t read; while((read = getline(&line, &len, fp))!=-1) { if(strcmp(removeleadingspace(line),"\n")){ myPrint(line); separate(line, ";", execute); } } exit(0); }
workingMemory_t* initWorkingMemory(workingMemory_t* workingMemory){ if(workingMemory == NULL){ myError("workingMemory == NULL"); } int i; for(i = 0; i < N; i++){ workingMemory->line[i].state = OUT; workingMemory->line[i].supporter = NULL; workingMemory->column[i].state = OUT; workingMemory->column[i].supporter = NULL; } workingMemory->goal.state = OUT; workingMemory->goal.supporter = NULL; workingMemory->nogood.state = OUT; workingMemory->nogood.supporter = NULL; workingMemory->buffer = NULL; return workingMemory; }
int init (int argc, char *argv[]) { if (BlockSize > MAX_BLOCK_SIZE) { return myError("Block size too big"); } NumRows = BlockSize / u8S_PER_ROW; setSignals(); initscr(); cbreak(); noecho(); nonl(); intrflush(stdscr, FALSE); //idlok(stdscr, TRUE); keypad(stdscr, TRUE); initStack(); initMemory(); Initialized = TRUE; return 0; }