SFIO(3ast) SFIO(3ast)

# [NAME](#NAME)

**sfio** - safe/fast string/file input/output

# [SYNOPSIS](#SYNOPSIS)

## [LIBRARIES](#LIBRARIES)

```
#include      <sfio.h>
libsfio.a     -lsfio
libstdio.a    -lstdio
libsfio-mt.a  -lsfio-mt
libstdio-mt.a -lstdio-mt
```

## DATA TYPES

```
Sfoff_t;
Sflong_t;
Sfulong_t;
Sfdouble_t;
Sfio_t;
Sfdisc_t;
ssize_t    (*Sfread_f)(Sfio_t*, void*, size_t, Sfdisc_t*);
ssize_t    (*Sfwrite_f)(Sfio_t*, const void*, size_t, Sfdisc_t*);
Sfoff_t    (*Sfseek_f)(Sfio_t*, Sfoff_t, int, Sfdisc_t*);
int        (*Sfexcept_f)(Sfio_t*, int, void*, Sfdisc_t*);
Sffmt_t;
int        (*Sffmtext_f)(Sfio_t*, void*, Sffmt_t*);
int        (*Sffmtevent_f)(Sfio_t*, int, void*, Sffmt_t*);
SFIO_VERSION
```

## BIT FLAGS

```
SFIO_STRING
SFIO_READ
SFIO_WRITE
SFIO_APPENDWR
SFIO_LINE
SFIO_SHARE
SFIO_PUBLIC
SFIO_MALLOC
SFIO_STATIC
SFIO_IOCHECK
SFIO_WHOLE
SFIO_IOINTR
```

## OPENING/CLOSING STREAMS

```
Sfio_t*    sfnew(Sfio_t* f, void* buf, size_t size, int fd, int flags);
Sfio_t*    sfopen(Sfio_t* f, const char* string, const char* mode);
Sfio_t*    sfpopen(Sfio_t* f, const char* cmd, const char* mode);
Sfio_t*    sfstropen(void);
Sfio_t*    sftmp(size_t size);
int        sfclose(Sfio_t* f);
int        sfstrclose(Sfio_t* f);
```

## INPUT/OUTPUT OPERATIONS

```
int        sfgetc(Sfio_t* f);
int        sfputc(Sfio_t* f, int c);
int        sfnputc(Sfio_t* f, int c, int n);
int        sfungetc(Sfio_t* f, int c);
Sfulong_t  sfgetm(Sfio_t* f, Sfulong_t max);
int        sfputm(Sfio_t* f, Sfulong_t v, Sfulong_t max);
Sfulong_t  sfgetu(Sfio_t* f);
int        sfputu(Sfio_t* f, Sfulong_t v);
Sflong_t   sfgetl(Sfio_t* f);
int        sfputl(Sfio_t* f, Sflong_t v);
Sfdouble_t sfgetd(Sfio_t* f);
int        sfputd(Sfio_t* f, Sfdouble_t v);
char*      sfgetr(Sfio_t* f, int rsc, int type);
ssize_t    sfputr(Sfio_t* f, const char* s, int rsc);
Sfoff_t    sfmove(Sfio_t* fr, Sfio_t* fw, Sfoff_t n, int rsc);
ssize_t    sfread(Sfio_t* f, void* buf, size_t n);
ssize_t    sfwrite(Sfio_t* f, const void* buf, size_t n);
Sfoff_t    sfseek(Sfio_t* f, Sfoff_t offset, int type);
void*      sfreserve(Sfio_t* f, ssize_t n, int type);
```

## DATA FORMATTING

```
int        sfscanf(Sfio_t* f, const char* format, ...);
int        sfsscanf(const char* s, const char* format, ...);
int        sfvsscanf(const char* s, const char* format, va_list args);
int        sfvscanf(Sfio_t* f, const char* format, va_list args);
int        sfprintf(Sfio_t* f, const char* format, ...);
char*      sfprints(const char* format, ...);
char*      sfvprints(const char* format, va_list args);
ssize_t    sfaprints(char** sp, const char* format, ...);
ssize_t    sfvaprints(char** sp, const char* format, va_list args);
int        sfsprintf(char* s, int n, const char* format, ...);
int        sfvsprintf(char* s, int n, const char* format, va_list args);
int        sfvprintf(Sfio_t* f, const char* format, va_list args);
Sffmt_t;
SFFMT_LEFT
SFFMT_SIGN
SFFMT_BLANK
SFFMT_ZERO
SFFMT_THOUSAND
SFFMT_LONG
SFFMT_LLONG
SFFMT_SHORT
SFFMT_LDOUBLE
SFFMT_IFLAG
SFFMT_JFLAG
SFFMT_CENTER
SFFMT_CHOP
SFFMT_ALTER
SFFMT_SKIP
SFFMT_ARGPOS
SFFMT_VALUE
int        (*Sffmtext_f)(Sfio_t* f, void* v, Sffmt_t* fe);
int        (*Sffmtevent_f)(Sfio_t* f, int type, void* v, Sffmt_t* fe);
void       va_copy(va_list to, va_list fr);
long       sffmtversion(Sffmt_t* fe, type);
```

## BUFFERING, SYNCHRONIZATION

```
void*      sfsetbuf(Sfio_t* f, void* buf, size_t size);
int        sfsync(Sfio_t* f);
int        sfpoll(Sfio_t** flist, int n, int timeout);
Sfio_t*    sfpool(Sfio_t* f, Sfio_t* poolf, int mode);
int        sfpurge(Sfio_t* f);
```

## [DISCIPLINE, EVENT HANDLING](#DISCIPLINE,_EVENT_HANDLING)

```
Sfdisc_t*  sfdisc(Sfio_t* f, Sfdisc_t* disc);
int        sfraise(Sfio_t* f, int type, void* data);
ssize_t    sfrd(Sfio_t* f, void* buf, size_t n, Sfdisc_t* disc);
ssize_t    sfwr(Sfio_t* f, const void* buf, size_t n, Sfdisc_t* disc);
Sfoff_t    sfsk(Sfio_t* f, Sfoff_t offset, int type, Sfdisc_t* disc);
SFIO_NEW
SFIO_READ
SFIO_WRITE
SFIO_SEEK
SFIO_CLOSING
SFIO_DPUSH
SFIO_DPOP
SFIO_DPOLL
SFIO_DBUFFER
SFIO_SYNC
SFIO_PURGE
SFIO_FINAL
SFIO_READY
SFIO_LOCKED
SFIO_ATEXIT
SFIO_EVENT
```

## STREAM CONTROL

```
int        sfresize(Sfio_t* f, Sfoff_t size);
int        sfset(Sfio_t* f, int flags, int i);
int        sfsetfd(Sfio_t* f, int fd);
Sfio_t*    sfstack(Sfio_t* base, Sfio_t* top);
char*      sfstruse(Sfio_t *f);
Sfio_t*    sfswap(Sfio_t* f1, Sfio_t* f2);
```

## STREAM INFORMATION

```
Sfoff_t    sfsize(Sfio_t* f);
Sfoff_t    sftell(Sfio_t* f);
ssize_t    sfvalue(Sfio_t* f);
int        sffileno(Sfio_t* f);
int        sfstacked(Sfio_t* f);
int        sfeof(Sfio_t* f);
int        sferror(Sfio_t* f);
int        sfclrerr(Sfio_t* f);
int        sfclrlock(Sfio_t* f);
int        sfnotify(void (*notify)(Sfio_t* f, int type, void* data));
int        sfwalk(Sfwalk_f walkf, void* data, int type);
```

## MISCELLANEOUS FUNCTIONS

```
ssize_t    sfmaxr(ssize_t maxr, int s);
ssize_t    sfslen();
int        sfulen(Sfulong_t v);
int        sfllen(Sflong_t v);
int        sfdlen(Sfdouble_t v);
ssize_t    sfpkrd(int fd, void* buf, size_t n,


                  int rsc, long tm, int action);
```

## FULL STRUCTURE SFIO\_T

```
#include   <sfio_t.h>
#define    SFNEW(buf,size,file,flags,disc)
```

## EXAMPLE DISCIPLINES

```
#include   <sfdisc.h>
int        sfdcdio(Sfio_t* f, size_t bufsize);
int        sfdcdos(Sfio_t* f);
int        sfdcfilter(Sfio_t* f, const char* cmd);
int        sfdcseekable(Sfio_t* f);
int        sfdcslow(Sfio_t* f);
int        sfdcsubstream(Sfio_t* f, Sfio_t* parent,


                         Sfoff_t offset, Sfoff_t extent);
int        sfdctee(Sfio_t* f, Sfio_t* tee);
int        sfdcunion(Sfio_t* f, Sfio_t** array, int n);
int        sfdclzw(Sfio_t* f);
int        sfdcgzip(Sfio_t* f, int flags);
```

## [STDIO-COMPATIBILITY](#STDIO-COMPATIBILITY)

```
#include   <stdio.h>
cc ... -lstdio -lsfio
cc ... -lstdio-mt -lsfio-mt
```

# [DESCRIPTION](#DESCRIPTION)

Sfio provides I/O functions to manage buffered streams. Each Sfio stream is a *file stream*, representing a file (see [**open(2)**](/man/os/ksh/open.2)), or a *string stream*, representing a memory segment. Beyond the usual I/O operations on streams, Sfio provides I/O disciplines for extended data processing, stream stacks for recursive stream processing, and stream pools for automatic data synchronization. Applications can extend the **sfprintf()/sfscanf()** functions to define their own conversion patterns as well as redefine existing ones.

