Printing Language-Independent Date and Time
The following statement can be used to print date and time using
a language-independent format:
printf(format, weekday, month, day, hour, min);
For American usage, format could be a pointer to the following
string:
"%s, %s %d, %d:%.2d\n"
This example would produce the following message:
Sunday, July 3, 10:02
For German usage, format could be a pointer to the following
string:
"%1$s, %3$d. %2$s, %4$d:%5$.2d\n"
This definition of format would produce the following message:
Sonntag, 3. Juli, 10:02
Printing File Information
The following example prints information about the type,
permissions, and number of links of a specific file in a
directory.
The first two calls to printf() use data decoded from a previous
stat() call. The user-defined strperm() function shall return a
string similar to the one at the beginning of the output for the
following command:
ls -l
The next call to printf() outputs the owner's name if it is found
using getpwuid(); the getpwuid() function shall return a passwd
structure from which the name of the user is extracted. If the
user name is not found, the program instead prints out the
numeric value of the user ID.
The next call prints out the group name if it is found using
getgrgid(); getgrgid() is very similar to getpwuid() except that
it shall return group information based on the group number.
Once again, if the group is not found, the program prints the
numeric value of the group for the entry.
The final call to printf() prints the size of the file.
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
char *strperm (mode_t);
...
struct stat statbuf;
struct passwd *pwd;
struct group *grp;
...
printf("%10.10s", strperm (statbuf.st_mode));
printf("%4d", statbuf.st_nlink);
if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
printf(" %-8.8s", pwd->pw_name);
else
printf(" %-8ld", (long) statbuf.st_uid);
if ((grp = getgrgid(statbuf.st_gid)) != NULL)
printf(" %-8.8s", grp->gr_name);
else
printf(" %-8ld", (long) statbuf.st_gid);
printf("%9jd", (intmax_t) statbuf.st_size);
...
Printing a Localized Date String
The following example gets a localized date string. The
nl_langinfo() function shall return the localized date string,
which specifies the order and layout of the date. The strftime()
function takes this information and, using the tm
structure for
values, places the date and time information into datestring.
The printf() function then outputs datestring and the name of the
entry.
#include <stdio.h>
#include <time.h>
#include <langinfo.h>
...
struct dirent *dp;
struct tm *tm;
char datestring[256];
...
strftime(datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm);
printf(" %s %s\n", datestring, dp->d_name);
...
Printing Error Information
The following example uses fprintf() to write error information
to standard error.
In the first group of calls, the program tries to open the
password lock file named LOCKFILE
. If the file already exists,
this is an error, as indicated by the O_EXCL flag on the open()
function. If the call fails, the program assumes that someone
else is updating the password file, and the program exits.
The next group of calls saves a new password file as the current
password file by creating a link between LOCKFILE
and the new
password file PASSWDFILE
.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define LOCKFILE "/etc/ptmp"
#define PASSWDFILE "/etc/passwd"
...
int pfd;
...
if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
{
fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n");
exit(1);
}
...
if (link(LOCKFILE,PASSWDFILE) == -1) {
fprintf(stderr, "Link error: %s\n", strerror(errno));
exit(1);
}
...
Printing Usage Information
The following example checks to make sure the program has the
necessary arguments, and uses fprintf() to print usage
information if the expected number of arguments is not present.
#include <stdio.h>
#include <stdlib.h>
...
char *Options = "hdbtl";
...
if (argc < 2) {
fprintf(stderr, "Usage: %s -%s <file\n", argv[0], Options); exit(1);
}
...
Formatting a Decimal String
The following example prints a key and data pair on stdout. Note
use of the <asterisk> ('*'
) in the format string; this ensures
the correct number of decimal places for the element based on the
number of elements requested.
#include <stdio.h>
...
long i;
char *keystr;
int elementlen, len;
...
while (len < elementlen) {
...
printf("%s Element%0*ld\n", keystr, elementlen, i);
...
}
Creating a Pathname
The following example creates a pathname using information from a
previous getpwnam() function that returned the password database
entry of the user.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
...
char *pathname;
struct passwd *pw;
size_t len;
...
// digits required for pid_t is number of bits times
// log2(10) = approx 10/33
len = strlen(pw->pw_dir) + 1 + 1+(sizeof(pid_t)*80+32)/33 +
sizeof ".out";
pathname = malloc(len);
if (pathname != NULL)
{
snprintf(pathname, len, "%s/%jd.out", pw->pw_dir,
(intmax_t)getpid());
...
}
Reporting an Event
The following example loops until an event has timed out. The
pause() function waits forever unless it receives a signal. The
fprintf() statement should never occur due to the possible return
values of pause().
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
...
while (!event_complete) {
...
if (pause() != -1 || errno != EINTR)
fprintf(stderr, "pause: unknown error: %s\n", strerror(errno));
}
...
Printing Monetary Information
The following example uses strfmon() to convert a number and
store it as a formatted monetary string named convbuf. If the
first number is printed, the program prints the format and the
description; otherwise, it just prints the number.
#include <monetary.h>
#include <stdio.h>
...
struct tblfmt {
char *format;
char *description;
};
struct tblfmt table[] = {
{ "%n", "default formatting" },
{ "%11n", "right align within an 11 character field" },
{ "%#5n", "aligned columns for values up to 99999" },
{ "%=*#5n", "specify a fill character" },
{ "%=0#5n", "fill characters do not use grouping" },
{ "%^#5n", "disable the grouping separator" },
{ "%^#5.0n", "round off to whole units" },
{ "%^#5.4n", "increase the precision" },
{ "%(#5n", "use an alternative pos/neg style" },
{ "%!(#5n", "disable the currency symbol" },
};
...
float input[3];
int i, j;
char convbuf[100];
...
strfmon(convbuf, sizeof(convbuf), table[i].format, input[j]);
if (j == 0) {
printf("%s%s%s\n", table[i].format,
convbuf, table[i].description);
}
else {
printf("%s\n", convbuf);
}
...
Printing Wide Characters
The following example prints a series of wide characters. Suppose
that "L`@`"
expands to three bytes:
wchar_t wz [3] = L"@@"; // Zero-terminated
wchar_t wn [3] = L"@@@"; // Unterminated
fprintf (stdout,"%ls", wz); // Outputs 6 bytes
fprintf (stdout,"%ls", wn); // Undefined because wn has no terminator
fprintf (stdout,"%4ls", wz); // Outputs 3 bytes
fprintf (stdout,"%4ls", wn); // Outputs 3 bytes; no terminator needed
fprintf (stdout,"%9ls", wz); // Outputs 6 bytes
fprintf (stdout,"%9ls", wn); // Outputs 9 bytes; no terminator needed
fprintf (stdout,"%10ls", wz); // Outputs 6 bytes
fprintf (stdout,"%10ls", wn); // Undefined because wn has no terminator
In the last line of the example, after processing three
characters, nine bytes have been output. The fourth character
must then be examined to determine whether it converts to one
byte or more. If it converts to more than one byte, the output is
only nine bytes. Since there is no fourth character in the array,
the behavior is undefined.