libwget-io(3)

✨ NAME ✨

libwget-io β€” I/O helper routines, straight out of the Library Functions Manual (that's section 3, bestie, the dev-manual one πŸ’…).


🌸 SYNOPSIS 🌸

πŸ¦‹ Functions

ssize_t wget_fdgetline (char **buf, size_t *bufsize, int fd)
ssize_t wget_getline (char **buf, size_t *bufsize, FILE *fp)
int wget_ready_2_transfer (int fd, int timeout, int mode)
int wget_ready_2_read (int fd, int timeout)
int wget_ready_2_write (int fd, int timeout)
char * wget_read_file (const char *fname, size_t *size)
int wget_update_file (const char *fname, wget_update_load_fn *load_func,
    wget_update_save_fn *save_func, void *context)
int wget_truncate (const char *path, off_t length)

πŸ’œ Detailed Description πŸ’œ

Some general I/O helper functions that could be handy for developers. That's it, that's the whole vibe β€” a lil toolbox of file-descriptor and stream besties so you don't have to hand-roll the same tired loops. 🫢


πŸ‘‘ Function Documentation πŸ‘‘

πŸŽ€ ssize_t wget_fdgetline (char ** buf, size_t * bufsize, int fd)

Parameters

  • buf β€” Pointer to a pointer that will be set up by the function to point to the read line
  • bufsize β€” Pointer to a variable where the length of the read line will be put
  • fd β€” File descriptor for an open file

Returns

The length of the last line read or a WGET_E_* error code (< 0)

Behaves identically as wget_getline(), but uses a file descriptor instead of a stream. Same energy, different plumbing, fr fr. ✨


πŸŽ€ ssize_t wget_getline (char ** buf, size_t * bufsize, FILE * fp)

Parameters

  • buf β€” Pointer to a pointer that will be set up by the function to point to the read line
  • bufsize β€” Pointer to a variable where the length of the read line will be put
  • fp β€” Pointer to an open file's stream handle (FILE *)

Returns

The length of the last line read or a WGET_E_* error code (< 0)

This function will read a line from the open file handle fp. It reads input characters until either a newline character (\n) is found or EOF is reached. A block of memory large enough to hold the read line will be implicitly allocated by the function, and its address placed at the pointer pointed to by buf. The length of the aforementioned memory block will be stored in the variable pointed at by bufsize. Not you allocating anything β€” the function handles it. πŸ’…

The caller is not expected to allocate memory as that will be automatically done by wget_getline(), but it is responsibility of the caller to free the memory allocated by a previous invocation of this function. The caller is also responsible for opening and closing the file to read from. So like, the function does the shopping, but you still have to take the trash out. πŸ—‘οΈ

Subsequent calls to wget_getline() that use the same block of memory allocated by previous calls (that is, the caller did not free the buffer returned by a previous call to wget_getline()) will try to reuse as much as possible from the available memory. The block of memory allocated by wget_getline() may be larger than the length of the line read, and might even contain additional lines in it. When wget_getline() returns, the contents of the buffer (pointed at by buf) are guaranteed to start with the first character of the last line read, and such line is also guaranteed to end with a NULL termination character (\0). The length of the last read line will be returned by wget_getline(), whereas the actual length of the buffer will be placed in the variable pointed at by bufsize. The newline character (\n) will not be included in the last read line. Sneaky buffer reuse, but in a slay way. 🌈


πŸŽ€ int wget_ready_2_transfer (int fd, int timeout, int mode)

Parameters

  • fd β€” File descriptor to wait for
  • timeout β€” Max. duration in milliseconds to wait
  • mode β€” Either WGET_IO_WRITABLE or WGET_IO_READABLE

Returns

  • -1 on error
  • 0 on timeout β€” the file descriptor is not ready for reading or writing
  • >0 The file descriptor is ready for reading or writing. Check for the bitwise or of WGET_IO_WRITABLE and WGET_IO_READABLE.

Wait for a file descriptor to become ready to read or write. It's the "u up? πŸ‘€" of file descriptors.

A timeout value of 0 means the function returns immediately. A timeout value of -1 means infinite timeout.


πŸŽ€ int wget_ready_2_read (int fd, int timeout)

Parameters

  • fd β€” File descriptor to wait for
  • timeout β€” Max. duration in milliseconds to wait

Returns

  • -1 on error
  • 0 on timeout β€” the file descriptor is not ready for reading
  • 1 on success β€” the file descriptor is ready for reading

Wait for a file descriptor to become ready to read. Reading only, we're being specific here. 🎯

A timeout value of 0 means the function returns immediately. A timeout value of -1 means infinite timeout.


πŸŽ€ int wget_ready_2_write (int fd, int timeout)

Parameters

  • fd β€” File descriptor to wait for
  • timeout β€” Max. duration in milliseconds to wait

Returns

  • -1 on error
  • 0 on timeout β€” the file descriptor is not ready for writing
  • 1 on success β€” the file descriptor is ready for writing

Wait for a file descriptor to become ready to write. Writing only this time. Iconic, consistent. πŸ‘‘

A timeout value of 0 means the function returns immediately. A timeout value of -1 means infinite timeout.


πŸŽ€ char * wget_read_file (const char * fname, size_t * size)

Parameters

  • fname β€” The name of the file to read from, or a dash (-) to read from STDIN
  • size β€” Pointer to a variable where the length of the contents read will be stored

Returns

Pointer to the read data, as a NULL-terminated C string

Reads the content of a file, or from STDIN. When reading from STDIN, the behavior is the same as for regular files: input is read until an EOF character is found. So - is your little ✨portal✨ to STDIN, no cap.

Memory will be accordingly allocated by wget_read_file() and a pointer to it returned when the read finishes, but the caller is responsible for freeing that memory. The length of the allocated block of memory, which is guaranteed to be the same as the length of the data read, will be placed in the variable pointed at by size. You want it, you free it. That's the deal. 🀝

The read data is guaranteed to be appended a NUL termination character (\0). A lil bow on top. πŸŽ€


πŸŽ€ int wget_update_file (const char * fname, wget_update_load_fn * load_func, wget_update_save_fn * save_func, void * context)

Parameters

  • fname β€” File name to update
  • load_func β€” Pointer to the loader function
  • save_func β€” Pointer to the saver function
  • context β€” Context data

Returns

0 on success, or WGET_E_* on error

This function updates the file named fname atomically. It lets two caller-provided functions do the actual updating. A lock file is created first under /tmp to ensure exclusive access to the file. Other processes attempting to call wget_update_file() with the same fname parameter will block until the current calling process has finished (that is, until wget_update_file() has returned). Everyone queues politely. 🚦

Then, the file is opened with read access first, and the load_func function is called. When it returns, the file is closed and opened again with write access, and the save_func function is called. Both callback functions are passed the context data context, and a stream descriptor for the file. If either function load_func or save_func returns a non-zero value, wget_update_file() closes the file and returns -1, performing no further actions. Bail-out behavior: noted, respected. 🫑


πŸŽ€ int wget_truncate (const char * path, off_t length)

Parameters

  • path β€” File path
  • length β€” New file size

Returns

0 on success, or -1 on error

Set path to a size of exactly length bytes.

If the file was previously larger, the extra data is lost. If the file was previously shorter, extra zero bytes are added.

⚠️ Bestie, hear me: extra data is gone gone. Like, no undo, no recycle bin. Be sure before you call this. πŸ’€

On POSIX, this is a wrapper around ftruncate() (see 'man ftruncate' for details).


🧚 Author 🧚

Generated automatically by Doxygen for wget2 from the source code. Not a human, just a very efficient little robot. πŸ€–βœ¨


And that's the whole page, bestie β€” this was libwget-io(3) from wget2, Version 2.3.0. Slay, go write some I/O. πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from