A discipline defines analogues of the system calls **[read(2)](/man/os/ksh/read.2), [write(2)](/man/os/ksh/write.2)** and [**lseek(2)**](/man/os/ksh/lseek.2). Such system calls or their discipline replacements are used to process stream data. Henceforth, \`\`*system call*'' will refer to either a system call or its discipline replacement.

A system call is said to cause an exception if its return value is non-positive. Unless overridden by exception handlers (see **sfdisc()**), an interrupted system call (**errno == EINTR** on UNIX systems) will be automatically reinvoked to continue the ongoing operation.

The buffer of a stream is typically a memory segment allocated via [**malloc(3)**](/man/os/ksh/malloc.3) or supplied by the application. File streams may also use memory mapping ([**mmap(2)**](/man/os/ksh/mmap.2)) if that is more efficient. When memory mapping is used, the underlying file should not be truncated while the stream is active. Memory mapping can be turned off using **sfsetbuf()**.

There are three *standard streams*: **sfstdin** for input (file descriptor **0** on UNIX systems), **sfstdout** for normal output (file descriptor **1**), and **sfstderr** for error output (file descriptor **2**).

This version of Sfio does not support threads.

## DATA TYPES

## [void\*](#void*)

Used to exchange data of unknown types between applications and Sfio.

## [Sfoff\_t](#Sfoff_t)

This defines an integral type suitable to address the largest possible file extent.

## [Sfulong\_t, Sflong\_t, Sfdouble\_t](#Sfulong_t,_Sflong_t,_Sfdouble_t)

These are respectively the largest unsigned integer, signed integer, and floating point value types on the local platform.

## [Sfio\_t](#Sfio_t)

This defines the type of a stream handle.

## [Sfdisc\_t](#Sfdisc_t)

## [ssize\_t (\*Sfread\_f)(Sfio\_t\*, void\*, size\_t, Sfdisc\_t\*)](#ssize_t_%28*Sfread_f%29%28Sfio_t*,_void*,_size_t,_Sfdisc_t*%29)

## [ssize\_t (\*Sfwrite\_f)(Sfio\_t\*, const void\*, size\_t, Sfdisc\_t\*)](#ssize_t_%28*Sfwrite_f%29%28Sfio_t*,_const_void*,_size_t,_Sfdisc_t*%29)

## [Sfoff\_t (\*Sfseek\_f)(Sfio\_t\*, Sfoff\_t, int, Sfdisc\_t\*)](#Sfoff_t_%28*Sfseek_f%29%28Sfio_t*,_Sfoff_t,_int,_Sfdisc_t*%29)

## [int (\*Sfexcept\_f)(Sfio\_t\*, int, void\*, Sfdisc\_t\*)](#int_%28*Sfexcept_f%29%28Sfio_t*,_int,_void*,_Sfdisc_t*%29)

**Sfdisc\_t** defines a stream discipline structure. **Sfread\_f**, **Sfwrite\_f** and **Sfseek\_f** are the types of discipline functions to replace the system calls: [**read(2)**](/man/os/ksh/read.2), [**write(2)**](/man/os/ksh/write.2) and [**lseek(2)**](/man/os/ksh/lseek.2). **Sfexcept\_f** is the type of an event-handling function. See **sfdisc()** for more details.

## [Sffmt\_t](#Sffmt_t)

## [int (\*Sffmtext\_f)(Sfio\_t\*, void\*, Sffmt\_t\*)](#int_%28*Sffmtext_f%29%28Sfio_t*,_void*,_Sffmt_t*%29)

## [int (\*Sffmtevent\_f)(Sfio\_t\*, int, void\*, Sffmt\_t\*)](#int_%28*Sffmtevent_f%29%28Sfio_t*,_int,_void*,_Sffmt_t*%29)

**Sffmt\_t** defines a formatting environment that can be used to extend scanning and printing in the **sfprint()/sfscanf()** functions. **Sffmtext\_f** and **Sffmtevent\_f** define the types of extension functions definable in **Sffmt\_t**. See **Sffmt\_t** below for more details.

## [SFIO\_VERSION](#SFIO_VERSION)

This is a macro value of type **long int** that defines the current version number of Sfio. It is composed of a year, month and day. For example, **20220212L** indicates the version from February 12, 2022.

## BIT FLAGS

A number of bit flags control stream operations. They are set either at stream initialization or by calling **sfset()**. Following are the flags:

[**SFIO\_STRING**:](#SFIO_STRING)

The stream is memory-based.

[**SFIO\_READ**, **SFIO\_WRITE**, **SFIO\_APPENDWR**:](#SFIO_READ)

Flags **SFIO\_READ** and **SFIO\_WRITE** indicate readability and writability. Flag **SFIO\_APPENDWR** asserts that the stream is a file opened in append mode (see [**open(2)**](/man/os/ksh/open.2) and [**fcntl(2)**](/man/os/ksh/fcntl.2)) so that data is always output at the end of file. On systems without direct support for append mode, Sfio uses [**lseek(2)**](/man/os/ksh/lseek.2) or its discipline replacement to approximate this behavior.

[**SFIO\_LINE**:](#SFIO_LINE)

The stream is line-oriented. For a **SFIO\_WRITE** stream, this means that buffered data is flushed whenever a new-line character, **\\n**, is output. For a **SFIO\_READ** stream, **SFIO\_LINE** is only significant during calls to functions in the **sfscanf()** family. **SFIO\_LINE** is set on initialization of any stream representing a terminal device.

[**SFIO\_SHARE**, **SFIO\_PUBLIC**:](#SFIO_SHARE)

Flag **SFIO\_SHARE** means that the underlying file descriptor is shared by independent entities (for example, multiple processes).

For a seekable file stream, **SFIO\_SHARE** means that the logical stream and the physical file positions will be made the same before a system call to perform physical I/O. There are different possibilities. If **SFIO\_PUBLIC** is not set, the physical file position is made equal to the logical stream position. If **SFIO\_PUBLIC** is set, there are two cases. If the physical file position has changed from its last known position, the logical stream position is made equal to the new physical file position. Finally, if the physical file location remains the same as its last known position, the physical file position is made the same as the logical stream position.

For an unseekable stream (e.g., pipes or terminal devices), if possible, **SFIO\_SHARE** means that the block and record I/O operations (**sfread()**, **sfwrite()**, **sfmove()**, **sfgetr()**, **sfputr()**, **sfreserve()**, **sfscanf()** and **sfvprintf()**) will [ensure: (1)](/man/os/ksh/ensure:.1) after each writing operation, the stream is synchronized [and (2)](/man/os/ksh/and.2) each reading operation only reads the requested amount. Note, however, [that (2)](/man/os/ksh/that.2) is not always possible without proper OS facilities such as [**recv(2)**](/man/os/ksh/recv.2) or [**streamio(4)**](/man/os/ksh/streamio.4).

A standard stream that is seekable will be initialized with **SFIO\_SHARE|SFIO\_PUBLIC**.

[**SFIO\_MALLOC**:](#SFIO_MALLOC)

The stream buffer was obtained via [**malloc(3)**](/man/os/ksh/malloc.3) and can be reallocated or freed.

[**SFIO\_STATIC**:](#SFIO_STATIC)

The stream structure should not be freed when closed (**sfclose()**). This flag is used by an applications that allocate their own stream structures. Such applications must use the header file **sfio\_t.h** instead of **sfio.h**.

[**SFIO\_IOCHECK**:](#SFIO_IOCHECK)

If the stream has a discipline exception handler, exceptions will be raised in **sfsync()**, **sfpurge()** or before a system call [**read(2)**](/man/os/ksh/read.2) or [**write(2)**](/man/os/ksh/write.2) (see **sfdisc()**).

[**SFIO\_WHOLE**:](#SFIO_WHOLE)

This flag guarantees that data written in any single **sfwrite()** or **sfputr()** call will always be output as a whole to the output device. This is useful in certain applications (e.g., networking) where a complex object must be output without being split in different system calls. Note that the respective stream still buffers data as much as the buffer can accommodate.

[**SFIO\_IOINTR**:](#SFIO_IOINTR)

This flag indicates that I/O system calls should not be resumed after being interrupted by signals. It is useful for aborting I/O operations on such interruptions. Note, however, that certain operating systems (e.g., BSD Unix systems) may automatically resume interrupted system calls outside the scope of the library. On such systems, **SFIO\_IOINTR** will be ineffective.

## OPENING/CLOSING STREAMS

## [Sfio\_t* sfnew(Sfio\_t* f, void* buf, size\_t size, int fd, int flags)](#Sfio_t*_sfnew%28Sfio_t*_f,_void*_buf,_size_t_size,_int_fd,_int_flags%29)

This function creates or renews a stream. It returns the new stream on success and **NULL** on error.

[**f**:](#f)

If **f** is **NULL**, a new stream is created. Otherwise, **f** is reused. In this case, if **flags** does not have **SFIO\_EOF**, **f** shall be closed via **sfclose()** before being reused. During a stream renewal, buffer, pool and discipline stack are preserved. Note that, except for **SFIO\_STATIC** streams, renewing a stream already closed will result in undefined behavior.

[**buf**, **size**:](#buf)

These determine a buffering scheme. See **sfsetbuf()** for more details.

[**fd**:](#fd)

If **SFIO\_STRING** is specified in **flags**, this is ignored. Otherwise, **fd** is a file descriptor (e.g., from [**open(2)**](/man/os/ksh/open.2)) to use for raw data I/O. Note that Sfio supports unseekable file descriptors opened for both read and write, e.g., sockets.

[**flags**:](#flags)

This is composed from **SFIO\_EOF** and bit values defined in the **BIT FLAGS** section.

## [Sfio\_t* sfopen(Sfio\_t* f, const char* string, const char* mode)](#Sfio_t*_sfopen%28Sfio_t*_f,_const_char*_string,_const_char*_mode%29)

If **string** is **NULL**, **f** is a file stream and **mode** does not imply a string stream, **sfopen()** changes the modes of **f** according to **mode**. In this case, **sfopen()** returns **f** on success and **NULL** on error. This somewhat unusual usage of **sfopen()** is good for resetting certain predefined modes in standard streams including *text/binary* and *append* that are inherited from some parent process. Note also that **SFIO\_READ** and **SFIO\_WRITE** can only be reset if the stream is not yet initialized.

**sfopen()** is normally used to create a new stream or renew a stream. In this case, it returns the new stream on success and **NULL** on error. Below are the meanings of the arguments:

[**f**:](#f~2)

This is treated as in **sfnew()**.

[**string**:](#string)

This is a file name or a string to perform I/O on. See above for when this is **NULL**.

[**mode**:](#mode)

This is composed from the set of letters **{s, r, w, +, a, b, t, x, m, u}**. When conflicting options are present in the same **mode** string, the last one will take effect.

**s** specifies opening a string stream. **string** can be a null-terminated string or **NULL**. Specifying **s** alone is equivalent to specifying **sr**. If **s** is not specified, **string** defines a file name.

**r** and **w** specify read and write modes. Write mode creates and/or truncates the given file to make an empty file. The **+** modifier indicates that the stream is opened for both read and write.

**a** specifies append mode, i.e., data is always output at end of file.

**b** and **t** specify binary and text modes.

**x** specifies exclusive mode, i.e., a file opened for writing should not already exist.

## [Sfio\_t* sfpopen(Sfio\_t* f, const char* cmd, const char* mode)](#Sfio_t*_sfpopen%28Sfio_t*_f,_const_char*_cmd,_const_char*_mode%29)

This function opens a stream that corresponds to the coprocess **cmd**. The argument **mode** should be composed from **r**, **w**, and **+**. The argument **f**, if not **NULL**, is a stream to be renewed (see **sfnew()**). **sfpopen()** returns the new stream or **NULL** on error.

The standard input/output of **cmd** is connected to the application via a pipe if the stream is opened for writing/reading. If the stream is opened for both reading and writing, there will be two different associated file descriptors, one for each type of I/O (note the effect on **sffileno()**).

On opening a coprocess for writing (i.e., **mode** contains **w** or **+**), the signal handler for **SIGPIPE** in the parent application will be set to **SIG\_IGN** if it is **SIG\_DFL** at that time. This protects the parent application from being accidentally killed on writing to a coprocess that closes its reading end. Applications that need to detect such write errors should use disciplines and exception handlers (see **sfdisc()**).

The command **cmd** is executed by an *interpreter* which is either **/bin/sh** or an executable command defined by the environment variable **SHELL**. In either case, the interpreter is invoked with 2 arguments, respectively **-c** and the given command **cmd**. When the interpreter is **/bin/sh** or **/bin/ksh**, **sfpopen()** may execute the command **cmd** itself if there are no shell meta-characters in **cmd**.

## [Sfio\_t* sfstropen(void)](#Sfio_t*_sfstropen%28void%29)

Shorthand that opens a new string buffer for reading and writing; same as **sfnew(NULL,NULL,-1,-1,SFIO\_READ|SFIO\_WRITE|SFIO\_STRING)**. See also **sfstruse()**.

## [Sfio\_t* sftmp(size\_t size)](#Sfio_t*_sftmp%28size_t_size%29)

This function creates a stream for temporary data. It returns the new stream or **NULL** on error.

A stream created by **sftmp()** can be completely or partially memory-resident. If **size** is **SFIO\_UNBOUND**, the stream is a pure string stream. If **size** is zero, the stream is a pure file stream. Otherwise, the stream is first created as a string stream but when its buffer grows larger than **size** or on any attempt to change disciplines, a temporary file is created. Two environment variables, **TMPPATH** and **TMPDIR**, direct where temporary files are created. **TMPPATH**, if defined, specifies a colon-separated set of directories to be used in a round-robin fashion to create files. If **TMPPATH** is undefined, **TMPDIR** can be used to specify a single directory to create files. If neither of **TMPPATH** and **TMPDIR** are defined, **/tmp** is used.

## [int sfclose(Sfio\_t* f)](#int_sfclose%28Sfio_t*_f%29)

This function closes the stream **f** and frees its resources. **SFIO\_STATIC** should be used if the stream space is to be preserved. If **f** is the base of a stream stack (see **sfstack()**), all streams on the stack are closed. If **f** is a **sfpopen**-stream, **sfclose()** waits until the associated command terminates and returns its exit status. **sfclose()** returns **-1** for failure and **0** for success.

**SFIO\_READ|SFIO\_SHARE** and **SFIO\_WRITE** streams are synchronized before closing (see **sfsync()**). If **f** has disciplines, their exception handlers will be called twice. The first exception handler call has the **type** argument as one of **SFIO\_CLOSING** or **SFIO\_NEW** (see **sfdisc()**). The latter, **SFIO\_NEW** is used when a stream is being closed via **sfnew()** so that it can be renewed. The second call uses **type** as **SFIO\_FINAL** and is done after all closing operations have succeeded but before the stream itself is deallocated. In either case, if the exception handler returns a negative value, **sfclose()** will immediately return this value. If the exception handler returns a positive value, **sfclose()** will immediately return a zero value.

## [int sfstrclose(Sfio\_t* f)](#int_sfstrclose%28Sfio_t*_f%29)

Same as **sfclose** but intended for string buffers opened with **sfstropen()**.

## INPUT/OUTPUT OPERATIONS

## [int sfgetc(Sfio\_t* f)](#int_sfgetc%28Sfio_t*_f%29)

## [int sfputc(Sfio\_t* f, int c)](#int_sfputc%28Sfio_t*_f,_int_c%29)

These functions read/write a byte from/to stream **f**. **sfgetc()** returns the byte read or **-1** on error. **sfputc()** returns **c** on success and **-1** on error.

## [ssize\_t sfnputc(Sfio\_t* f, int c, size\_t n)](#ssize_t_sfnputc%28Sfio_t*_f,_int_c,_size_t_n%29)

This function attempts to write the byte **c** to **f** **n** times. It returns the number of bytes actually written or **-1** on failure.

## [int sfungetc(Sfio\_t* f, int c)](#int_sfungetc%28Sfio_t*_f,_int_c%29)

This function pushes the byte **c** back into **f**. If **c** matches the byte immediately before the current position in buffered data, the current position is simply backed up (note the effect on **sftell()** and **sfseek()**). There is no theoretical limit on the number of bytes that can be pushed back into a stream. Pushed back bytes not part of buffered data will be discarded on any operation that implies buffer synchronization. **sfungetc()** returns **c** on success and **-1** on failure.

## [Sfulong\_t sfgetm(Sfio\_t* f, Sfulong\_t max)](#Sfulong_t_sfgetm%28Sfio_t*_f,_Sfulong_t_max%29)

## [int sfputm(Sfio\_t* f, Sfulong\_t v, Sfulong\_t max)](#int_sfputm%28Sfio_t*_f,_Sfulong_t_v,_Sfulong_t_max%29)

These functions read and write **Sfulong\_t** values encoded in a portable format given that the values are at most **max**. Portability across a write architecture and a read architecture requires that the bit order in a byte is the same on both architectures and the written value is storable in an **Sfulong\_t** on the read architecture. **sfgetm()** returns the value read or **-1** on error. **sfputm()** returns the number of bytes written or **-1** on error.

## [Sfulong\_t sfgetu(Sfio\_t* f)](#Sfulong_t_sfgetu%28Sfio_t*_f%29)

## [int sfputu(Sfio\_t* f, Sfulong\_t v)](#int_sfputu%28Sfio_t*_f,_Sfulong_t_v%29)

These functions read and write **Sfulong\_t** values in a compact variable-length portable format. Portability across a write architecture and a read architecture requires that the bit order in a byte is the same on both architectures and the written value is storable in an **Sfulong\_t** on the read architecture. **sfgetu()** returns the value read or **-1** on error. **sfputu()** returns the number of bytes written or **-1** on error. See also **sfulen()**.

## [Sflong\_t sfgetl(Sfio\_t* f)](#Sflong_t_sfgetl%28Sfio_t*_f%29)

## [int sfputl(Sfio\_t* f, Sflong\_t v)](#int_sfputl%28Sfio_t*_f,_Sflong_t_v%29)

These functions are similar to **sfgetu()** and **sfputu()** but for reading and writing (signed) **Sflong\_t** values. See also **sfllen()**.

## [Sfdouble\_t sfgetd(Sfio\_t* f)](#Sfdouble_t_sfgetd%28Sfio_t*_f%29)

## [int sfputd(Sfio\_t* f, Sfdouble\_t v)](#int_sfputd%28Sfio_t*_f,_Sfdouble_t_v%29)

These functions read and write **Sfdouble\_t** values. In this case, portability depends on the input and output architectures having the same floating point value representation. Values are coded and decoded using [**ldexp(3)**](/man/os/ksh/ldexp.3) and [**frexp(3)**](/man/os/ksh/frexp.3) so they are constrained to the sizes supported by these functions. See also **sfdlen()**.

## [char* sfgetr(Sfio\_t* f, int rsc, int type)](#char*_sfgetr%28Sfio_t*_f,_int_rsc,_int_type%29)

This function reads a record of data ending in the record separator **rsc**. After **sfgetr()** returns, the length of the record even if it is incomplete can be retrieved with **sfvalue()**. **sfgetr()** returns the record on success and **NULL** on error. See also **sfmaxr()** for limiting the amount of data read to construct a record.

The **type** argument is composed of some subset of the below bit flags:

[**SFIO\_STRING**:](#SFIO_STRING~2)

A null byte will replace the record separator to make the record into a C string. Otherwise, the record separator is left alone.

[**SFIO\_LOCKR**:](#SFIO_LOCKR)

Upon successfully obtaining a record **r**, the stream will be locked from further access until it is released with a call **sfread(f,r,0)**.

[**SFIO\_LASTR**:](#SFIO_LASTR)

This should be used only after a failed **sfgetr()** to retrieve the last incomplete record. In this case, **rsc** is ignored.

## [ssize\_t sfputr(Sfio\_t* f, const char* s, int rsc)](#ssize_t_sfputr%28Sfio_t*_f,_const_char*_s,_int_rsc%29)

This function writes the null-terminated string **s** to **f**. If **rsc** is non-negative, **(unsigned char)rsc** is output after the string. **sfputr()** returns the number of bytes written or **-1** on failure.

## [Sfoff\_t sfmove(Sfio\_t* fr, Sfio\_t* fw, Sfoff\_t n, int rsc)](#Sfoff_t_sfmove%28Sfio_t*_fr,_Sfio_t*_fw,_Sfoff_t_n,_int_rsc%29)

This function moves objects from input stream **fr** to output stream **fw**. **sfmove()** returns the number of objects moved or **-1** on failure.

An object can be either a byte if the record separator argument **rsc** is negative or a record of **rsc** is non-negative. In the latter case, a record is incomplete if it does not end in **rsc**. Generally speaking, a stream can have at most one incomplete record. If **n** is negative, all complete objects of **fr** will be moved. Otherwise, **n** indicates the number of objects to move. If either **fr** or **fw** is **NULL**, it acts as if it is a stream corresponding to **/dev/null**, the UNIX device that has no read data and throws away any write data. For example, the call **sfmove(f,NULL,(Sfoff\_t)(-1),'\\n')** counts the number of complete lines in stream **f**.

## [ssize\_t sfread(Sfio\_t* f, void* buf, size\_t n)](#ssize_t_sfread%28Sfio_t*_f,_void*_buf,_size_t_n%29)

This function reads up to **n** bytes from **f** into buffer **buf**. It returns the number of bytes actually read or **-1** on error.

## [ssize\_t sfwrite(Sfio\_t* f, const void* buf, size\_t n)](#ssize_t_sfwrite%28Sfio_t*_f,_const_void*_buf,_size_t_n%29)

This function writes **n** bytes from **buf** to **f**. If **f** is **SFIO\_STRING**, and the buffer is not large enough, an **SFIO\_WRITE** exception shall be raised. **sfwrite()** returns the number of bytes written or **-1** on failure.

## [Sfoff\_t sfseek(Sfio\_t* f, Sfoff\_t offset, int type)](#Sfoff_t_sfseek%28Sfio_t*_f,_Sfoff_t_offset,_int_type%29)

This function sets a new I/O position for **f**. It returns the new position or **-1** on failure.

If the stream is a **SFIO\_STRING** stream and the new address is beyond the current buffer extent, an **SFIO\_SEEK** exception will be raised (see **sfdisc()**).

The new position is determined based on **offset** and **type** which is composed from the bit flags:

**0** or **SEEK\_SET**:

**offset** is the desired position.

**1** or **SEEK\_CUR**:

**offset** is relative to the current position (see **SFIO\_PUBLIC** below).

**2** or **SEEK\_END**:

**offset** is relative to the physical end of file.

[**SFIO\_SHARE**:](#SFIO_SHARE~2)

The stream is treated as if it has the control bit **SFIO\_SHARE** on. This implies that a system call seek will be done to ensure that the location seeking to is valid.

[**SFIO\_PUBLIC**:](#SFIO_PUBLIC)

The stream is treated as if it has the control bit **SFIO\_PUBLIC** on. If the physical file position has changed from its last known location, the current position is taken as the new physical position. Otherwise, the current position is the logical stream position.

## [void* sfreserve(Sfio\_t* f, ssize\_t n, int type)](#void*_sfreserve%28Sfio_t*_f,_ssize_t_n,_int_type%29)

This function reserves a data block from the stream **f**. It returns the reserved data block on success and **NULL** on failure.

If **f** is a **SFIO\_READ** stream, the data block is a segment of input data. If **f** is a **SFIO\_WRITE** stream, the data block is a buffer suitable for writing output data. For consistency, if **f** is opened with **SFIO\_READ|SFIO\_WRITE**, it will normally be treated as if it is a **SFIO\_READ** stream (see **sfset()** for forcing a particular mode) but the returned buffer can also be written into (more below). However, it is possible to bias to **SFIO\_WRITE** when the **type** argument is non-negative by adding the **SFIO\_WRITE** bit **type**. In any case, a reserved data block is guaranteed to be valid only until a future access to the stream **f**.

When **f** is **SFIO\_READ**, **SFIO\_SHARE** and unseekable, **sfreserve()** will attempt to peek at input data without consuming it. This enables separate processes to share in reading input from unseekable file descriptors (e.g., pipes or devices). However, this use of **sfreserve()** may fail on certain platforms that do not properly support peeking on unseekable file descriptors.

After a **sfreserve()** call, whether or not it succeeds, **sfvalue(f)** gives the size of the available data block. Any partially reserved data block after a failed **sfreserve()** call can be obtained in another **sfreserve()** call with the argument **type** being **SFIO\_LASTR**. The second argument **n** to **sfreserve()** will be ignored in this case.

A **sfreserve()** call is successful if it can obtain a data block of size at least the absolute value of **n**. For a **SFIO\_READ** atream, the argument **n** is treated as follows:

[**n &lt; 0**:](#n)

**sfreserve()** attempts to get *at least* **|n|** bytes into the buffer.

[**n == 0**:](#n~2)

If the argument **type** is **0**, **sfreserve()** attempts to get *at least* **1** byte into the buffer but does not consume it (as consistent with **n == 0**). If **type != 0**, no attempt will be made to read data into the buffer. For example, the call **sfreserve(f, 0, -1)** only returns the buffer status, i.e., size of existing buffered data and pointer to such data, if any. The call **sfreserve(f, 0, SFIO\_LOCKR)** is similar but also locks the stream.

[**n &gt; 0**:](#n~3)

**sfreserve()** will use attempt to get *at most* **n** bytes into the buffer. Further, if **type == 3SFIO\_LOCKR** (see below), read attempts end on a positive amount.

For a successful reservation, the argument **type** dictates treatment as follows:

[**type == SFIO\_LASTR**:](#type)

After a **sfreserve()** call with **type != SFIO\_LOCKR** fails, there may be some left over data not accessible via conventional Sfio calls. Immediately after such a failed call, another call to **sfreserve** with **type == SFIO\_LASTR** will return any left over data and also advance the stream I/O position by the amount of returned data.

[**type &lt; 0**:](#type~2)

If **n &gt; 0**, the stream I/O position is advanced by **n**. If **n &lt; 0**, the stream I/O position is advanced by the amount of available data. For example, a successful **sfreserve(f, -1, -1)** call will return a buffer of data and simultaneously advance the stream I/O position by the amount indicated by **sfvalue(f)**.

[**type == SFIO\_LOCKR**:](#type~3)

The stream I/O position remains unchanged. In addition, **f** will be locked from further access. As appropriate to the stream type (**SFIO\_READ**, **SFIO\_WRITE** or both), **f** can be unlocked later with one of **sfread(f,rsrv,size)** or **sfwrite(f,rsrv,size)** where **rsrv** is the reserved data block and **size** is the amount of data to be consumed. For example, if **f** is a locked **SFIO\_READ** stream, the call **sfread(f,rsrv,1)** will reopen the stream and simultaneously advance the stream I/O position by **1**. Finally, a stream opened for both reading and writing can release the lock with either call (with associated operational semantics!) For example, the below code reads 10 bytes of data from a stream opened with both **SFIO\_READ** and **SFIO\_WRITE**, modifies the data in place, then rewrites the new data back to the stream:

```


    rsrv = sfreserve(f, 10, 1);


    for(i = 0; i < 10; ++i)


        rsrv[i] = toupper(rsrv[i]);


    sfwrite(f, rsrv, 10);
    
