Sponsored Content
Full Discussion: Some questions for mini game
Top Forums Programming Some questions for mini game Post 302475095 by Corona688 on Friday 26th of November 2010 11:17:19 AM
Old 11-26-2010
Threading is a whole new kettle of fish and wouldn't completely fix the problem anyway -- you'd still need to mess with the terminal settings. You can do reads without blocking anyway, though it means changing your terminal settings and using the unix read() or select() call instead of stdio calls or iostream objects. Letting you do what you want in a loop.

Code:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    int fd=STDIN_FILENO;	/* STDIN=0, STDOUT=1, STDERR=2 */
    int running=1;

    system("stty -icanon min 0 -echo"); // So we can read one char at a time.
    // -echo means it doesn't autorepeat what you type, either.

    while(running)
    {
        char buf[512];
        ssize_t bytes;

        bytes=read(fd, &buf, 511);
        if(bytes > 0)
        {
          running=0;
          buf[bytes]='\0'; // NULL-terminate it
          fprintf(stderr, "Read %d bytes: %s\n", (int)bytes, buf);
          running=0;
        }
        else
        {
          fprintf(stderr, "not ready\n");
          usleep(100000L);
        }
    }

    system("stty sane"); // Return terminal to normal

    return(0);
}

Using system("stty") is a bit of a hack but works fine. It alters terminal settings for stdin, which are global to a terminal, i.e. if you don't reset your terminal after your program quits you'll still have weird settings! If you want to take a look at how stty actually works, you can see my examples tcgets.c and tcgets.h. That's code for using a serial port but as far as UNIX is concerned, all terminals are serial ports...

Last edited by Corona688; 11-26-2010 at 12:38 PM..
 

8 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

mini digi-cam

hey guys! I am on a fedora core2 i686 with gui and I have a miniture cool-cam which is digital.. it connects through regular usb.. the system recognizes it and the when I go into the desktop peripheral and go to camera it gives name and tells test was successful.. it also says that the camera has... (0 Replies)
Discussion started by: moxxx68
0 Replies

2. Programming

Mini Shell in C

Hi Everyone, I am a student learning C and Unix. I want to create a shell in C which accepts command line arguments and executes them. I am not sure how to do this. Any help would be greatly appreciated. Thanks (5 Replies)
Discussion started by: passat
5 Replies

3. UNIX for Dummies Questions & Answers

mini distribution

Maybe someone knows floppy distribution with which I could connect to the internet and browse sites. THanks for answers (1 Reply)
Discussion started by: Vilmis
1 Replies

4. UNIX for Dummies Questions & Answers

mini shell programming (help)

Hi All, Well i m a taking an operating system course (newbie to unix) we have studied till now: the fork () execv() the teacher asked us to create a mini shell that execute a user command: cmd1 he said everything in is optional we can use any combination Well dudes , i m really... (2 Replies)
Discussion started by: ELECTRO
2 Replies

5. OS X (Apple)

Mac mini to Sony TV

Hi All, I have Mac mini, I bought DVI to HDMI cable and connected this to TV and sounds didn't come. On the Sony TV, right below VGA, I see mini-port plug in. I then connected VGA cable from Mac to Sony TV, I can see every thing. but for the sound, should I buy mini-port to mini-port cable.... (1 Reply)
Discussion started by: samnyc
1 Replies

6. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

7. Shell Programming and Scripting

Mini Project

Hi Experts, I'm a newbie.....just now i started to write some simple scripts on my own. Can anyone suggest me any simple project kind of stuff to hone my SHELL SCRIPTING skills....which involves database connection and more than that.....bcoz i already tried to write a script which connects to... (0 Replies)
Discussion started by: kritibalu
0 Replies

8. Shell Programming and Scripting

Need help in a mini project

Hi All, I want to make something like described below - "Double click on an executable file that will check the health status and other things of various linux servers and send an email to a list of people." I can make shell scripts for individual servers but how to make a script that will check... (1 Reply)
Discussion started by: csrohit
1 Replies
CREATELANG(1)						  PostgreSQL Client Applications					     CREATELANG(1)

NAME
createlang - define a new PostgreSQL procedural language SYNOPSIS
createlang [ connection-options... ] langname [ dbname ] createlang [ connection-options... ] --list | -l dbname DESCRIPTION
createlang is a utility for adding a new programming language to a PostgreSQL database. createlang can handle all the languages supplied in the default PostgreSQL distribution, but not languages provided by other parties. Although backend programming languages can be added directly using several SQL commands, it is recommended to use createlang because it performs a number of checks and is much easier to use. See CREATE LANGUAGE [create_language(7)] for additional information. OPTIONS
createlang accepts the following command-line arguments: langname Specifies the name of the procedural programming language to be defined. [-d] dbname [--dbname] dbname Specifies to which database the language should be added. The default is to use the database with the same name as the current sys- tem user. -e --echo Displays SQL commands as they are executed. -l --list Shows a list of already installed languages in the target database (which must be specified). -L directory Specifies the directory in which the language interpreter is to be found. The directory is normally found automatically; this option is primarily for debugging purposes. createlang also accepts the following command-line arguments for connection parameters: -h host --host host Specifies the host name of the machine on which the server is running. If host begins with a slash, it is used as the directory for the Unix domain socket. -p port --port port Specifies the Internet TCP/IP port or local Unix domain socket file extension on which the server is listening for connections. -U username --username username User name to connect as -W --password Force password prompt. ENVIRONMENT
PGDATABASE PGHOST PGPORT PGUSER Default connection parameters. DIAGNOSTICS
Most error messages are self-explanatory. If not, run createlang with the --echo option and see under the respective SQL command for details. Check also under psql(1) for more possibilities. NOTES
Use droplang(1) to remove a language. createlang is a shell script that invokes psql several times. If you have things arranged so that a password prompt is required to connect, you will be prompted for a password several times. EXAMPLES
To install pltcl into the database template1: $ createlang pltcl template1 SEE ALSO
droplang(1), CREATE LANGUAGE [create_language(7)] Application 2002-11-22 CREATELANG(1)
All times are GMT -4. The time now is 12:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy