UNIX print job sequence no program

//download link for the file: http://www.slideshare.net/hackintoshrao/fileinc
/* First create a file in your Home directory by name seqno.txt,just write one character the
   digit one( 1 ) into the file and save it.This program reads the number from the file , increments it
    then writes back the incremented value back into the file.
    This sequence o program is used to control print jobs in UNIX */

#define PATH “/home/hackintoshrao/seqno.txt” //Enter the path of your home directory here
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
int main()
{

    int i,fd,n;
    char buf[100];
    if((fd=open(PATH,O_RDWR))==-1) //open the file to read and write
       fprintf(stderr,”Error opening file”,50);
    read(fd,buf,100)   ;//read the number from the file
    n=sizeof(buf);
    buf[n]=”;   //terminate with null characters to use sscanf
    sscanf(buf,”%d\n”,&i); //read the number from the string into integer i
    i++;//increment the value
    sprintf(buf,”%d”,i); //write the incremented value into the string
    lseek(fd,0,SEEK_SET);
    /*set the write pointer to the beginning  of the file(the file position was altered
      by the read function after reading the first character*/
    write(fd,buf,sizeof(buf));//write the incremented value back to the file
    close(fd);
    return 0;
}

Advertisements

Program to obtain the short description of a signal given the signal number

/*Program to obtain the short description of a signal
  given the signal number as input , using
  `strsignal()` system call in GNU/LINUX */

#include<string.h>
#include<signal.h>
int main()
{
    char *sigdescrip;
    int signo;
    sigdescrip= malloc(100);
    printf(“\nEnter the Signal Number: “);
    scanf(“%d”,&signo);
    sigdescrip=strsignal(signo);
    // char *strsignal(int signo);
    /*strsignal returns a character pointer to a string which stores
      the short signal description */
    printf(“\n\n%s\n\n”,sigdescrip);
    exit(0);

}

ART OF APP BUILDING IN GNU/LINUX-2:Creating a window with username and time-date info(combining system programming and QT)

 

 

Image

 

Here it is the second post of the series “The Art of app building in GNU/LINUX”.One of the powerful feature of QTCREATOR is that it can interpret HTML codes,in short its HTML aware.This property is used to set the colour and font of the text.
this this post we’ll create a simple app for GNU/LINUX, A basic window displaying User name and the time-date info,here goes the tutorial , happy coding , happy hacking ..

Create a Empty Qt project as shown in the reference video .Add a C++ source file as illustrated in the video, write this piece of into it and run .

 

#include<QApplication>
#include<QLabel>
#include<time.h>
#include<unistd.h>
#include<pwd.h>
#include<string.h>
int main(int argc,char *argv[])
{
    char *buff,*buff1;
    buff=new char [100];
    buff1=new char [1000];
    time_t ticks;
    ticks=time(NULL);
    buff=ctime(&ticks);//buff now contains the date and time details
    struct passwd *pwd;
    pwd=getpwuid(getuid());//to get the usr name
    QApplication app(argc,argv);
    strcat(buff1,”<font color=blue><h2>HI “);
    strcat(buff1,pwd->pw_name);//obtaining the user name
    strcat(buff1,”</h2></font>”);

    strcat(buff1,buff);
/*only one string is supposed to be passed onto Qlabel to display it in the window
  so the strings are concatenated to form one big string with HTML code for font and
  colour with user name   and date-time information*/

    QLabel *label1=new QLabel(buff1);

    label1->showNormal();
    return app.exec();
}

Catching SIGINT(ctrl+c) signal in GNU/LINUX

/*SIGINT
 *RUN the code and press ctrl+c ,This signal is sent to all processes in the foreground process group when the user
 *enters the interrupt character (usually Ctrl-C). The default behavior is to
 *terminate; however, processes can elect to catch and handle this signal, and
 *generally do so to clean up before terminating.*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
/* handler for SIGINT */
static void sigint_handler (int signo)
{

  printf (“Caught SIGINT!\n”);
  exit (EXIT_SUCCESS);
}
int main (void)
{
/*
* Register sigint_handler as our signal handler
* for SIGINT.
*/
if (signal (SIGINT, sigint_handler) == SIG_ERR) {
 fprintf (stderr, “Cannot handle SIGINT!\n”);
exit (EXIT_FAILURE);
}

pause (); //pause waits till a signal is generated
return 0;
}

Signals and Description of Default action in GNU/LINUX