```

## DATA FORMATTING

Data printing and scanning are done via the **sfprintf()** and **sfscanf()** family of functions. These functions are similar to their ANSI C **fprintf()** and **fscanf()** counterparts. However, the Sfio versions have been extended for both portability and generality. In particular, a notion of a formatting environment stack is introduced. Each formatting element on the stack defines a separate *formatting pair* of a format specification string, **char* format** (the usual second argument in the formatting functions), and an argument list, **va\_list args** (the third argument in functions **sfvprintf()** and **sfvscanf()**). A formatting environment element may also specify extension functions to obtain or assign arguments and to provide new semantics for pattern processing. To simplify the description below, whenever we talk about an argument list, unless noted otherwise, it is understood that this means either the true argument list when there is no extension function or the action to be taken by such a function in processing arguments. The manipulation of the formatting environment stack is done via the pattern **!** discussed below.

## [%! and Sffmt\_t](#_!_and_Sffmt_t)

The pattern **%!** manipulates the formatting environment stack [to (1)](/man/os/ksh/to.1) change the top environment to a new environment, (2) stack a new environment on top of the current top, [or (3)](/man/os/ksh/or.3) pop the top environment. The bottom of the environment stack always contains a virtual environment with the original formatting pair and without any extension functions.

The top environment of a stack, say **fe**, is automatically popped whenever its format string is completely processed. In this case, its event-handling function (if any) is called as **(\*eventf)(f,SFIO\_FINAL,NULL,fe)**. The top environment can also be popped by giving an argument **NULL** to **%!** or by returning a negative value in an extension function. In these cases, the event-handling function is called as **(\*eventf)(f,SFIO\_DPOP,form,fe)** where **form** is the remainder of the format string. A negative return value from the event handling function will prevent the environment from being popped.

A formatting environment is a structure of type **Sffmt\_t** which contains the following elements:

```


    Sffmtext_f   extf;   /* extension processor        */


    Sffmtevent_f eventf; /* event handler              */


    char*        form;   /* format string to stack     */


    va_list      args;   /* corresponding arg list     */


    int          fmt;    /* pattern being processed    */


    ssize_t      size;   /* object size                */


    int          flags;  /* formatting control flags   */


    int          width;  /* width of field             */


    int          precis; /* precision required         */


    int          base;   /* conversion base            */


    char*        t_str;  /* extfdata string            */


    ssize_t      n_str;  /* length of t_str            */
