Pregunta

I am new to this forum and to C programming as well. I want to display the tty modification time before sleep and after sleep (if I enter something on terminal during sleep). I am using the following code but if I declare struct stat as global variable, this gives the correct modification time but does not change it after sleep. On the other hand, I declare struct stat as local variable, it gives me completely incorrect date but seems to change the date after sleep. I am trying to figure this out for two days but no luck. Please help

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

char *modTime;
void getmt();
time_t modifiedTime;

//struct stat statbuf; //this gives the correct modification time but does not change it   after sleep

int main(int argc, char **argv)
{
    int i, status = 0;
    time_t now;

    if (argc == 1) {
        fprintf(stderr, "usage: %s file \n", argv[0]);
        return(1);
    }

    for (i = 1; i < argc; i++)
    {
        struct stat statbuf;

        if (lstat(argv[i], &statbuf)) {
            perror(argv[i]);
            status = 1;
        }
        else
        {
            getmt();
            time(&now);
            printf("\nNOW %ld\n", now);
            sleep(20);
            time(&now);
            printf("after sleep %ld\n", now);
            getmt();
        }
    }

    return(status);
}

void getmt()
{
    struct stat statbuf; //this does not give correct modification time but changes it after sleep
    time_t modifiedTime;

    //modification time of tty as string
    modTime = ctime(&statbuf.st_mtime);
    printf("\n last modified time as string %s\n", modTime);

    //modification time of tty as long date
    modifiedTime = statbuf.st_mtime;
    printf ("last modified time as long date %ld\n", modifiedTime);
}
¿Fue útil?

Solución

The struct stat statbuf inside getmt() is never initialized, so it is full of garbage values. Note that although it has the same name as the (commented-out) global variable, it is a different object, living at a different address, created when getmt() starts and destroyed again when getmt() returns.

The lstat call fills in a struct stat object at the time it is called. You call it only once in the per-argv loop, so you will only get one time-value. You must call lstat again after the sleep() to see if the modification-time has changed.

Otros consejos

I tried to do as torek suggested but it worked only partly as it changes the modification time no matter I touch the keyboard (during the sleep) or not. Here is what I did

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


char *modTime;
 void getmt();
time_t modifiedTime;

struct stat statbuf; 


int main(int argc, char **argv)
{
  int i, status = 0;
  time_t now;


if (argc == 1) {
fprintf(stderr, "usage: %s file \n", argv[0]);
return(1);
}

 for (i = 1; i < argc; i++)
 {
    //struct stat statbuf;

    if (lstat(argv[i], &statbuf)) {
    perror(argv[i]);
    status = 1;
   }

    else
    {

    getmt();


    time(&now);
    printf("\nNOW %ld\n", now);


}
 }


    sleep(20);

     for (i = 1; i < argc; i++)
 {

    if (lstat(argv[i], &statbuf)) {
    perror(argv[i]);
    status = 1;
   }

    else
    {


    time(&now);


    printf("after sleep %ld\n", now);
    getmt();

  }



}//for


    return(status);
 }


void getmt()
{

 time_t modifiedTime;

 //modification time of tty as string
 modTime = ctime(&statbuf.st_mtime);
    printf("\n last modified time as string %s\n", modTime);


       //modification time of tty as long date
       modifiedTime = statbuf.st_mtime;

    printf ("last modified time as long date %ld\n", modifiedTime);

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top