Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Appendix C Chapter 7 Solutions 919


printf("pw_passwd = %s\n", ptr->pw_passwd == NULL ||
ptr->pw_passwd[0] == 0? "(null)" : ptr->pw_passwd);
exit(0);
}

Figure C.6 Print encrypted passwordunder FreeBSD and Mac OS X

6.5 The program shown in FigureC.7 prints the date in a format similar to thedate
command.
#include "apue.h"
#include <time.h>

int
main(void)
{
time_t caltime;
struct tm *tm;
char line[MAXLINE];

if ((caltime = time(NULL)) == -1)
err_sys("time error");
if ((tm = localtime(&caltime)) == NULL)
err_sys("localtime error");
if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", tm) == 0)
err_sys("strftime error");
fputs(line, stdout);
exit(0);
}

Figure C.7 Print the time and date in a format similar todate( 1 )

Running this program gives us
$./a.out author ’sdefault is US/Eastern
Wed Jul 25 22:58:32 EDT 2012
$TZ=US/Mountain ./a.out U.S. Mountain time zone
Wed Jul 25 20:58:32 MDT 2012
$TZ=Japan ./a.out Japan
Thu Jul 26 11:58:32 JST 2012

Chapter 7


7.1 It appears that the return value fromprintf(the number of characters output)
becomes the return value ofmain.Toverify this theory,change the length of the
string printed and see if the new length matches the return value. Note that not
all systems exhibit this property.Also note that if you enable the ISO C extensions
ingcc,then the return value is always 0, as required by the standard.
Free download pdf