/* Lseek -- demonstrate lseek() */

/* Usage: lseek file, seeks to position 1000000 and writes a string */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>		/* Include file with definitions required */
#include <fcntl.h>		/* for opening & seeking on files */
#include <unistd.h>
#include <stdlib.h>


char *string = "The end\n";	/* String to print out */

int outfd;			/* File descriptor of output file */
int i;

int main(argc, argv)
  int argc;			/* Command-line arguments to the process */
  char *argv[];
{

					/* Check we have an argument */
  if (argc != 2) { printf("I need the name of the file!\n"); exit(1); }

  					/* Open the output file */
  outfd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0644);

  if (outfd == -1)			/* Exit if an error */
  { perror("opening file"); exit(1); }

  if (lseek(outfd, 1000000L, SEEK_SET) == -1)	/* Seek 1000000 bytes from
						   start */
  {
    perror("lseek"); exit(1);
  }

  					/* Write out the string */
  write(outfd, string, strlen(string));

					/* and close the file */
  close(outfd); exit(0);
}
