| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <fcntl.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <sys/stat.h> |
| 11 |
#include <sys/types.h> |
| 12 |
#include <time.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
/* Return a character string representing the current date and time. */ |
| 16 |
|
| 17 |
char* get_timestamp () |
| 18 |
{ |
| 19 |
time_t now = time (NULL); |
| 20 |
return asctime (localtime (&now)); |
| 21 |
} |
| 22 |
|
| 23 |
int main (int argc, char* argv[]) |
| 24 |
{ |
| 25 |
/* The file to which to append the timestamp. */ |
| 26 |
char* filename = argv[1]; |
| 27 |
/* Get the current timestamp. */ |
| 28 |
char* timestamp = get_timestamp (); |
| 29 |
/* Open the file for writing. If it exists, append to it; |
| 30 |
otherwise, create a new file. */ |
| 31 |
int fd = open (filename, O_WRONLY | O_CREAT | O_APPEND, 0666); |
| 32 |
/* Compute the length of the timestamp string. */ |
| 33 |
size_t length = strlen (timestamp); |
| 34 |
/* Write the timestamp to the file. */ |
| 35 |
write (fd, timestamp, length); |
| 36 |
/* All done. */ |
| 37 |
close (fd); |
| 38 |
return 0; |
| 39 |
} |