#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>

void prtime() {
        time_t rawtime;
        struct tm * timeinfo;
        char *s;

        signal(SIGALRM, prtime);

        time ( &rawtime );
        timeinfo = localtime ( &rawtime );

        alarm (1) ; //printf("Time\n");
        printf ( "\033[s\033[6;45H %s \033[u", asctime (timeinfo) );

int main()
        signal(SIGALRM, prtime);
        printf("continue instructions\n");
        while (1) {
                printf("continue instructions\n");

I just try to write the clock at a specific position in terminal, but I want to save the cursor position before writing the clock, and then restore that position after writing the clock.

but the \033[s & \033[u doesn't work.

closed as off topic by manatwork, Renan, rahmu, Gilles, jasonwryan Feb 22 '13 at 18:58

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.


You are using commands CSI s and CSI u which have incompatible interpretations depending on the implementation.

The first one can be either "Save cursor position" or "Reset terminal emulator", the latter being a Solaris private interface.

You should use a library like ncurses to achieve portable screen handling but if you still want to hardcode the escape sequences, this might do what you expect:

printf ( "\0337\033[6;45H %s \0338", asctime (timeinfo) );

Not the answer you're looking for? Browse other questions tagged or ask your own question.