/* ********************************************************************** */ /* * * */ /* * FILE: append.c * */ /* * * */ /* * PURPOSE: This file contains the main routines for program #1.3 * */ /* * in CS590 / Summer 2002. Specifically, this file * */ /* * shows the use of the O_APPEND flag on the open syscall. * */ /* * * */ /* * AUTHOR: Richard Johnson * */ /* * * */ /* * REVS: 05/29/2002 - Program Created. * */ /* * * */ /* ********************************************************************** */ #include #include #include #include #include /* ********************************************************************** */ /* * main - This routine opens the file "file.txt" and writes 10,000 * */ /* * lines of text to it using the O_APPEND flag. * */ /* ********************************************************************** */ int main(int argc, char **argv) { int i; /* Loop counter */ int fd; /* File descriptor */ int count = 0; /* Seek counter */ char buf[64]; /* Write buffer */ /* Check the command line args */ if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } /* Open the file for write, but do not trunc (we are running */ /* concurrent processes). */ if ((fd = open("file.txt", O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { perror("open failed"); exit(1); } /* Write many lines of text to the output file */ for (i = 0; i < 10000; i++) { count++; sprintf(buf, "Process number %s - %d\n", argv[1], count); if (write(fd, buf, strlen(buf)) != strlen(buf)) perror("write failed"); } /* Cleanup */ exit(0); }