fileio/seek_io.c

This is fileio/seek_io.c (Listing 4-3, page 84), an example from the book, The Linux Programming Interface.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

  Cover of The Linux Programming Interface
+/* seek_io.c
+
+   Demonstrate the use of lseek() and file I/O system calls.
+
+   Usage: seek_io file {r<length>|R<length>|w<string>|s<offset>}...
+
+   This program opens the file named on its command line, and then performs
+   the file I/O operations specified by its remaining command-line arguments:
+
+           r<length>    Read 'length' bytes from the file at current
+                        file offset, displaying them as text.
+
+           R<length>    Read 'length' bytes from the file at current
+                        file offset, displaying them in hex.
+
+           w<string>    Write 'string' at current file offset.
+
+           s<offset>    Set the file offset to 'offset'.
+
+   Example:
+
+        seek_io myfile wxyz s1 r2
+*/
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <ctype.h>
 #include "tlpi_hdr.h"
 
 int
 main(int argc, char *argv[])
 {
     size_t len;
     off_t offset;
     int fd, ap, j;
     unsigned char *buf;
     ssize_t numRead, numWritten;
 
     if (argc < 3 || strcmp(argv[1], "--help") == 0)
         usageErr("%s file {r<length>|R<length>|w<string>|s<offset>}...\n",
                  argv[0]);
 
     fd = open(argv[1], O_RDWR | O_CREAT,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
                 S_IROTH | S_IWOTH);                     /* rw-rw-rw- */
     if (fd == -1)
         errExit("open");
 
     for (ap = 2; ap < argc; ap++) {
         switch (argv[ap][0]) {
         case 'r':   /* Display bytes at current offset, as text */
         case 'R':   /* Display bytes at current offset, in hex */
             len = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]);
 
             buf = malloc(len);
             if (buf == NULL)
                 errExit("malloc");
 
             numRead = read(fd, buf, len);
             if (numRead == -1)
                 errExit("read");
 
             if (numRead == 0) {
                 printf("%s: end-of-file\n", argv[ap]);
             } else {
                 printf("%s: ", argv[ap]);
                 for (j = 0; j < numRead; j++) {
                     if (argv[ap][0] == 'r')
                         printf("%c", isprint(buf[j]) ?  buf[j] : '?');
                     else
                         printf("%02x ", buf[j]);
                 }
                 printf("\n");
             }
 
             free(buf);
             break;
 
         case 'w':   /* Write string at current offset */
             numWritten = write(fd, &argv[ap][1], strlen(&argv[ap][1]));
             if (numWritten == -1)
                 errExit("write");
             printf("%s: wrote %ld bytes\n", argv[ap], (long) numWritten);
             break;
 
         case 's':   /* Change file offset */
             offset = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]);
             if (lseek(fd, offset, SEEK_SET) == -1)
                 errExit("lseek");
             printf("%s: seek succeeded\n", argv[ap]);
             break;
 
         default:
             cmdLineErr("Argument must start with [rRws]: %s\n", argv[ap]);
         }
     }
 
     if (close(fd) == -1)
         errExit("close");
 
     exit(EXIT_SUCCESS);
 }

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1