I can't understand why this code does not print current time in every one second.
What is the problem here ?
#include <stdio.h>
#include <unistd.h>
#include <time.h>
int main(int argc, const char * argv[])
{
while (1) {
sleep(1);
time_t tm;
struct tm *t_struct;
tm = time(NULL);
t_struct = localtime(&tm);
printf("%.2d:%.2d:%.2d", t_struct->tm_hour, t_struct->tm_min, t_struct->tm_sec);
}
return 0;
}
stdout may be line buffered, so you might need to either fflush it after outputting text, or print a newline to make changes visible.
Related
all this in Linux not windows
hello i want to know how i can change the color of xeyes like we can do in terminal like
xeyes -fg blue
now i want to to do this in c program using path
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <malloc.h>
//#inlcude <windows.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[0], fullPathName);
myArgv[1] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
if i simply call /usr/bin/X11/xeyes it just show eyes
now i am trying to add command like /usr/bin/X11/xeyes-fg but its not working
any suggestion?
You can add onto the argument vector, like this:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#define LB_SIZE 1024
int main(int argc, char *argv[])
{
char fullPathName[] = "/usr/bin/X11/xeyes";
char *myArgv[LB_SIZE]; // an array of pointers
int n = 0;
myArgv[0] = (char *) malloc(strlen(fullPathName) + 1);
strcpy(myArgv[n++], fullPathName);
myArgv[n++] = "-fg";
myArgv[n++] = "blue";
myArgv[n] = NULL; // last element should be a NULL pointer
execvp(fullPathName, myArgv);
exit(0); // should not be reached
}
Here is a picture of the result:
Offhand, I would have expected strace to show the file rgb.txt being opened, but do not see this using -f option (assume it happens in the server). The "blue" does show up in a trace, but only in the exec call, e.g.,
execve("/usr/bin/X11/xeyes", ["/usr/bin/X11/xeyes", "-fg", "blue"], [/* 62 vars */]) = 0
I have the following c setuid wrapper:
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
main( int argc, char ** argv ) {
struct passwd *pwd;
char user[] = "cvmfs-test";
pwd = getpwnam(user);
setuid(pwd->pw_uid);
system(argv[1]);
}
I can call my perl script with ./cwrapper perlscript.pl.
I would like to do ./cwrapper perlscript.pl --option1 --option2 --option3 and elaborate all arguments inside the perl script with GetOptions. How should I change my wrapper?
There is also a nice solution which does not need any allocation, is able to deal with arbitrary long commands and does not imply running useless processes because it does not use system. Moreover with the following solution you get the exit code of the spawned process for free.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#define SETUIDUSER "foobar"
int main(int argc, char **argv) {
struct passwd *pwd;
char user[] = SETUIDUSER;
pwd = getpwnam(user);
// success test needed here
setuid(pwd->pw_uid);
// success test needed here
if (argc < 2)
return 1;
execvp(argv[1], &argv[1]);
return 42;
}
Here is a version dealing with a variable number of arguments. Please note that your syscalls should be tested to ensure everything is going OK.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#define CMDMAXLGTH 4096
#define SETUIDUSER "testuser"
int main( int argc, char ** argv ) {
struct passwd *pwd;
char user[] = SETUIDUSER;
char buf[CMDMAXLGTH];
char *p = buf;
int i = 1;
pwd = getpwnam(user);
// success test needed here
setuid(pwd->pw_uid);
// success test needed here
memset (buf, 0, sizeof(buf));
while (argv[i]) {
p += sprintf(p, " %s", argv[i++]);
}
system(buf);
return 0;
}
You should use sprintf to build a character string with your options, then pass this string to system:
char command [100];
sprintf (command, "./cwrapper %s --%s --%s --%s", program_name,option1,option2,
option3);
system(command);
Update: this approach assumes a fixed number of arguments, and looking back at your question, I see that may not be the case.
Ignore the argv[0] because is the name of the c program and use all the other. You can calculate (strlen) the required memory to assemble a new string, malloc() the memory for the new string and then build your new string by concatenating all the argv (strcat). Or for a fixed length approach, follow #dan1111 answer.
#include <langinfo.h>
#include <stdio.h>
int main(int argc, char **argv){
char *firstDayAb;
firstDayAb = nl_langinfo(ABDAY_1);
printf("\nFirst day ab is %s\n", firstDayAb);
return 0;
}
This code works fine on Mac and Linux but it doesn't work on windows due to absence of langinfo.h. How to avoid using langinfo.h? Or maybe there is another way of getting abbreviated weekday name?
#include <stdio.h>
#include <time.h>
int main ()
{
struct tm timeinfo = {0};
char buffer [80];
timeinfo.tm_wday = 1;
strftime (buffer, 80, "First day ab is %a", &timeinfo);
puts (buffer);
return 0;
}
I have found a link Code for header file is given here. It uses KDE32 on windows. I hope this helps you.
I'm trying to print the time in ISO-8601 with decisecond precision.
YYYY-MM-DDThh:mm:ss.s
Here is my code:
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void milli_time(char* dest, struct timeval* t)
{
struct tm* timeInfo;
strftime(dest, 22, "%Y-%m-%dT%t", localtime(&t->tv_sec));
printf("%s\n", dest);
fflush(stdout);
char deciTime[3];
sprintf(deciTime, ".%lu", ((t->tv_usec)/100000ul));
strcat(dest, deciTime);
}
int main()
{
struct timeval* theTime;
char timeString[32];
gettimeofday(theTime, NULL);
printf("%lu.%lu\n", theTime->tv_sec, theTime->tv_usec);
milli_time(timeString, theTime);
printf("%s\n", timeString);
fflush(stdout);
}
And the output every time I run it is:
134520616.3077826840
1974-04-06T17:50:16
1974-04-06T17:50:16.30778
The other thing I notice is that tv_usec is greater than one million.
Change struct timeval* theTime to struct timeval theTime and update the corresponding references to it:
gettimeofday(&theTime, NULL);
// etc
This way you're allocating the space for the struct, rather than just a pointer to the struct. Your code segfaults when I try to run it on my machine.
localtime returns null. Why? (I'm using Visual C++ 2008)
struct tm *tb;
time_t lDate;
time(&lDate);
tb = localtime(&lDate); // tb is null everytime I try this!
Is that your exact code? I just compiled this program and it works fine:
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv)
{
struct tm *tb;
time_t lDate;
time(&lDate);
if (lDate == -1) {
perror("time");
return 1;
}
tb = localtime(&lDate);
if (tb == NULL) {
fprintf(stderr, "localtime failed\n");
return 1;
}
printf("Good\n");
return 0;
}
#include <time.h>
#include <stdio.h>
int main(void)
{
// get the current time
time_t now = time(0);
struct tm* theTime = localtime(&now);
int t=(int)theTime;
printf("%d",t);
getch();
return 0;
}
it works
Define the preprocessor _USE_32BIT_TIME_T in your project and try again. Good luck:)
The code you posted in your comments works fine, up until you get to the if statement. I'm not sure what you are trying to do here, but you have a ; in if (pArea); that almost definitely should not be there (hard to tell since it's formatted horribly because you put it in a comment). You are also returning 0 all the time, is that what you intended to do?