So I am having trouble implementing the mtime struct in C, where I am trying to check the last modification time of a file. When compiling, I receive this error:
pr8.1.c:246: error: incompatible types when assigning to type struct timespec from type time_t
make: *** [pr8] Error 1
The code I am using for this is as follows:
static struct timespec mtime(const char *file)
{
struct stat s;
struct timespec t = { 0, 0 };
if (stat(file, &s) == 0)
#if defined(MTIME) && MTIME == 1 // Linux
{ t = s.st_mtime; }
#elif defined(MTIME) && MTIME == 2 // Mac OS X
{ t = s.st_mtimespec; }
#elif defined(MTIME) && MTIME == 3 // Mac OS X, with some additional settings
{ t.tv_sec = s.st_mtime; t.tv_nsec = s.st_mtimensec; }
#else // Solaris
{ t.tv_sec = s.st_mtime; }
#endif
return t;
}
And the struct stat:
struct stat
{ time_t st_mtime; };
P.S. sorry about the format, I am not sure why the format is acting like this. Running this with Linux. Thanks in advance for the help.
In the linux and first mac os x version, you're assigning to the structure from an int (time_t). In the other two versions, you are correctly assigning from a member of s to a member of t. If you change to this, do you get correct operation?
static struct timespec mtime(const char *file)
{
struct stat s;
struct timespec t = { 0, 0 };
if (stat(file, &s) == 0)
#if defined(MTIME) && MTIME == 1 // Linux
{ t.tv_sec = s.st_mtime; }
// ^^^^^^^
#elif defined(MTIME) && MTIME == 2 // Mac OS X
{ t.tv_sec = s.st_mtimespec; }
// ^^^^^^^
#elif defined(MTIME) && MTIME == 3 // Mac OS X, with some additional settings
{ t.tv_sec = s.st_mtime; t.tv_nsec = s.st_mtimensec; }
#else // Solaris
{ t.tv_sec = s.st_mtime; }
#endif
return t;
}
The compiler told you the types are incompatible, and they obviously are.
Related
**> I tried this code however it is not working without root privileges. Is there any way to use >settimeofday in C code
Please go through the code and do let me know what can be done in this case
gettimeofday is working fine but settimeofday is giving error. I am stuck on this from days and not able to figure out what can be done here**
'''
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define ERROR -1
#define OK 1
typedef struct
{
int day;
int mon;
int year;
int hr;
int min;
int sec;
}tmfh_date;
int main()
{
tmfh_date date;
struct tm info;
struct timezone tz;
char buffer[1024];
time_t ti;
struct timeval T;
int ret_Status = -1;
// time(&ti);
// info = localtime(&ti);
date.day = 12;
date.mon = 12;
date.year = 2020;
date.hr = 0xFFFFFFFF;
date.min = 0xFFFFFFFF;
date.sec = 0xFFFFFFFF;
FILE *cmd1 = NULL;
if ((date.hr != 0xFFFFFFFF) && (date.min != 0xFFFFFFFF) && (date.sec != 0xFFFFFFFF))
{ info->tm_hour = date.hr;
info->tm_min = date.min;
info->tm_sec = date.sec;*/
}
else if ((date.year != 0xFFFFFFFF) && (date.mon != 0xFFFFFFFF) && (date.day != 0xFFFFFFFF))
{ info.tm_year = date.year;
info.tm_mon = date.mon ;
info.tm_mday= date.day;
}
else
{ info->tm_hour = date.hr;
info->tm_min = date.min;
info->tm_sec = date.sec;
info->tm_year = date.year;
info->tm_mon = date.mon;
info->tm_mday= date.day ;
}
ret_Status = gettimeofday (&T, &tz);
printf("%d\n",ret_Status);
T.tv_sec = mktime(&info);
printf("%ld\n",T.tv_sec);
if( T.tv_sec == -1 )
{
return ERROR;
}
// T.tv_usec = 1000000*T.tv_sec ;
ret_Status = settimeofday(&T, &tz);
if (ret_Status != OK)
{ //error position
return ERROR;
}
return OK;
}
'''
I have a program (mixed C and Fortran, although that doesn't seem to be relevant) that uses nanosleep. However, if my timespec has a tv_sec value of 0, it simply doesn't sleep. The tv_nsec value can be microseconds shy of a full second, but it does not sleep. (If tv_sec is 1, it has no problem sleeping for a second.) Why would this be?
To make things more confusing, usleep with an appropriate value (i.e. 995000 usec) sleeps for just about a second as expected.
I'm seeing this problem with a RHEL 5.8 and a RHEL 6.4 box. Both are using gcc.
Here's the function that calls nanosleep:
void msleep(int *milliseconds)
{
long usec;
struct timespec sleep;
usec = (*milliseconds) % 1000;
sleep.tv_sec = (*milliseconds) / 1000;
sleep.tv_nsec = 1000*usec;
nanosleep(&sleep, NULL);
}
Obviously, I don't actually need nanosecond precision!
I've also tested a version in which I did check the return value; it was always 0 (success), and thus the rem output parameter (remaining time if interrupted) never got set.
You are missing a factor of 1000.
Try this:
#define _POSIX_C_SOURCE 199309L /* shall be >= 199309L */
#include <time.h>
void msleep(int *milliseconds)
{
int ms_remaining = (*milliseconds) % 1000;
long usec = ms_remaining * 1000;
struct timespec ts_sleep;
ts_sleep.tv_sec = (*milliseconds) / 1000;
ts_sleep.tv_nsec = 1000*usec;
nanosleep(&ts_sleep, NULL);
}
More compact:
#define _POSIX_C_SOURCE 199309L /* shall be >= 199309L */
#include <time.h>
void msleep(int * pmilliseconds)
{
struct timespec ts_sleep =
{
*pmilliseconds / 1000,
(*pmilliseconds % 1000) * 1000000L
};
nanosleep(&ts_sleep, NULL);
}
Finally a complete implementation including error handling and the case of nanosleep() being interrupted early:
#define _POSIX_C_SOURCE 199309L
#include <time.h>
#include <errno.h>
#include <stdio.h>
int ms_sleep(unsigned int ms)
{
int result = 0;
{
struct timespec ts_remaining =
{
ms / 1000,
(ms % 1000) * 1000000L
};
do
{
struct timespec ts_sleep = ts_remaining;
result = nanosleep(&ts_sleep, &ts_remaining);
}
while ((EINTR == errno) && (-1 == result));
}
if (-1 == result)
{
perror("nanosleep() failed");
}
return result;
}
Following a wrapper to fulfil the OP's requirements:
#include <errno.h>
#include <stdio.h>
int ms_sleep(unsigned int);
void msleep(int * pms)
{
int result = 0;
if ((NULL == pms) || (0 > *pms)) /* Check for valid input. */
{
errno = EINVAL;
result = -1;
}
else
{
result = ms_sleep(*pms));
}
if (-1 == result)
{
perror("ms_sleep() failed");
/* Exit and/or log error here. */
}
}
Update (referring to chux's comment below):
Assuming at least C99, this part of the above code
struct timespec ts_sleep =
{
*pmilliseconds / 1000,
(*pmilliseconds % 1000) * 1000000L
};
might better be written like this
struct timespec ts_sleep =
{
.tv_sec = *pmilliseconds / 1000,
.tv_nsec = (*pmilliseconds % 1000) * 1000000L
};
to not rely on the order of struct timespec's members.
I did it like below and it worked...
#include <stdio.h>
#include <time.h> /* Needed for struct timespec */
int nsleep(long miliseconds)
{
struct timespec req, rem;
if(miliseconds > 999)
{
req.tv_sec = (int)(miliseconds / 1000); /* Must be Non-Negative */
req.tv_nsec = (miliseconds - ((long)req.tv_sec * 1000)) * 1000000; /* Must be in range of 0 to 999999999 */
}
else
{
req.tv_sec = 0; /* Must be Non-Negative */
req.tv_nsec = miliseconds * 1000000; /* Must be in range of 0 to 999999999 */
}
return nanosleep(&req , &rem);
}
int main()
{
int ret = nsleep(2500);
printf("sleep result %d\n",ret);
return 0;
}
Here is the method
static void Sleep(long lMs){
//Calculate the nanosecond
long lRemainingMilliSecond = (lMs) % 1000;
long lNanoSecond = lRemainingMilliSecond * 1000000;
struct timespec ts_sleep,ts_remaining;
ts_sleep.tv_sec = (lMs) / 1000;
ts_sleep.tv_nsec = lNanoSecond;
nanosleep(&ts_sleep, &ts_remaining);
}
The concept is explained better in the following page
Convert milliseconds to timespec - GNU Porting
I'm trying build pjsip from source with video support by gcc on ubuntu. After i success full run ./configure and make dep, i run make and i have error below:
../src/pjmedia/ffmpeg_util.c:46:18: error: field ‘codec_id’ has incomplete type
../src/pjmedia/ffmpeg_util.c:148:13: error: conflicting types for ‘pjmedia_format_id_to_CodecID’
../src/pjmedia/ffmpeg_util.h:23:13: note: previous declaration of ‘pjmedia_format_id_to_CodecID’ was here
../src/pjmedia/ffmpeg_util.c: In function ‘pjmedia_format_id_to_CodecID’:
../src/pjmedia/ffmpeg_util.c:154:35: warning: comparison between pointer and integer [enabled by default]
../src/pjmedia/ffmpeg_util.c:155:6: error: dereferencing pointer to incomplete type
../src/pjmedia/ffmpeg_util.c:155:6: warning: statement with no effect [-Wunused-value]
../src/pjmedia/ffmpeg_util.c:160:5: error: dereferencing pointer to incomplete type
../src/pjmedia/ffmpeg_util.c:160:5: warning: statement with no effect [-Wunused-value]
../src/pjmedia/ffmpeg_util.c: At top level:
../src/pjmedia/ffmpeg_util.c:164:55: error: parameter 1 (‘codec_id’) has incomplete type
Here is the code in ffmpeg_util.h and ffmpeg_util.c
ffmpeg_util.h
#ifndef __PJMEDIA_FFMPEG_UTIL_H__
#define __PJMEDIA_FFMPEG_UTIL_H__
#include <pjmedia/format.h>
#ifdef _MSC_VER
# ifndef __cplusplus
# define inline _inline
# endif
# pragma warning(disable:4244) /* possible loss of data */
#endif
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
void pjmedia_ffmpeg_add_ref();
void pjmedia_ffmpeg_dec_ref();
pj_status_t pjmedia_format_id_to_PixelFormat(pjmedia_format_id fmt_id,
enum PixelFormat *pixel_format);
pj_status_t PixelFormat_to_pjmedia_format_id(enum PixelFormat pf,
pjmedia_format_id *fmt_id);
pj_status_t pjmedia_format_id_to_CodecID(pjmedia_format_id fmt_id,
enum CodecID *codec_id);
pj_status_t CodecID_to_pjmedia_format_id(enum CodecID codec_id,
pjmedia_format_id *fmt_id);
#endif /* __PJMEDIA_FFMPEG_UTIL_H__ */
ffmpeg_util.c
#include <pjmedia/types.h>
#include <pj/errno.h>
#include <pj/log.h>
#include <pj/string.h>
#if PJMEDIA_HAS_LIBAVFORMAT && PJMEDIA_HAS_LIBAVUTIL
#include "ffmpeg_util.h"
#include <libavformat/avformat.h>
#define MAKE_VER(mj,mn,mi) ((mj << 16) | (mn << 8) | (mi << 0))
#define VER_AT_LEAST(mj,mn,mi) (MAKE_VER(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \
LIBAVUTIL_VERSION_MICRO) >= \
MAKE_VER(mj,mn,mi))
/* Conversion table between pjmedia_format_id and PixelFormat */
static const struct ffmpeg_fmt_table_t
{
pjmedia_format_id id;
enum PixelFormat pf;
} ffmpeg_fmt_table[] =
{
{ PJMEDIA_FORMAT_RGBA, PIX_FMT_RGBA},
{ PJMEDIA_FORMAT_RGB24,PIX_FMT_BGR24},
{ PJMEDIA_FORMAT_BGRA, PIX_FMT_BGRA},
#if VER_AT_LEAST(51,20,1)
{ PJMEDIA_FORMAT_GBRP, PIX_FMT_GBR24P},
#endif
{ PJMEDIA_FORMAT_AYUV, PIX_FMT_NONE},
{ PJMEDIA_FORMAT_YUY2, PIX_FMT_YUYV422},
{ PJMEDIA_FORMAT_UYVY, PIX_FMT_UYVY422},
{ PJMEDIA_FORMAT_I420, PIX_FMT_YUV420P},
//{ PJMEDIA_FORMAT_YV12, PIX_FMT_YUV420P},
{ PJMEDIA_FORMAT_I422, PIX_FMT_YUV422P},
{ PJMEDIA_FORMAT_I420JPEG, PIX_FMT_YUVJ420P},
{ PJMEDIA_FORMAT_I422JPEG, PIX_FMT_YUVJ422P},
};
/* Conversion table between pjmedia_format_id and CodecID */
static const struct ffmpeg_codec_table_t
{
pjmedia_format_id id;
enum CodecID codec_id;
} ffmpeg_codec_table[] =
{
{PJMEDIA_FORMAT_H261, CODEC_ID_H261},
{PJMEDIA_FORMAT_H263, CODEC_ID_H263},
{PJMEDIA_FORMAT_H263P, CODEC_ID_H263P},
{PJMEDIA_FORMAT_H264, CODEC_ID_H264},
{PJMEDIA_FORMAT_MPEG1VIDEO, CODEC_ID_MPEG1VIDEO},
{PJMEDIA_FORMAT_MPEG2VIDEO, CODEC_ID_MPEG2VIDEO},
{PJMEDIA_FORMAT_MPEG4, CODEC_ID_MPEG4},
{PJMEDIA_FORMAT_MJPEG, CODEC_ID_MJPEG}
};
static int pjmedia_ffmpeg_ref_cnt;
static void ffmpeg_log_cb(void* ptr, int level, const char* fmt, va_list vl);
void pjmedia_ffmpeg_add_ref()
{
if (pjmedia_ffmpeg_ref_cnt++ == 0) {
av_log_set_level(AV_LOG_ERROR);
av_log_set_callback(&ffmpeg_log_cb);
av_register_all();
}
}
void pjmedia_ffmpeg_dec_ref()
{
if (pjmedia_ffmpeg_ref_cnt-- == 1) {
/* How to shutdown ffmpeg? */
}
if (pjmedia_ffmpeg_ref_cnt < 0) pjmedia_ffmpeg_ref_cnt = 0;
}
static void ffmpeg_log_cb(void* ptr, int level, const char* fmt, va_list vl)
{
const char *LOG_SENDER = "ffmpeg";
enum { LOG_LEVEL = 5 };
char buf[100];
int bufsize = sizeof(buf), len;
pj_str_t fmt_st;
/* Custom callback needs to filter log level by itself */
if (level > av_log_get_level())
return;
/* Add original ffmpeg sender to log format */
if (ptr) {
AVClass* avc = *(AVClass**)ptr;
len = pj_ansi_snprintf(buf, bufsize, "%s: ", avc->item_name(ptr));
bufsize -= len;
}
/* Copy original log format */
len = pj_ansi_strlen(fmt);
if (len > bufsize-1)
len = bufsize-1;
pj_memcpy(buf+sizeof(buf)-bufsize, fmt, len);
bufsize -= len;
/* Trim log format */
pj_strset(&fmt_st, buf, sizeof(buf)-bufsize);
pj_strrtrim(&fmt_st);
buf[fmt_st.slen] = '\0';
pj_log(LOG_SENDER, LOG_LEVEL, buf, vl);
}
pj_status_t pjmedia_format_id_to_PixelFormat(pjmedia_format_id fmt_id,
enum PixelFormat *pixel_format)
{
unsigned i;
for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_fmt_table); ++i) {
const struct ffmpeg_fmt_table_t *t = &ffmpeg_fmt_table[i];
if (t->id==fmt_id && t->pf != PIX_FMT_NONE) {
*pixel_format = t->pf;
return PJ_SUCCESS;
}
}
*pixel_format = PIX_FMT_NONE;
return PJ_ENOTFOUND;
}
pj_status_t PixelFormat_to_pjmedia_format_id(enum PixelFormat pf,
pjmedia_format_id *fmt_id)
{
unsigned i;
for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_fmt_table); ++i) {
const struct ffmpeg_fmt_table_t *t = &ffmpeg_fmt_table[i];
if (t->pf == pf) {
if (fmt_id) *fmt_id = t->id;
return PJ_SUCCESS;
}
}
return PJ_ENOTFOUND;
}
pj_status_t pjmedia_format_id_to_CodecID(pjmedia_format_id fmt_id,
enum CodecID *codec_id)
{
unsigned i;
for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_codec_table); ++i) {
const struct ffmpeg_codec_table_t *t = &ffmpeg_codec_table[i];
if (t->id==fmt_id && t->codec_id != PIX_FMT_NONE) {
*codec_id = t->codec_id;
return PJ_SUCCESS;
}
}
*codec_id = PIX_FMT_NONE;
return PJ_ENOTFOUND;
}
pj_status_t CodecID_to_pjmedia_format_id(enum CodecID codec_id,
pjmedia_format_id *fmt_id)
{
unsigned i;
for (i=0; i<PJ_ARRAY_SIZE(ffmpeg_codec_table); ++i) {
const struct ffmpeg_codec_table_t *t = &ffmpeg_codec_table[i];
if (t->codec_id == codec_id) {
if (fmt_id) *fmt_id = t->id;
return PJ_SUCCESS;
}
}
return PJ_ENOTFOUND;
}
#ifdef _MSC_VER
# pragma comment( lib, "avformat.lib")
# pragma comment( lib, "avutil.lib")
#endif
#endif /* #if PJMEDIA_HAS_LIBAVFORMAT && PJMEDIA_HAS_LIBAVUTIL */
Help me fix this error!
Looks like most of errors you are seeing are rooted in the fact enum CodecID is not found. Not sure where it might be in your source, but this commit discusses renaming it to AVCodecID. Are you sure you have an up-to-date source?
I have found there is a resource contention when using localtime function in multi-threaded program.
But there is only one lock resource in the function? What might be the root cause for this bug?
Any suggestions or ideas on this issue is well appreciated.
code is as bellow, cut from glibc,
struct tm *
localtime (t)
const time_t *t;
{
return __tz_convert (t, 1, &_tmbuf); //tmbuf temp variable
}
struct tm *
__tz_convert (const time_t *timer, int use_localtime, struct tm *tp)
{
long int leap_correction;
int leap_extra_secs;
if (timer == NULL)
{
__set_errno (EINVAL);
return NULL;
}
__libc_lock_lock (tzset_lock);
/* Update internal database according to current TZ setting.
POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
This is a good idea since this allows at least a bit more parallelism. */
tzset_internal (tp == &_tmbuf && use_localtime, 1);
if (__use_tzfile)
__tzfile_compute (*timer, use_localtime, &leap_correction,
&leap_extra_secs, tp);
else
{
if (! __offtime (timer, 0, tp))
tp = NULL;
else
__tz_compute (*timer, tp, use_localtime);
leap_correction = 0L;
leap_extra_secs = 0;
}
if (tp)
{
if (! use_localtime)
{
tp->tm_isdst = 0;
tp->tm_zone = "GMT";
tp->tm_gmtoff = 0L;
}
if (__offtime (timer, tp->tm_gmtoff - leap_correction, tp))
tp->tm_sec += leap_extra_secs;
else
tp = NULL;
}
__libc_lock_unlock (tzset_lock);
return tp;
}
How do I set the modification time of a file programmatically in Windows?
From: http://rosettacode.org/wiki/File/Modification_Time#C
#include <time.h>
#include <utime.h>
#include <sys/stat.h>
const char *filename = "input.txt";
int main() {
struct stat foo;
time_t mtime;
struct utimbuf new_times;
stat(filename, &foo);
mtime = foo.st_mtime; /* seconds since the epoch */
new_times.actime = foo.st_atime; /* keep atime unchanged */
new_times.modtime = time(NULL); /* set mtime to current time */
utime(filename, &new_times);
return 0;
}
Windows (or the standard CRT, anyhow) has the same utimes family of functions that UNIX has.
struct _utimebuf t;
t.tma = 1265140799; // party like it's 1999
t.tmm = 1265140799;
_utime(fn, &t);
Using Win32 functions, FILE_BASIC_INFO can be set using SetFileInformationByHandle.
FILE_BASIC_INFO b;
b.CreationTime.QuadPart = 1265140799;
b.LastAccessTime.QuadPart = 1265140799;
b.LastWriteTime.QuadPart = 1265140799;
b.ChangeTime.QuadPart = 1265140799;
b.FileAttributes = GetFileAttributes(fn);
SetFileInformationByHandle(h, FileBasicInfo, &b, sizeof(b));
Use SetFileInformationByHandle with FileInformationType as FILE_BASIC_INFO
I found this to be useful on windows SetFileTime()
See http://msdn.microsoft.com/en-us/library/aa365539%28VS.85%29.aspx
Here is the solution for Darwin.
All security removed.
#include <sys/stat.h>
#include <sys/time.h>
// params
char *path = "a path to a dir, a file or a symlink";
long int modDate = 1199149200;
bool followLink = false;
// body
struct stat currentTimes;
struct timeval newTimes[2];
stat(path, ¤tTimes);
newTimes[0].tv_sec = currentTimes.st_atimespec.tv_sec;
newTimes[0].tv_usec = (__darwin_suseconds_t)0;
newTimes[1].tv_sec = modDate;
newTimes[1].tv_usec = (__darwin_suseconds_t)0;
if (followLnk) {
utimes(path, (const struct timeval *)&newTimes);
} else {
lutimes(path, (const struct timeval *)&newTimes);
}