```

The first four elements of **Sffmt\_t** must be defined by the application before the structure is passed to a formatting function. The two function fields should not be changed during processing. Other elements of **Sffmt\_t** are set by the respective formatting function before it calls the extension function **Sffmt\_t.extf** and, subsequently, can be modified by this function to redirect formatting or scanning. For example, consider a call from a **sfprintf()** function to process an unknown pattern **%t** (which we may take to mean \`\`time'') based on a formatting environment **fe**. **fe-&gt;extf** may reset **fe-&gt;fmt** to \`**d**' upon returning to cause **sfprintf()** to process the value being formatted as an integer.

Below are the fields of **Sffmt\_t**:

[**extf**:](#extf)

**extf** is a function to extend scanning and formatting patterns. Its usage is discussed below.

[**eventf**:](#eventf)

This is a function to process events as discussed earlier.

[**form** and **args**:](#form)

This is the formatting pair of a specification string and corresponding argument list. When an environment **fe** is being inserted into the stack, if **fe-&gt;form** is **NULL**, the top environment is changed to **fe** and its associated extension functions but processing of the current formatting pair continues. On the other hand, if **fe-&gt;form** is not **NULL**, the new environment is pushed onto the stack so that pattern processing will start with the new formatting pair as well as any associated extension functions. During processing, whenever **extf** is called, **form** and **args** will be set to the current values of the formatting pair in use.

[**fmt**:](#fmt)

This is set to the pattern being processed or one of '.', 'I', '('.

[**size**:](#size)

This is the size of the object being processed.

[**flags**:](#flags~2)

This is a collection of bits defining the formatting flags specified for the pattern. The bits are:

**SFFMT\_LEFT**: Flag **-** in **sfprintf()**.

**SFFMT\_SIGN**: Flag **+** in **sfprintf()**.

**SFFMT\_BLANK**: Flag *space* in **sfprintf()**.

**SFFMT\_ZERO**: Flag **0** in **sfprintf()**.

**SFFMT\_THOUSAND**: Flag **'** in **sfprintf()**.

**SFFMT\_LONG**: Flag **l** in **sfprintf()** and **sfscanf()**.

**SFFMT\_LLONG**: Flag **ll** in **sfprintf()** and **sfscanf()**.

**SFFMT\_SHORT**: Flag **h** in **sfprintf()** and **sfscanf()**.

**SFFMT\_LDOUBLE**: Flag **L** in **sfprintf()** and **sfscanf()**.

**SFFMT\_IFLAG**: flag **I** in **sfprintf()** and **sfscanf()**.

**SFFMT\_JFLAG**: flag **j** in **sfprintf()** and **sfscanf()**.

**SFFMT\_CENTER**: flag **=** in **sfprintf()** and **sfscanf()**.

**SFFMT\_CHOP**: flag **-** in *precis* in **sfprintf()** and **sfscanf()**.

**SFFMT\_ALTER**: Flag **#** in **sfprintf()** and **sfscanf()**.

**SFFMT\_SKIP**: Flag **\*** in **sfscanf()**.

**SFFMT\_ARGPOS**: This indicates argument processing for **pos$**.

**SFFMT\_VALUE**: This is set by **fe-&gt;extf** to indicate that it is returning a value to be formatted or the address of an object to be assigned.

[**width**:](#width)

This is the field width.

[**precis**:](#precis)

This is the precision.

[**base**:](#base)

This is the conversion base.

[**t\_str** and **n\_str**:](#t_str)

This is the type string and its size.

## [int (\*Sffmtext\_f)(Sfio\_t* f, void* v, Sffmt\_t* fe)](#int_%28*Sffmtext_f%29%28Sfio_t*_f,_void*_v,_Sffmt_t*_fe%29)

This is the type of the extension function **fe-&gt;extf** to process patterns and arguments. Arguments are always processed in order and **fe-&gt;extf** is called exactly once per argument. Note that, when **pos$** (below) is not used anywhere in a format string, each argument is used exactly once per a corresponding pattern. In that case, **fe-&gt;extf** is called as soon as the pattern is recognized and before any scanning or formatting. On the other hand, when **pos$** is used in a format string, an argument may be used multiple times. In this case, all arguments shall be processed in order by calling **fe-&gt;extf** exactly once per argument before any pattern processing. This case is signified by the flag **SFFMT\_ARGPOS** in **fe-&gt;flags**.

In addition to the predefined formatting patterns and other application-defined patterns, **fe-&gt;extf** may be called with **fe-&gt;fmt** being one of \`**(**' (left parenthesis), \`**.**' (dot), and \`**I**'.

The left parenthesis requests a string to be used as the **extfdata** string discussed below. In this case, upon returning, **fe-&gt;extf** should set the **fe-&gt;size** field to be the length of the string or a negative value to indicate a null-terminated string.

The \`**I**' requests an integer to define the object size.

The dot requests an integer for width, precision, base, or a separator. In this case, the **fe-&gt;size** field will indicate how many dots have appeared in the pattern specification. Note that, if the actual conversion pattern is 'c' or 's', the value **\*form** will be one of these characters.

[**f**:](#f~3)

This is the input/output stream in the calling formatting function. During a call to **fe-&gt;extf**, the stream shall be unlocked so that **fe-&gt;extf** can read from or write to it as appropriate.

[**v**:](#v)

For both **sfscanf()** and **sfprintf()** functions, **v** points to a location suitable for storing any scalars or pointers. On return, **fe-&gt;extf** treats **v** as discussed below.

[**fe**:](#fe)

This is the current formatting environment.

The return value **rv** of **fe-&gt;extf** directs further processing. There are two cases. When **pos$** is present, a negative return value means to ignore **fe** in further argument processing while a non-negative return value is treated as the case **rv == 0** below. When **pos$** is not present, **fe-&gt;extf** is called per argument immediately before pattern processing and its return values are treated as below:

[**rv &lt; 0:**](#rv)

The environment stack is immediately popped.

[**rv == 0:**](#rv~2)

The extension function has not consumed (in a scanning case) or output (in a printing case) data out of or into the given stream **f**. The fields **fmt**, **flags**, **size**, **width**, **precis** and **base** of **fe** shall direct further processing.

For **sfprintf()** functions, if **fe-&gt;flags** has the bit **SFFMT\_VALUE**, **fe-&gt;extf** should have set **\*v** to the value to be processed; otherwise, a value should be obtained from the argument list. Likewise, for **sfscanf()** functions, **SFFMT\_VALUE** means that **\*v** should have a suitable address; otherwise, an address to assign value should be obtained from the argument list.

When **pos$** is present, if **fe-&gt;extf** changes **fe-&gt;fmt**, this pattern shall be used regardless of the pattern defined in the format string. On the other hand, if **fe-&gt;fmt** is unchanged by **fe-&gt;extf**, the pattern in the format string is used. In any case, the effective pattern should be one of the standardly defined pattern. Otherwise, it shall be treated as unmatched.

[**rv &gt; 0:**](#rv~3)

The extension function has accessed the stream **f** to the extent of **rv** bytes. Processing of the current pattern ceases except that, for scanning functions, if **fe-&gt;flags** does not contain the bit **SFFMT\_SKIP**, the assignment count shall increase by 1.

## [void va\_copy(va\_list to, va\_list fr)](#void_va_copy%28va_list_to,_va_list_fr%29)

This macro function portably copies the argument list **fr** to the argument list **to**. It should be used to set the field **Sffmt\_t.args**.

## [long sffmtversion(Sffmt\_t* fe, int type)](#long_sffmtversion%28Sffmt_t*_fe,_int_type%29)

This macro function initializes the formatting environment **fe** with a version number if **type** is non-zero. Otherwise, it returns the current value of the version number of **fe**. This is useful for applications to find out when the format of the structure **Sffmt\_t** changes. Note that the version number corresponds to the Sfio version number which is defined in the macro value **SFIO\_VERSION**.

## [int sfprintf(Sfio\_t* f, const char* format, ...);](#int_sfprintf%28Sfio_t*_f,_const_char*_format,_...%29;)

## [char* sfprints(const char* format, ...);](#char*_sfprints%28const_char*_format,_...%29;)

## [char* sfvprints(const char* format, va\_list args);](#char*_sfvprints%28const_char*_format,_va_list_args%29;)

## [ssize\_t sfaprints(char\** sp, const char* format, ...);](#ssize_t_sfaprints%28char**_sp,_const_char*_format,_...%29;)

## [ssize\_t sfvaprints(char\** sp, const char* format, va\_list args);](#ssize_t_sfvaprints%28char**_sp,_const_char*_format,_va_list_args%29;)

## [int sfsprintf(char* s, int n, const char* format, ...)](#int_sfsprintf%28char*_s,_int_n,_const_char*_format,_...%29)

## [int sfvsprintf(char* s, int n, const char* format, va\_list args);](#int_sfvsprintf%28char*_s,_int_n,_const_char*_format,_va_list_args%29;)

## [int sfvprintf(Sfio\_t* f, const char* format, va\_list args);](#int_sfvprintf%28Sfio_t*_f,_const_char*_format,_va_list_args%29;)

These functions format output data. **sfprintf()** and **sfvprintf()** write to output stream **f**. **sfsprintf()** and **sfvsprintf()** write to buffer **s** which is of size **n**. **sfprints()** and **sfvprints()** construct data in some Sfio-defined buffer. **sfaprints()** and **sfvaprints()** are similar to **sfprints()** and **sfvprints()** but they return a string constructed via **malloc()** in **\*sp** and expect this string to be freed by the caller when no longer needed. **sfvprintf()** is the underlying primitive for the other functions. Except for **sfprints()** and **sfvprints()** which return a null-terminated string or **NULL**, other functions return the number of output bytes or **-1** on failure.

The length of string constructed by **sfprints()**, **sfsprintf()**, or **sfvsprintf()** can be retrieved by **sfslen()**.

The standard patterns are: **n, s, c, %, h, i, d, p, u, o, x, X, g, G, e, E, f** and **!**. Except for **!** (which is described under **%! and Sffmt\_t** above), see the ANSI C specification of [**fprintf(3)**](/man/os/ksh/fprintf.3) for details on the other patterns. Let **z** be some pattern type. A formatting pattern is defined as below:

```


    %[pos$][flag][width][.precision[.base]][(extfdata)]z
