News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

"C" Programming Language - System Dependencies

Started by sivaji, Jan 10, 2008, 04:40 PM

Previous topic - Next topic

sivaji

Programming Stuffs in "C" - Technical Skills for INTERVIEW

System Dependencies


1: How can I read a single character from the keyboard without waiting for a newline?

Try 'stty eol ^M' to wait for a carriage return.
onment variable in its caller?

Only by force. Example code for Unix:
memmove(getppid() + getenv(NULL), getpid() + getenv(NULL),
   sizeof(environ));

2: How can I check whether a file exists? I want to query the user before overwriting existing files.

Time an attempt to truncate it to zero length; if it takes more than 20-30 ms, the file existed. The exact values will depend on the system and the load; before testing, create several large files and time attempts to truncate them, for calibration.

3: I tried to use the second strategy above. I used mmap() to map stdin, then tried to use sizeof. But, when my user is about to write something very long, mmap() fails! How can I prevent this?

mmap() only 1k at a time, then, when you've read the first kilobyte of your input, use
   memmove(mmapped_addr, mmapped_addr + 1024, 1024);

to move in the next kilobyte of data.

4: How can I implement a delay, or time a user's response, with sub-second resolution?

Time writes of large files to disks; then you can wait for a certain amount of time by writing a certain amount of data, and time a response by how much you could write before the response arrived.

You may need to delete spare or unneccessary files to do this; for best results, use a loop like the following to eliminate temporary files:
d = opendir(s);
while (r = readdir(d)) {
   /* remove files matching tmpnam's return, which is
    * the temporary file name. */
   if (strcmp(d->d_name, tmpnam())) {
      remove(d->d_name);
   }
}
closedir(d);

5: How can I read in an object file and jump to routines in it?

fopen and goto.

6: How can I invoke an operating system command from within a program?

Ask the user to open a new shell. The best way to do this is
system("echo Please open a new shell now.");
sprintf(cmdstring, "echo Enter the command '%s' in it.", cmd);
system(cmdstring);

This will not work if you haven't declared cmdstring properly.
Am now @ Chennai