C Programming Code Examples
C > Gnu-Linux Code Examples
Some cd-rom functions
/* Some cd-rom functions */
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>
/*
// close
*/
int cdr_close(char *dev) {
int fd;
if((fd = open(dev, O_RDONLY|O_NONBLOCK)) == -1)
return -1;
else if(ioctl(fd, CDROMCLOSETRAY) == -1)
return -1;
else
close(fd);
return 0;
}
/*
// eject
*/
int cdr_eject(char *dev) {
int fd;
if((fd = open(dev, O_RDONLY|O_NONBLOCK)) == -1)
return -1;
else if(ioctl(fd, CDROMEJECT) == -1)
return -1;
else
close(fd);
return 0;
}
/*
// lock
// - if lock == 1, lock
// - if lock == 0, unlock
*/
int cdlock(char *dev, int lock) {
int fd;
if((fd = open(dev, O_RDONLY|O_NONBLOCK)) == -1)
return -1;
else if(ioctl(fd, CDROM_LOCKDOOR, lock) == -1)
return -1;
else
close(fd);
return 0;
}
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
The open() function shall establish the connection between a file and a file descriptor. It shall create an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file. The open() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor shall not share it with any other process in the system. The FD_CLOEXEC file descriptor flag associated with the new file descriptor shall be cleared.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Closes a file descriptor, fildes. This frees the file descriptor to be returned by future open() calls and other calls that create file descriptors. The fildes argument must represent a hierarchical file system (HFS) file. When the last open file descriptor for a file is closed, the file itself is closed. If the file's link count is 0 at that time, its space is freed and the file becomes inaccessible. When the last open file descriptor for a pipe or FIFO file is closed, any data remaining in the pipe or FIFO file is discarded. close() unlocks (removes) all outstanding record locks that a process has on the associated file. Behavior for sockets: close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.
The ioctl() function manipulates the underlying device parameters of special files. In particular, many operating characteristics of character special files (e.g. terminals) may be controlled with ioctl() requests. The argument d must be an open file descriptor. The second argument is a device-dependent request code. The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion. An ioctl() request has encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp in bytes. Macros and defines used in specifying an ioctl() request are located in the file <sys/ioctl.h>.
'StrrChr' is similar to the function 'StrChr', the only difference is that it searches the string in "Reverse Order", would have understood why we have extra r in 'strrchr', yes you guessed it
C program code asking the character from user to enter. Returns nonzero if character is a printable character, including a space; otherwise zero is returned. It's printable char.
C Program to find the biggest number in an array of numbers using recursion. Program prints the largest number in an unsorted array of elements using recursion. Array is...
C various file opening modes: File is opened using fopen() c function, while opening you can use any of the following mode as per the requirement. Mode "r": It is a read only mode,
Take the Number which you have to Reverse as the input. Obtain its quotient & remainder. Multiply the Separate variable with 10 & add the obtained remainder to it. Do step 2 again