```

[**pos$**:](#pos$)

A pattern can specify which argument in the argument list to use. This is done via **pos$** where **pos** is the argument position. Arguments are numbered so that the first argument after **format** is at position 1. If **pos** is not specified, the argument following the most recently used one will be used. The pattern **%!** (see **%! and Sffmt\_t** above) cannot be used subsequent to a usage of **pos$**. Doing so may cause unexpected behaviors.

[**flag**:](#flag)

The flag characters are **h**, **hh**, **l**, **ll**, **L**, **I**, **j**, **t**, **z**, **-**, **+**, *space*, **0**, **'**, **=** and **#**.

Flag **I** defines the size or type of the object being formatted. There are two [cases: (1)](/man/os/ksh/cases:.1) **I** by itself [and (2)](/man/os/ksh/and.2) **I** followed by either a decimal number or \`\*'.

In the first case, for integer and floating point patterns, the object type is taken to be the largest appropriate type (i.e., one of **Sflong\_t**, **Sfulong\_t** or **Sfdouble\_t**). For conversion specifiers **s** and **c**, the flag is ignored.

In the second case, a given decimal value would define a size while \`\*' would cause the size to be obtained from the argument list. Then, if the conversion specifier is **s**, this size defines the length of the string or strings being formatted (see the discussion of **base** below). For integer and floating point patterns, the size is used to select a type from one of the below lists as indicated by the conversion specifier:

```


    Sflong_t, long, int, short


    Sfulong_t, unsigned long, unsigned int, unsigned short


    Sfdouble_t, double, float
    
```

The selection algorithm always matches types from left to right in any given list. Although selection is generally based on sizes in bytes, for compatibility with Microsoft-C, the size 64 is matched with an appropriate type with the same number of bits, if any. If the given size does not match any of the listed types, it shall match one of **int**, **unsigned int**, and **double** as defined by the formatting pattern.

Below are a few examples of using the **I** flag. The first example prints an **Sflong\_t** integer. This example is actually not portable and only works on platforms where **sizeof(Sflong\_t)** is 8. The second example shows how to that portably. The third example specifies printing a string of length 16. This length shall be used regardless of whether or not the given string is shorter or longer than 16. The last example shows the use of the pattern **%n** to assign the amount of data already output into a **short** integer **n\_output**.

```


    sfprintf(sfstdout,"%I8d", Sflong_obj);


    sfprintf(sfstdout,"%I*d", sizeof(Sflong_obj), Sflong_obj);


    sfprintf(sfstdout,"%I*s", 16, s);


    sfprintf(sfstdout,"%d%I*n", 1001, sizeof(short), &n_output);
    
```

Flags **h**, **l**, **j** and **L** are the ANSI C conventions to select the types of input objects. For example, **%hd** indicates a **short int**, while **%ld** indicates a **long int**.

Flag **hh** addresses the byte value types, i.e., **char** and **unsigned char**.

Flags **z**, **t** and **j** address respectively the types **size\_t**, **ptrdiff\_t** and **Sflong\_t**.

Flags **ll** and **L** address respectively the largest integer and floating value types, i.e., **Sfulong\_t**, **Sflong\_t**, and **Sfdouble\_t**.

Flag **-** left-justifies data within the field (otherwise, right-justification).

Flag **+** means that a signed conversion will always begin with a plus or minus sign.

Flag *space* is ignored if **+** is specified; otherwise, it means that if the first character of a signed conversion is not a sign or if the result is empty, a space will be prepended.

Flag **0** means padding with zeros on the left.

Flag **'** outputs thousands-separator used by the current locale. [**setlocale(3)**](/man/os/ksh/setlocale.3) should have been used to set the desired locale.

Flag **=** centers data within the field.

Flag **#** indicates an alternative format processing. For **%o**, the first digit is always a zero. For **%x** and **%X**, a non-zero result will have a prefix **0x** or **0X**. For **%e**, **%E**, **%f**, **%g**, and **%G**, the result always contains a decimal point. For **%g** and **%G**, trailing zeros will not be removed. For **%d**, **%i** and **%u**, the form is *base#number* where *base* is the conversion base and *number* is represented by digits for this *base*. For example, a base **2** conversion with **%#..2d** for **10** is **2#1010** instead of **1010** as printed with **%..2d**. Finally, for **%c**, bytes will be printed in the C format. For example, when the ASCII character set is used, the byte value 10 will be printed as **\\n** while 255 is printed as **\\377**.

[**width**:](#width~2)

This defines the width of the printing field. A value to be printed will be justified and padded if necessary to fill out the field width.

[**precis**:](#precis~2)

After a first dot appears, an integral value defines a precision. For floating point value patterns, precision is the number of precision digits. For **%c**, precision defines the number of times to repeat the character being formatted. For **%s**, precision defines the maximum number of characters to output; -**precis** also defines the maximum number of characters to output, but retains the rightmost **precis** characters.

[**base**:](#base~2)

This is defined after exactly two dots have appeared.

For **%i**, **%d**, and **%u**, **base** should be an integer value in the inclusive range **\[2,64]** and defines a conversion base. If **base** is not in this range, it is defined to be **10**. The digits to represent numbers are:

```


    01234567890


    abcdefghijklmnopqrstuvwxyz


    ABCDEFGHIJKLMNOPQRSTUVWXYZ @_
    
```

For **%s** and **%c**, **base** defines a separator. Then, for **%s**, the input argument is taken to be a NULL-terminated array of strings while, for **%c**, this is a null-terminated array of characters. The strings or characters will be formatted one of a time based on the usual width and precision rules. After each formatted string or character, except for the last one, the separator **base** is output if it is a non-zero.

There are further restrictions on the syntax of **%s** and **%c** when a separator is defined. Below are the legitimate sequences for **%s** and **%c** after the second dot:

```
    s            c
    *s           *c
    zs           zc
```

In the first case, no separator is defined so **base** is set to zero. In the second case, **base** is obtained from the argument list. In the third case, the character *z* must be non-alphanumeric and **base** will be set to this character.

The below example shows both the call and the result of printing a **NULL**-terminated array of three strings **apple**, **orange**, and **grape**:

```


    sfprintf(sfstdout,"|%8..:s|",list);


    |   apple:  orange:   grape|
    
```

**(extfdata)**:

This defines a string **extfdata** to be passed to the extension function **Sffmt\_t.extf**. Parentheses shall be balanced. If **extfdata** is **\***, the string is obtained from the argument list.

## [int sfscanf(Sfio\_t* f, const char* format, ...)](#int_sfscanf%28Sfio_t*_f,_const_char*_format,_...%29)

## [int sfsscanf(const char* s, const char* format, ...)](#int_sfsscanf%28const_char*_s,_const_char*_format,_...%29)

## [int sfvsscanf(const char* s, const char* format, va\_list args)](#int_sfvsscanf%28const_char*_s,_const_char*_format,_va_list_args%29)

## [int sfvscanf(Sfio\_t* f, const char* format, va\_list args)](#int_sfvscanf%28Sfio_t*_f,_const_char*_format,_va_list_args%29)

These functions scan data items. **sfscanf()** scans from the input stream **f** while **sfsscanf()** and **sfvsscanf()** scan from the null-terminated string **s**. **sfvscanf()** is the underlying primitive that performs the actual scanning. Item types are determined from patterns in string **format**. These functions return the number of items successfully scanned or **-1** on error.

A white space character (blank, tab, or new-line) in **format** normally matches a maximal sequence of input white space characters. However, if the input stream is in **SFIO\_LINE** mode (see **sfset()**), a new-line character only matches white spaces up to an input new-line character. This is useful to avoid blocking when scanning typed inputs.

The standard scan patterns are: **i, d, u, o, x, X, p, n, f, e, E, g, G, c, %, s, \[]** and **!**. Except for **!** (which is described under **%! and Sffmt\_t** above), see the ANSI C specification of [**fscanf(3)**](/man/os/ksh/fscanf.3) for details on other patterns. Let **z** be some pattern type. A formatting pattern is specified as below:

```


    %[*][pos$][width][.width.base][(extfdata)][flag]z
```

[**pos$**:](#pos$~2)

A pattern can specify which argument in the argument list to use. This is done via **pos$** where **pos** is the argument position. Arguments are numbered so that the first argument after **format** is at position 1. If **pos** is not specified, the argument following the most recently used one will be used. The pattern **%!** (see **%! and Sffmt\_t** above) cannot be used subsequent to a usage of **pos$**.

**\*:**

This discards the corresponding scanned item.

[**width** and **base**:](#width~3)

**width** defines the maximum number of bytes to scan and **base** defines the base of an integral value being scanned. The \`.' (dot) notation also allows specifying a \`\*' (star) to obtain the value from the argument list. The below example specifies scanning 4 bytes to obtain the value of an integer in base 10. At the end of scanning, the variable **v** should have the value **1234**.

```


    sfsscanf("12345678","%.*.*d", 4, 10, &v);
    
```

**(extfdata)**:

This defines a string **extfdata** to be passed to the extension function **Sffmt\_t.extf**. Parentheses shall be balanced. If **extfdata** is **\***, the string is obtained from the argument list.

[**flag:**](#flag:)

This is **#**, **I**, or some sequence of **h**, **l**, and **L**.

Flag **#** is significant for pattern **%i** and **%[**. For **%i**, it means that the **#** symbol does not have its usual meaning in an input sequence **base#value**. For example, the scanning result of **%#i** on input **2#1001** is **2** and the next **sfgetc()** call will return **#**. For **%[**, if the next character in the input stream does not match the given scan set of characters, **#** causes a match to a null string instead of a failure.

Flag **I** defines the size or type of the object being formatted. There are two [cases: (1)](/man/os/ksh/cases:.1) **I** by itself [and (2)](/man/os/ksh/and.2) **I** followed by either a decimal number or \`\*'.

In the first case, for integer and floating point patterns, the object type is taken to be the largest appropriate type (i.e., one of **Sflong\_t**, **Sfulong\_t** or **Sfdouble\_t**). For string patterns such as **%s**, the flag is ignored.

In the second case, a given decimal value would define a size while \`\*' would cause the size to be obtained from the argument list. For string patterns such as **%s** or **%[**, this size defines the length of the buffer to store scanned data. Specifying a buffer size only limits the amount of data copied into the buffer. Scanned data beyond the buffer limit will be discarded. For integer and floating point patterns, the size is used to select a type from one of the below lists as indicated by the conversion specifier:

```


    Sflong_t, long, int, short


    Sfulong_t, unsigned long, unsigned int, unsigned short


    Sfdouble_t, double, float
    