Signals and Description of Default action

 

  1. SIGABRT Sent by abort( ) Terminate with core dump

  2. SIGALRM Sent by alarm( ) Terminate

  3. SIGBUS Hardware or alignment error Terminate with core dump

  4. SIGCHLD Child has terminated Ignored

  5. SIGCONT Process has continued after being stopped Ignored

  6. SIGFPE Arithmetic exception Terminate with core dump

  7. SIGHUP Process’s controlling terminal was closed (most frequently, Terminate the user logged out)

  8. SIGILL Process tried to execute an illegal instruction Terminate with core dump SIGINT User generated the interrupt character (Ctrl-C) Terminate

  9. SIGIO Asynchronous I/O event Terminate

  10. SIGKILL Uncatchable process termination Terminate

  11. SIGPIPE Process wrote to a pipe but there are no readers Terminate

  12. SIGPROF Profiling timer expired Terminate

  13. SIGPWR Power failure Terminate

  14. SIGQUIT User generated the quit character (Ctrl-\) Terminate with core dump

  15. SIGSEGV Memory access violation Terminate with core dump

  16. SIGSTKFLT Coprocessor stack fault Terminate

  17. SIGSTOP Suspends execution of the process Stop

  18. SIGSYS Process tried to execute an invalid system call Terminate with core dump

  19. SIGTERM Catchable process termination Terminate

  20. SIGTRAP Break point encountered Terminate with core dump

  21. SIGTSTP User generated the suspend character (Ctrl-Z) Stop

  22. SIGTTIN Background process read from controlling terminal Stop

  23. SIGTTOU Background process wrote to controlling terminal Stop

  24. SIGURG Urgent I/O pending Ignored

  25. SIGUSR1 Process-defined signal Terminate

  26. SIGUSR2 Process-defined signal Terminate

  27. SIGVTALRM Generated by setitimer( ) when called with the Terminate ITIMER_VIRTUAL flag

  28. SIGWINCH Size of controlling terminal window changed Ignored

  29. SIGXCPU Processor resource limits were exceeded Terminate with core dump

  30. SIGXFSZ File resource limits were exceeded Terminate with core dump

 

Illustration of ‘isatty’ system call in GNU/LINUX

/* Hackintosh rao , codes for GNU/LINUX*/
/* int isatty(fd);
   fd is the file descriptor of a open file
   if the terminal is associated with the file whose Fd is given then ,
   isatty returns 1 , else
   it returns 0
   this system call is used to check whether any open file  with a file descriptor
   is connected to a terminal or not */
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
    if(!(isatty(fileno(stdout))))  /*fileno function returns the filedescriptor*/
    {
        write(2,”\n\nstandard o/p not associated with control terminal\n\n”,60);
        /*cannot write into standard output with File descriptor 1 , when the
        terminal is not associated with it , that is when standard o/p is redirected
        using ‘>’ operator when the program is invoked, so the o/p is written into
        standard error with file descriptor 2*/
        exit(0);
    }
    else

    {
        printf(“\no/p is associated with the control terminal\n\n”);
    }

    return 0;
}

illustration of lseek system call in GNU/LINUX

 

The lseek( ) system call is provided to set the file position of a file descriptor to a given value.

#include <sys/types.h>

#include <unistd.h>

off_t lseek (int fd, off_t pos, int origin);

 

Description of the arguments to lseek

1.fd=file descriptor of the open file

 

2.pos=offset

 

3.The behavior of lseek( ) depends on the origin argument, which can be one of the following:

SEEK_CUR

The current file position of fd is set to its current value plus pos, which can be negative, zero, or positive. A pos of zero returns the current file position value

 

new file postion=current file position + offset(the pos argument to lseek)

 

SEEK_END

The current file position of fd is set to the current length of the file plus pos,

which can be negative, zero, or positive. A pos of zero sets the offset to the end

of the file.

 

new file postion= file position of End Of the File + offset

SEEK_SET

The current file position of fd is set to pos.the offset is measured from the beggining of the file. A pos of zero sets the offset to the beginning of the file.

 

new file postion= offset

The call returns the new file position on success. On error, it returns -1 and errno is set as

appropriate.

 

For example, to set the file position of fd to 1000:

off_t ret;

ret = lseek (fd, (off_t) 1000, SEEK_SET);

if (ret == (off_t) -1)

/* error */

 

 

Alternatively, to set the file position of fd to the end of the file:

off_t ret;

ret = lseek (fd, 0, SEEK_END);

if (ret == (off_t) -1)

/* error */

 

As lseek( ) returns the updated file position, it can be used to find the current file position

via a SEEK_CUR to zero:

int pos;

pos = lseek (fd, 0, SEEK_CUR);

if (pos == (off_t) -1)

/* error */

else

/* ‘pos’ is the current position of fd */

By far, the most common uses of lseek( ) are seeking to the beginning, seeking to the end,

or determining the current file position of a file descriptor.