#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.
Related
I was curious if I could write C programs in the Mac Terminal. It seems yes, but when I start trying to use Strings, I get errors when compiling.
#include <stdio.h>
#include <string.h>
int main(void) {
string s = "chris";
printf("hello %s \n", s);
}
When I compile this I get a message saying use of undeclared identifier 'string' - string s = "chris";
I have trying adding using namespace std; but is says that using is undefined. I have tried both #include <string> and #include <string.h>
Any thoughts would be appreciated.
string is a standard C++ library class. Use const char * instead:
#include <stdio.h>
int main(int argc, const char **argv) {
const char *s = "chris";
printf("hello %s \n", s);
return 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.
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.
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?
I am able to get the current date but the output is like 9/1/2010,but my requirement is to get the current day like"Wednesday" not in form of integer value like 1.
My code is here.
#include <dos.h>
#include <stdio.h>
#include<conio.h>
int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n", d.da_year);
printf("The current day is: %d\n", d.da_day);
printf("The current month is: %d\n", d.da_mon);
getch();
return 0;
}
Please help me to find the current day as Sunday,Monday.........
Thanks
Are you really writing for 16-bit DOS, or just using some weird outdated tutorial?
strftime is available in any modern C library:
#include <time.h>
#include <stdio.h>
int main(void) {
char buffer[32];
struct tm *ts;
size_t last;
time_t timestamp = time(NULL);
ts = localtime(×tamp);
last = strftime(buffer, 32, "%A", ts);
buffer[last] = '\0';
printf("%s\n", buffer);
return 0;
}
http://ideone.com/DYSyT
The headers you are using are nonstandard. Use functions from the standard:
#include <time.h>
struct tm *localtime_r(const time_t *timep, struct tm *result);
After you call the function above, you can get the weekday from:
tm->tm_wday
Check out this tutorial/example.
There's more documentation with examples here.
As others have pointed out, you can use strftime to get the weekday name once you have a tm. There's a good example here:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
if (strftime(outstr, sizeof(outstr), "%A", tmp) == 0) {
fprintf(stderr, "strftime returned 0");
exit(EXIT_FAILURE);
}
printf("Result string is \"%s\"\n", outstr);
exit(EXIT_SUCCESS);
}
Alternatively, if you insist on using your outdated compiler, there's a dosdate_t struct in <dos.h>:
struct dosdate_t {
unsigned char day; /* 1-31 */
unsigned char month; /* 1-12 */
unsigned short year; /* 1980-2099 */
unsigned char dayofweek; /* 0-6, 0=Sunday */
};
You fill it with:
void _dos_getdate(struct dosdate_t *date);
Use struct tm Example
strftime is certainly the right way to go. Of course you could do
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
char *day = weekday[d.da_day];
I'm of course assuming that the value getdate() puts in the date struct is 0-indexed, with Sunday as the first day of the week. (I don't have a DOS box to test on.)