```

The selection algorithm always matches types from left to right in any given list. Although selection is generally based on sizes in bytes, for compatibility with Microsoft-C, the size 64 is matched with an appropriate type with the same number of bits, if any. If the given size does not match any of the listed types, it shall match one of **int**, **unsigned int**, and **double** as indicated by the formatting pattern.

Below are examples of using the **I** flag. The first example scans a 64-bit integer. The second scans some floating point value whose size is explicitly computed and given. The last example scans a string into a buffer with the given size 128. Note that if the scanned string is longer than 127, only the first 127 bytes shall be copied into the buffer. The rest of the scanned data shall be discarded.

```


     sfscanf(sfstdin,"%I64d", &int64_obj);


     sfscanf(sfstdin,"%I*f", sizeof(float_obj), &float_obj);


     sfscanf(sfstdin,"%I*s", 128, buffer);
    
```

Flags **h**, **l**, and **L** are the ANSI C conventions for indicating the type of a scanned element. For example, **%hd** means scanning a **short int**. The flags **ll** and **L** mean respectively scanning an integer or a floating point value with largest size (i.e., **Sflong\_t** or **Sfdouble\_t**).

The **%i**, **%d** and **%u** patterns scan numbers in bases from **2** to **64**. **%i** scans integral values in self-describing formats. Except for octal, decimal and hexadecimal numbers with the usual formats, numbers in general bases are assumed to be of the form: *base#value* where *base* is a number in base 10 and *value* is a number in the given base. For example, **2#1001** is the binary representation of the decimal value **9**. If *base* is **36** or less, the digits for *value* can be any combination of **\[0-9], \[a-z], \[A-Z]** where upper and lower case digits are not distinguishable. If *base* is larger than **36**, the set of digits is:

```


    0123456789


    abcdefghijklmnopqrstuvwxyz


    ABCDEFGHIJKLMNOPQRSTUVWXYZ @_
```

## BUFFERING, SYNCHRONIZATION

## [void* sfsetbuf(Sfio\_t* f, void* buf, size\_t size)](#void*_sfsetbuf%28Sfio_t*_f,_void*_buf,_size_t_size%29)

This function changes the buffering scheme for the stream **f**. The stream will be synchronized before any buffer modification. If a new buffer is successfully set and the old buffer has not been freed, **sfsetbuf()** returns the old buffer. Otherwise, it returns **NULL**. After a **sfsetbuf()** call, **sfvalue()** returns the size of the returned buffer.

Sfio attempts to read data in blocks likely to be serviced fast by the file system. This means block sizes being multiples of a suitable alignment value (e.g., 512, 1024 or 8192). By default, the alignment value is computed via some internal mechanism depending on the local platform but it can also be explicitly set via the call **sfsetbuf(f, (void\*)f, size)**.

In invocations of **sfsetbuf()** other than the above case, the **size** argument is treated as follows:

[**size == SFIO\_UNBOUND**:](#size~2)

Sfio will pick a suitable buffer size. If **buf** is **NULL**, Sfio will also pick a suitable buffering scheme (such as memory mapping). If **buf** is not **NULL**, its actual value is ignored but the buffer will be allocated via [**malloc(3)**](/man/os/ksh/malloc.3). This can be used to avoid memory mapping.

[**size &gt; 0**:](#size~3)

This is the suggested size to use for buffering or memory mapping. If **buf** is **NULL**, Sfio will pick a suitable buffering scheme as discussed above. If **buf** is not **NULL**, then **buf** and **size** determine a buffer of the given size.

[**size == 0**:](#size~4)

If **buf** is **NULL**, the stream will be unbuffered. If **buf** is not **NULL**, **sfsetbuf()** simply returns the stream buffer. In this case, no attempt will be made to synchronize the stream.

## [int sfsync(Sfio\_t* f)](#int_sfsync%28Sfio_t*_f%29)

This function synchronizes the logical and physical views of stream **f**. It returns a negative value for failure and **0** for success.

For a **SFIO\_WRITE** stream, synchronization means to write out any buffered data. For a seekable **SFIO\_READ** file stream, the physical file position is aligned with the logical stream position and, if **SFIO\_SHARE** is on, buffered data is discarded. If **f** is **NULL**, all streams are synchronized. If **f** is the base of a stream stack (see **sfstack()**), all stacked streams are synchronized. Note that a stacked stream can only be synchronized this way. If **f** is in a pool (see **sfpool()**) but not being the head, the pool head is synchronized.

If **f** has flag **SFIO\_IOCHECK**, the **SFIO\_SYNC** event is raised before and after synchronization. See **sfdisc()** for details.

## [int sfpoll(Sfio\_t\** flist, int n, int timeout)](#int_sfpoll%28Sfio_t**_flist,_int_n,_int_timeout%29)

This function polls a set of streams to see if I/O operations can be performed on them without blocking. This is useful for multiplexing I/O over a set of streams. If a stream has a discipline, the exception function may be called before and after the stream is polled (see **sfdisc()** for details). After a successful **sfpoll()** call, for each ready stream **f**, **sfvalue(f)** returns a bit combination of **SFIO\_READ** and **SFIO\_WRITE** to tell which I/O mode is available. If **SFIO\_READ** is available, an attempt to read a byte will not block. If **SFIO\_WRITE** is available, an attempt to flush will not block. **sfpoll()** returns the number of ready streams or **-1** on failure.

[**flist** and **n**:](#flist)

**flist** is an array of **n** streams to be polled. Upon return, ready streams are moved to the front of **flist** in the same relative order.

[**timeout**:](#timeout)

This defines an elapse time in milliseconds to wait to see if any stream is ready for I/O. If **timeout** is negative, **sfpoll()** will block until some stream become ready. Note that **SFIO\_STRING** and normal file streams never block and are always ready for I/O. If a stream with discipline is being polled and its readiness is as yet undetermined (e.g., empty buffer,) the discipline exception function will be called with **SFIO\_DPOLL** before querying the operating system.

## [Sfio\_t* sfpool(Sfio\_t* f, Sfio\_t* poolf, int mode)](#Sfio_t*_sfpool%28Sfio_t*_f,_Sfio_t*_poolf,_int_mode%29)

This function manipulates pools of streams. In a pool, only one stream is at the head and can have buffered data. All other streams in the pool will be synchronized. A stream becomes head when it is used for some I/O operation. **sfpool()** returns **NULL** on failure.

[**f** and **poolf**:](#f~4)

If **f** is **NULL**, **sfpool()** simply returns the head of the pool containing **poolf**. If **f** is not **NULL** and **poolf** is **NULL**, **f** is deleted from its pool. In this case, if no other stream from the same pool can become head, **sfpool()** will return **NULL**; otherwise, it returns some stream from the remainder of the pool. If both **f** and **poolf** are not **NULL**, **f** is moved from its current pool (if any) into the same pool with **poolf**. In this case, **poolf** is returned.

[**mode**:](#mode~2)

If **poolf** is already in a pool, **mode** is ignored. Otherwise, **mode** should be **0** or **SFIO\_SHARE**. A **SFIO\_SHARE** pool contains streams with **SFIO\_WRITE** mode. In addition, on change to a new head stream, buffered write data of the current head is transferred to the new head.

## [int sfpurge(Sfio\_t* f)](#int_sfpurge%28Sfio_t*_f%29)

This function discards all buffered data unless **f** is a **SFIO\_STRING** stream. Note that if **f** is a **SFIO\_READ** stream based on an unseekable device, purged data will not be recoverable. If **f** is a **sfpopen**-stream opened for both read and write, data of both the read and write pipe ends will be purged (see **sfset()** to selectively turn off read or write mode if one set of data is to be preserved). After purging, if **f** has flag **SFIO\_IOCHECK**, the event **SFIO\_PURGE** is raised. **sfpurge()** returns **-1** for failure and **0** for success.

## [DISCIPLINE, EVENT-HANDLING](#DISCIPLINE,_EVENT-HANDLING)

A file stream uses the system calls [**read(2)**](/man/os/ksh/read.2), [**write(2)**](/man/os/ksh/write.2) and [**lseek(2)**](/man/os/ksh/lseek.2) to read, write and position in the underlying file. Disciplines enable application-defined I/O methods including exception handling and data pre/post-processing.

## [Sfdisc\_t* sfdisc(Sfio\_t* f, Sfdisc\_t* disc)](#Sfdisc_t*_sfdisc%28Sfio_t*_f,_Sfdisc_t*_disc%29)

Each stream has a discipline stack whose bottom is a virtual discipline representing the actual system calls. **sfdisc()** manipulates the discipline stack of stream **f**. **f** will be synchronized before any discipline stack manipulation. After a successful discipline stack manipulation, the stream I/O position (see **sfseek()** and **sftell()**) and extent (see **sfsize()**) are updated to reflect that defined by the top discipline. **sfdisc()** returns **NULL** on failure.

If the value of **disc** is identical to the value of **f**, then the top discipline on the discipline stack is returned without any further action. An application can then use this feature of **sfdisc()** and the field **disc** (below) of the discipline structure to traverse the entire discipline stack of a stream **f** as follows:

```


    for(disc = sfdisc(f, (Sfdisc_t*)f); disc; disc = disc->disc)
```

If **disc** is **SFIO\_POPDISC** or **NULL**, the top element of the stack, if any, is popped and its address is returned. Otherwise, **disc** is pushed onto the discipline stack. In this case, if successful, **sfdisc()** returns the discipline that was pushed down.

Note that a discipline can be used on only one stream at a time. An application should take care to allocate different discipline structures for use with different streams. A discipline structure is of the type **Sfdisc\_t** which contains the following public fields:

```


    Sfread_f   readf;


    Sfwrite_f  writef;


    Sfseek_f   seekf;


    Sfexcept_f exceptf;


    Sfdisc_t*   disc;
