The Linux Programming Interface

(nextflipdebug5) #1
Directories and Links 347

Listing 18-1: Removing a link with unlink()


––––––––––––––––––––––––––––––––––––––––––––––––––––– dirs_links/t_unlink.c
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"


#define CMD_SIZE 200
#define BUF_SIZE 1024


int
main(int argc, char argv[])
{
int fd, j, numBlocks;
char shellCmd[CMD_SIZE]; /
Command to be passed to system() /
char buf[BUF_SIZE]; /
Random bytes to write to file */


if (argc < 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s temp-file [num-1kB-blocks] \n", argv[0]);


numBlocks = (argc > 2)? getInt(argv[2], GN_GT_0, "num-1kB-blocks")
: 100000;


fd = open(argv[1], O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd == -1)
errExit("open");


if (unlink(argv[1]) == -1) / Remove filename /
errExit("unlink");


for (j = 0; j < numBlocks; j++) / Write lots of junk to file /
if (write(fd, buf, BUF_SIZE) != BUF_SIZE)
fatal("partial/failed write");


snprintf(shellCmd, CMD_SIZE, "df -k dirname %s", argv[1]);
system(shellCmd); / View space used in file system /


if (close(fd) == -1) / File is now destroyed /
errExit("close");
printf("** Closed file descriptor\n");


system(shellCmd); / Review space used in file system /
exit(EXIT_SUCCESS);
}
––––––––––––––––––––––––––––––––––––––––––––––––––––– dirs_links/t_unlink.c


The program in Listing 18-1 accepts two command-line arguments. The first argu-
ment identifies the name of a file that the program should create. The program
opens this file and then immediately unlinks the filename. Although the filename
disappears, the file itself continues to exist. The program then writes random
blocks of data to the file. The number of these blocks is specified in the optional
second command-line argument of the program. At this point, the program
employs the df(1) command to display the amount of space used on the file system.

Free download pdf