Skip to content

Examples and Exercises in Rust from "Advanced Programming in the UNIX Environment"

License

Notifications You must be signed in to change notification settings

jellytech3438/apue-rust

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

283 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

I'm reading through "Advanced Programming in the UNIX Environment". My dislike of C caused me to learn Rust and port some of the examples and exercises from C to Rust.

Beware: this is my first Rust project. This code is far from beautiful and might have mistakes (e.g. dangling pointers). PR are very welcome. All code is tested on OS X and most on it also on Linux.

Progress

  • Chapter 1: Overview
  • Chapter 2: UNIX Standardization and Implementations
  • Chapter 3: File I/O
  • Chapter 4: Files and Directories
  • Chapter 5: Standard I/O Library
  • Chapter 6: System Data Files and Information
  • Chapter 7: Process Environment
  • Chapter 8: Process Control
  • Chapter 9: Process Relationships
  • Chapter 10: Signals
  • Chapter 11: Threads
  • Chapter 12: Thread control

Using this code

TODO:

  • can you copy-paste this code for your project? Mostly, yes, but some examples are probably optimal because they copy data (e.g. buffers) even when unneeded.
  • explain some basic usage cstr!, to_option() and working with buffers allocated via vectors.

Building

  • Building should work out of the box for Macos and Linux on rust nightly.
  • Some of the binaries only do something on Macos, others only for Linux (see #cfg switches in the main methods)
  • If you regularly switch between building MacOs and Linux you can tell cargo to put those files in different directories using export CARGO_TARGET_DIR=target/linux

Code not ported to Rust:

  • Figure 7.9, 7.11, 7.13: setjmp, longjmp: of course Rust solves this with exception handling (i.e. with explicit error handling or using panic::catch_unwind). These sections (as well as the one about malloc, etc.) are actually very good reasons why to not use C directly but instead turn to something safer like Rust. In addition to that: Rust doesn't offer a way to safe unwind the stack after a longjump: https://users.rust-lang.org/t/force-cleanup-before-longjmp/3376
  • Figure 7.14: That's exactly why you take Rust over C because Rust will complain at compile time that you cannot return a stack variable from a function.
  • Figure 8.13: avoid race condition: the synchronization features in Rust don't allow syncing of forks (only available for threads, see e.g. BurntSushi/chan-signal#13), so I skipped this Figure, maybe coming back to this later (when signals are discussed in later chapters)
  • Figure 10.8, 10.9, 10.11, 10.20: rust does not have setjmp/longjmp

About

Examples and Exercises in Rust from "Advanced Programming in the UNIX Environment"

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 97.2%
  • Python 1.8%
  • C 1.0%