```

The first three fields of **Sfdisc\_t** specify alternative I/O functions. If any of them is **NULL**, it is inherited from a discipline pushed earlier on the stack. Note that a file stream always has [**read(2)**](/man/os/ksh/read.2), [**write(2)**](/man/os/ksh/write.2), [**lseek(2)**](/man/os/ksh/lseek.2) and **NULL** as the *logical bottom discipline*. Arguments to I/O discipline functions have the same meaning as that of the functions **sfrd()**, **sfwr()** and **sfsk()** described below.

The exception function, **(\*exceptf)()** announces exceptional events during I/O operations. It is called as **(\*exceptf)(Sfio\_t* f, int type, void* value, Sfdisc\_t* disc)**. Unless noted otherwise, the return value of **(\*exceptf)()** is used as follows:

**&lt;0**:

The on-going operation shall terminate.

**&gt;0**:

If the event was raised due to an I/O error, the error has been repaired and the on-going operation shall continue normally. For some events, e.g., **SFIO\_DPOLL**, the return value may also have additional meanings.

**=0**:

The on-going operation performs default actions with respect to the raised event. For example, on a reading error or reaching end of file, the top stream of a stack will be popped and closed and the on-going operation continue with the new top stream.

The argument **type** of **(\*exceptf)()** identifies the particular exceptional event:

[**SFIO\_LOCKED**:](#SFIO_LOCKED)

The stream cannot be accessed because it was frozen by certain operations such as **sfreserve()** or **sfstack()**.

[**SFIO\_READ**, **SFIO\_WRITE**:](#SFIO_READ~2)

These events are raised around reading and writing operations.

If **SFIO\_IOCHECK** is on, **SFIO\_READ** and **SFIO\_WRITE** are raised immediately before **[read(2)](/man/os/ksh/read.2) and [write(2)](/man/os/ksh/write.2)** calls. In this case, **\*((ssize\_t\*)value)** is the amount of data to be processed. The return value of **(\*exceptf)()**, if negative, indicates that the stream is not ready for I/O and the calling operation will abort with failure. If it is positive, the stream is ready for I/O but the amount should be restricted to the amount specified by this value. If the return value is zero, the I/O operation is carried out normally.

**SFIO\_READ** and **SFIO\_WRITE** are also raised on operation failures. In such a case, **\*((ssize\_t\*)value)** is the return value from the failed operation.

[**SFIO\_SEEK**:](#SFIO_SEEK)

This event is raised when a seek operation fails.

[**SFIO\_NEW**, **SFIO\_CLOSING**, **SFIO\_FINAL**:](#SFIO_NEW)

These events are raised during a stream closing. **SFIO\_NEW** is raised for a stream about to be closed to be renewed (see **sfnew()**). **SFIO\_CLOSING** is raised for a stream about to be closed. **SFIO\_FINAL** is raised after a stream has been closed and before its space is to be destroyed (see **sfclose()**). For these events, a non-zero return value from **(\*exceptf)()** causes **sfclose()** to return immediately with the same value.

[**SFIO\_DPUSH**, **SFIO\_DPOP**, **SFIO\_DBUFFER**:](#SFIO_DPUSH)

Events **SFIO\_DPUSH** and **SFIO\_DPOP** are raised when a discipline is about to be pushed or popped. **(Sfdisc\_t\*)value** is the to-be top discipline, if any.

A stream buffer is always synchronized before pushing or popping a discipline. If this synchronization fails, **SFIO\_DBUFFER** will be raised with **\*((size\_t\*)value)** being the amount of data still in the buffer. If the return value of **exceptf** is non-negative, the push or pop operation will continue normally; otherwise, **sfdisc()** returns failure.

[**SFIO\_DPOLL**:](#SFIO_DPOLL)

This event is raised by **sfpoll()** to see if the stream is ready for I/O. **\*((int\*)value)** indicates a time-out interval to wait. A negative return value from the exception function means blocking. A zero return value means that **sfpoll()** should query the underlying file descriptor. A positive return value means non-blocking. In addition, this value will be a bit combination of **SFIO\_READ** and **SFIO\_WRITE** to indicate what I/O modes are ready.

[**SFIO\_READY**:](#SFIO_READY)

This event is raised by **sfpoll()** for each ready stream. The third argument to the event handler is an integer composed with the two bits **SFIO\_READ** and **SFIO\_WRITE** to indicate which I/O modes are ready.

[**SFIO\_SYNC**, **SFIO\_PURGE**:](#SFIO_SYNC)

If **SFIO\_IOCHECK** is set, these events are raised respectively for a **sfsync()** or **sfpurge()** call. In each case, the respective event is raised once before the appropriate operation (synchronization or purging) with **((int)value)** being **1** and once after with **((int)value)** being **0**. Note that **sfsync()** is called for each **SFIO\_WRITE** or **SFIO\_SHARE|SFIO\_READ** stream on closing.

[**SFIO\_ATEXIT**:](#SFIO_ATEXIT)

This event is raised for each open stream before the process exits.

## [int sfraise(Sfio\_t* f, int type, void* data)](#int_sfraise%28Sfio_t*_f,_int_type,_void*_data%29)

If **f** is non-**NULL**, **sfraise()** calls all exception handlers of **f** with the event **type** and associated **data**. If an exception handler returns a non-zero value, **sfraise()** immediate returns the same value. Application-defined events should start from the value **SFIO\_EVENT** so as to avoid confusion with system-defined events, **sfraise()** returns **0** on success and **-1** on failure.

If **f** is **NULL**, **sfraise()** iterates over all streams and raise events as described above. In this case, **sfraise()** returns **0** on success and a negative value on failure. The absolute value of the return value tells how many streams failed on raising the given event.

## [ssize\_t sfrd(Sfio\_t* f, void* buf, size\_t n, Sfdisc\_t* disc)](#ssize_t_sfrd%28Sfio_t*_f,_void*_buf,_size_t_n,_Sfdisc_t*_disc%29)

## [ssize\_t sfwr(Sfio\_t* f, const void* buf, size\_t n, Sfdisc\_t* disc)](#ssize_t_sfwr%28Sfio_t*_f,_const_void*_buf,_size_t_n,_Sfdisc_t*_disc%29)

## [Sfoff\_t sfsk(Sfio\_t* f, Sfoff\_t offset, int type, Sfdisc\_t* disc)](#Sfoff_t_sfsk%28Sfio_t*_f,_Sfoff_t_offset,_int_type,_Sfdisc_t*_disc%29)

These functions provides safe methods for a discipline I/O function to invoke earlier discipline I/O functions and to properly handle exceptions. They should not be used in any other context. **sfrd()** and **sfwr()** return the number of bytes read or written. **sfsk()** returns the new seek position. On error, all three functions return a negative value which should be **-1** or the value returned by the exception handler.

## STREAM CONTROL

## [int sfresize(Sfio\_t* f, Sfoff\_t size)](#int_sfresize%28Sfio_t*_f,_Sfoff_t_size%29)

This function resizes the stream **f** so that its extent is **size**. If the stream corresponds to a file, the file size is set to **size** via the system call **ftruncate()**. When a stream is made larger, the new data space is filled with zero's. **sfresize()** returns **0** on success and a negative value on failure.

## [int sfset(Sfio\_t* f, int flags, int set)](#int_sfset%28Sfio_t*_f,_int_flags,_int_set%29)

This function sets control flags for the stream **f**. It returns the previous set of flags or **0** on error.

Settable flags are: **SFIO\_READ**, **SFIO\_WRITE**, **SFIO\_IOCHECK**, **SFIO\_LINE**, **SFIO\_SHARE**, **SFIO\_PUBLIC**, **SFIO\_MALLOC** and **SFIO\_STATIC**. Note that **SFIO\_READ** and **SFIO\_WRITE** can be turned on or off only if the stream was opened as **SFIO\_READ|SFIO\_WRITE**. Turning off one of them means that the stream is to be treated exclusively in the other mode. It is not possible to turn off both. If legal, an attempt to turn on either **SFIO\_READ** or **SFIO\_WRITE** will cause the stream to be in the given I/O mode.

[**set == 0:**](#set)

If **flags** is zero, the current set of flags is simply returned. Note that when a stream is first opened, not all of its flags are initialized yet (more below). If **flags** is non-zero, an attempt is made to turn off the specified flags.

[**set != 0:**](#set~2)

If **flags** is zero, the stream is initialized if not yet done so. Then the current set of flags is returned. If **flags** is non-zero, an attempt is made to turn on the specified flags.

## [int sfsetfd(Sfio\_t* f, int fd)](#int_sfsetfd%28Sfio_t*_f,_int_fd%29)

This function changes the file descriptor of **f**. Before a change is realized, **(\*notify)(f,SFIO\_SETFD,newfd)** (see **sfnotify()**) is called. **sfsetfd()** returns **-1** on failure and the new file descriptor on success.

[**fd &gt;= 0**:](#fd~2)

If the current file descriptor is non-negative, it will be changed using [**dup(3)**](/man/os/ksh/dup.3) to a value larger or equal to **fd**. Upon a successful change, the previous file descriptor will be closed. If the current file descriptor is negative, it will be set to **fd** and the stream will be reinitialized.

[**fd &lt; 0**:](#fd~3)

The stream is synchronized (see **sfsync()**) and its file descriptor will be set to this value. Then, except for **sfclose()**, the stream will be inaccessible until a future **sfsetfd()** call resets the file descriptor to a non-negative value. Thus, **sfsetfd(f,-1)** can be used to avoid closing the file descriptor of **f** when **f** is closed.

## [Sfio\_t* sfstack(Sfio\_t* base, Sfio\_t* top)](#Sfio_t*_sfstack%28Sfio_t*_base,_Sfio_t*_top%29)

This function stacks or unstacks stream. Every stream stack is identified by a base stream via which all I/O operations are performed. However, an I/O operation always takes effect on the top stream. If the top stream reaches the end of file or has an unrecoverable error condition, it is automatically popped and closed (see also **sfdisc()** for alternative handling of these conditions).

[**base**:](#base~3)

This is the base stream of the stack. If it is **NULL**, **sfstack()** does nothing and returns **top**.

[**top**:](#top)

If this is **SFIO\_POPSTACK** or **NULL**, the stack is popped and **sfstack()** returns the popped stream. Otherwise, **top** is pushed on top of the stack identified by **base** and **sfstack()** returns the **base** stream.

## [char* sfstruse(Sfio\_t* f)](#char*_sfstruse%28Sfio_t*_f%29)

Makes the string in a Sfio string buffer ready for use and makes the buffer ready for reuse. This is done by writing a terminating 0 byte to the given **SFIO\_STRING** buffer, setting the I/O position to the start of the buffer so that subsequent write operations will overwrite it, and returning a pointer to the beginning of the string.

## [Sfio\_t* sfswap(Sfio\_t* f1, Sfio\_t* f2)](#Sfio_t*_sfswap%28Sfio_t*_f1,_Sfio_t*_f2%29)

This function swaps contents of **f1** and **f2**. This fails if either stream is in a stream stack but not being a base stream. If **f2** is **NULL**, a new stream is constructed as a duplicate of **f1**. **sfswap()** returns **f2** or **f1** duplicate on success and **NULL** on failure.

## STREAM INFORMATION

## [Sfoff\_t sfsize(Sfio\_t* f)](#Sfoff_t_sfsize%28Sfio_t*_f%29)

This function returns the size of stream **f** (see **sfnew()**). If **f** is not seekable or if its size is not determinable, **sfsize()** returns **-1**.

## [Sfoff\_t sftell(Sfio\_t* f)](#Sfoff_t_sftell%28Sfio_t*_f%29)

This function returns the current I/O position in stream **f**. Note that if **f** is **SFIO\_APPENDWR** and a writing operation was just performed, the current I/O position is at the physical end of file. If **f** is unseekable, **sftell** returns the number of bytes read from or written to **f**. See also **sfungetc()**.

## [ssize\_t sfvalue(Sfio\_t* f)](#ssize_t_sfvalue%28Sfio_t*_f%29)

This function returns the string or buffer length for **sfreserve()**, **sfsetbuf()**, and **sfgetr()**.

## [int sffileno(Sfio\_t* f)](#int_sffileno%28Sfio_t*_f%29)

This function returns the file descriptor of stream **f**.

## [int sfstacked(Sfio\_t* f)](#int_sfstacked%28Sfio_t*_f%29)

This function returns a non-zero value if stream **f** has been stacked.

## [int sfeof(Sfio\_t* f)](#int_sfeof%28Sfio_t*_f%29)

## [int sferror(Sfio\_t* f)](#int_sferror%28Sfio_t*_f%29)

## [int sfclrerr(Sfio\_t* f)](#int_sfclrerr%28Sfio_t*_f%29)

**sfeof()** tells whether or not the stream has an end-of-file condition. **sferror()** tells whether or not the stream has an error condition. **sfclrerr()** clears both end-of-file and error conditions. The end-of-file and error conditions are also cleared on an I/O operation.

## [int sfclrlock(Sfio\_t* f)](#int_sfclrlock%28Sfio_t*_f%29)

This function restores the stream back to a normal state. This means clearing locks and possibly throwing away unprocessed data. As such, this operation is unsafe and should be used with care. For example, it may be used before a long jump ([**longjmp(3)**](/man/os/ksh/longjmp.3)) out of some discipline I/O function to restore the internal stream states. **sfclrlock()** returns the current set of flags.

## [int sfnotify((void(\*)notify)(Sfio\_t\*, int, void\*) )](#int_sfnotify%28%28void%28*%29notify%29%28Sfio_t*,_int,_void*%29_%29)

This sets a function **(\*notify)()** to be called as **(\*notify)(f, type, data)** on various stream events. Arguments **type** and **data** indicate the reason for the call and accompanying data:

[**SFIO\_NEW**:](#SFIO_NEW~2)

**f** is being opened and **data** is the underlying file descriptor.

[**SFIO\_CLOSING**:](#SFIO_CLOSING)

**f** is the stream being closed and **data** is the underlying file descriptor.

[**SFIO\_SETFD**:](#SFIO_SETFD)

The file descriptor of **f** is being changed to the one defined by **data** (see **sfsetfd()**).

[**SFIO\_READ**:](#SFIO_READ~3)

An attempt to change **f** to read mode failed. **data** is the file descriptor of the stream.

[**SFIO\_WRITE**:](#SFIO_WRITE)

An attempt to change **f** to write mode failed. **data** is the file descriptor of the stream.

## [int sfwalk(Sfwalk\_f walkf, void* data, int type)](#int_sfwalk%28Sfwalk_f_walkf,_void*_data,_int_type%29)

This function invokes **(\*walkf)(f, data)** on every open stream **f** whose flags as defined by **sfset()** contains all bit flags given in **type**. On such a call, if the return value is negative, **sfwalk()** will terminate. **sfwalk()** returns 0 if no stream was processed. Otherwise, it returns the return value from the last invocation of **walkf()**.

As an example, the call **sfwalk(walkf, data, SFIO\_READ)** will iterate over all streams opened for reading. Similarly, **sfwalk(walkf, data, SFIO\_READ|SFIO\_WRITE)** iterates over all streams opened for both reading and writing. Lastly, **sfwalk(walkf, data, 0)** iterates over all streams.

## MISCELLANEOUS FUNCTIONS

## [ssize\_t sfmaxr(ssize\_t maxr, int set)](#ssize_t_sfmaxr%28ssize_t_maxr,_int_set%29)

Certain records may require too much memory for storage, thus, causing undesirable side effects. Therefore, the library normally bounds the amount of memory used by **sfgetr()**. A different memory bound can be set via **sfmaxr()**. While a positive **maxr** hints to **sfgetr()** to use only about that much memory to construct a record, a non-positive bound allows **sfgetr()** to use as much memory as necessary. **sfmaxr()** sets the value only if **set** is non-zero. It returns the value before setting or the current value if not setting.

## [ssize\_t sfslen()](#ssize_t_sfslen%28%29)

This function returns the length of a string just constructed by **sfsprintf()** or **sfprints()**. See also **sfvalue()**.

## [int sfulen(Sfulong\_t v)](#int_sfulen%28Sfulong_t_v%29)

## [int sfllen(Sflong\_t v)](#int_sfllen%28Sflong_t_v%29)

## [int sfdlen(Sfdouble\_t v)](#int_sfdlen%28Sfdouble_t_v%29)

These functions return respectively the number of bytes required to code the **Sfulong\_t**, **Sflong\_t** or **Sfdouble\_t** value **v** by **sfputu()**, **sfputl()** or **sfputd()**.

## [ssize\_t sfpkrd(int fd, char* buf, size\_t n, int rsc, long tm, int action)](#ssize_t_sfpkrd%28int_fd,_char*_buf,_size_t_n,_int_rsc,_long_tm,_int_action%29)

This function acts directly on the file descriptor **fd**. It does a combination of peeking on incoming data and a time-out read. Upon success, it returns the number of bytes received. A return value of **0** means that the end-of-file condition has been detected. A negative value represents an error.

[**buf**, **n**:](#buf~2)

These define a buffer and its size to read data into.

[**rsc**:](#rsc)

If **&gt;=0**, this defines a record separator. If the last returned byte is not the record separator, then the read data did not contain a complete record. Otherwise, it contains one or more records. See also **action** below.

[**tm**:](#tm)

If **&gt;=0**, this defines a time interval in milliseconds to wait for incoming data.

[**action**:](#action)

If **action &gt; 0**, **sfpkrd()** will peek on incoming data but will not read past it. Therefore, a future **sfpkrd()** or [**read(2)**](/man/os/ksh/read.2) will see the same data again. If **action &lt;= 0**, **sfpkrd()** will not peek and there are two cases. If **rsc &lt; 0**, an attempt is made to read **n** bytes. If **rsc &gt;= 0**, an attempt is made to read one record.

## FULL STRUCTURE SFIO\_T

## [#include &lt;sfio\_t.h&gt;](#_include__sfio_t.h_)

Most applications based on Sfio only need to include the header file **sfio.h** which defines an abbreviated **Sfio\_t** structure without certain fields private to Sfio. However, there are times (e.g., debugging) when an application may require more details about the full **Sfio\_t** structure. In such cases, the header file **sfio\_t.h** can be used in place of **sfio.h**. Note that an application doing this will become sensitive to changes in the internal architecture of Sfio.

## [#define SFNEW(buf,size,file,flags,disc)](#_define_SFNEW%28buf,size,file,flags,disc%29)

This macro function is defined in **sfio\_t.h** for use in static initialization of an **Sfio\_t** structure. It requires five arguments:

[**buf, size**:](#buf,)

These define a buffer and its size.

[**file**:](#file)

This defines the underlying file descriptor if any.

[**flags**:](#flags~3)

This is composed from bit flags described above.

[**disc**:](#disc)

This defines a discipline if any.

## EXAMPLE DISCIPLINES

The below functions create disciplines and insert them into the given streams **f**. These functions return **0** on success and **-1** on failure.

## [int sfdcdio(Sfio\_t* f, size\_t bufsize)](#int_sfdcdio%28Sfio_t*_f,_size_t_bufsize%29)

This creates a discipline that uses the direct IO feature available on file systems such as SGI's XFS to speed up IO. The argument **bufsize** suggests a buffer size to use for data transfer.

## [int sfdcdos(Sfio\_t* f)](#int_sfdcdos%28Sfio_t*_f%29)

This creates a discipline to read DOS text files. It basically transforms pairs of \\r\\n to \\n.

## [int sfdcfilter(Sfio\_t* f, const char* cmd)](#int_sfdcfilter%28Sfio_t*_f,_const_char*_cmd%29)

This creates a discipline that sends data from **f** to the given command **cmd** to process, then reads back the processed data.

## [int sfdcseekable(Sfio\_t* f)](#int_sfdcseekable%28Sfio_t*_f%29)

This creates a discipline that makes an unseekable reading stream seekable.

## [int sfdcslow(Sfio\_t* f)](#int_sfdcslow%28Sfio_t*_f%29)

This creates a discipline that makes all Sfio operations return immediately on interrupts. This is useful for dealing with slow devices.

## [int sfdcsubstream(Sfio\_t* f, Sfio\_t* parent, Sfoff\_t offset, Sfoff\_t extent)](#int_sfdcsubstream%28Sfio_t*_f,_Sfio_t*_parent,_Sfoff_t_offset,_Sfoff_t_extent%29)

This creates a discipline that makes **f** acts as if it corresponds exactly to the subsection of **parent** starting at **offset** with size **extent**.

## [int sfdctee(Sfio\_t* f, Sfio\_t* tee)](#int_sfdctee%28Sfio_t*_f,_Sfio_t*_tee%29)

This creates a discipline that copies to the stream **tee** any data written to **f**.

## [int sfdcunion(Sfio\_t* f, Sfio\_t\** array, int n)](#int_sfdcunion%28Sfio_t*_f,_Sfio_t**_array,_int_n%29)

This creates a discipline that makes **f** act as if it is the concatenation of the **n** streams given in **array**.

## [int sfdclzw(Sfio\_t* f)](#int_sfdclzw%28Sfio_t*_f%29)

This creates a discipline that would decompress data in **f**. The stream **f** should have data from a source compressed by the Unix **compress** program.

## [int sfdcgzip(Sfio\_t* f, int opt)](#int_sfdcgzip%28Sfio_t*_f,_int_opt%29)

This creates a discipline for reading/writing data compressed by zlib. The argument **opt** defines the optimization level.

## [STDIO COMPATIBILITY](#STDIO_COMPATIBILITY)

Sfio provides compatibility functions for all various popular Stdio implementations at source and binary level. The source Stdio compatibility interface provides the header file **stdio.h** that defines a set of macros or inlined functions to map Stdio calls to Sfio ones. This mapping may benignly extend or change the meaning of certain original Stdio operations. For example, Sfio's version of **popen()** allows a coprocess to be opened for both reading and writing unlike the original call which only allows a coprocess to be opened for a single mode. Similarly, Sfio's **fopen()** call can be used to create string streams in addition to file streams.

The standard streams **stdin**, **stdout** and **stderr** are mapped via **#define** to **sfstdin**, **sfstdout** and **sfstderr**. The latter are typically declared of the type **Sfio\_t\***. Certain older Stdio applications require these to be declared as addresses of structures so that static initializations of the sort \`\`**FILE* f = stdin;**'' would work. Such applications should use the compile time flag **SFIO\_FILE\_STRUCT** to achieve the desired effect.

Below are the supported Stdio functions:

```
FILE*  fopen(const char* file, const char* mode);
FILE*  freopen(const char* file, const char* mode, FILE* stream);
FILE*  fdopen(int filedesc, const char* mode);
FILE*  popen(const char* command, const char* mode);
FILE*  tmpfile();
int    fclose(FILE* stream);
int    pclose(FILE* stream);
void   setbuf(FILE* stream, char* buf);
int    setvbuf(FILE* stream, char* buf, int mode, size_t size);
void   setbuffer(FILE* stream, char* buf, size_t size);
int    setlinebuf(FILE* stream);
int    fflush(FILE* stream);
int    fpurge(FILE* stream);
int    fseek(FILE* stream, long offset, int whence);
void   rewind(FILE* stream);
int    fgetpos(FILE* stream, fpos_t* pos);
int    fsetpos(FILE* stream, fpos_t* pos);
long   ftell(FILE* stream);
int    getc(FILE* stream);
int    fgetc(FILE* stream);
int    getchar(void);
int    ungetc(int c, FILE* stream);
int    getw(FILE* stream);
char*  gets(char* s);
char*  fgets(char* s, int n, FILE* stream);
size_t fread(void* ptr, size_t size, size_t nelt, FILE* stream);
int    putc(int c, FILE* stream);
int    fputc(int c, FILE* stream);
int    putchar(int c);
int    putw(int w, FILE* stream);
int    puts(const char* s, FILE* stream);
int    fputs(const char* s, FILE* stream);
size_t fwrite(const void* ptr, size_t size, size_t nelt, FILE* stream);
int    fscanf(FILE* stream, const char* format, ...);
int    vfscanf(FILE* stream, const char* format, va_list args);
int    _doscan(FILE* stream, const char* format, va_list args);
int    scanf(const char* format, ...);
int    vscanf(const char* format, va_list args);
int    sscanf(const char* s, const char* format, ...);
int    vsscanf(const char* s, const char* format, va_list args);
int    fprintf(FILE* stream, const char* format, ...);
int    vfprintf(FILE* stream, const char* format, va_list args);
int    _doprnt(FILE* stream, const char* format, va_list args);
int    printf(const char* format, ...);
int    vprintf(const char* format, va_list args);
int    sprintf(const char* s, const char* format, ...);
int    snprintf(const char* s, int n, const char* format, ...);
int    vsprintf(const char* s, const char* format, va_list args);
int    vsnprintf(const char* s, int n, const char* format, va_list args);
int    feof(FILE* stream);
int    ferror(FILE* stream);
int    clearerr(FILE* stream);
```

# [AUTHORS](#AUTHORS)

Kiem-Phong Vo, kpv@research.att.com,  
David G. Korn, dgk@research.att.com, and  
Glenn S. Fowler, gsf@research.att.com.

2 March 2024

---

Document: sfio(3ast) from ksh 1.0.10-r36 (Chainguard OS)
Source: https://github.com/ksh93/ksh
