1 /* $OpenBSD: shf.c,v 1.33 2018/03/15 16:51:29 anton Exp $ */
4 * Shell file I/O routines
19 /* flags to shf_emptybuf() */
20 #define EB_READSW 0x01 /* about to switch to reading */
21 #define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
24 * Replacement stdio routines. Stdio is too flakey on too many machines
25 * to be useful when you have multiple processes using the same underlying
29 static int shf_fillbuf(struct shf
*);
30 static int shf_emptybuf(struct shf
*, int);
32 /* Open a file. First three args are for open(), last arg is flags for
33 * this package. Returns NULL if file could not be opened, or if a dup
37 shf_open(const char *name
, int oflags
, int mode
, int sflags
)
40 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
43 /* Done before open so if alloca fails, fd won't be lost. */
44 shf
= alloc(sizeof(struct shf
) + bsize
, ATEMP
);
46 shf
->buf
= (unsigned char *) &shf
[1];
48 shf
->flags
= SHF_ALLOCS
;
49 /* Rest filled in by reopen. */
51 fd
= open(name
, oflags
, mode
);
53 afree(shf
, shf
->areap
);
56 if ((sflags
& SHF_MAPHI
) && fd
< FDBASE
) {
59 nfd
= fcntl(fd
, F_DUPFD
, FDBASE
);
62 afree(shf
, shf
->areap
);
67 sflags
&= ~SHF_ACCMODE
;
68 sflags
|= (oflags
& O_ACCMODE
) == O_RDONLY
? SHF_RD
:
69 ((oflags
& O_ACCMODE
) == O_WRONLY
? SHF_WR
: SHF_RDWR
);
71 return shf_reopen(fd
, sflags
, shf
);
74 /* Set up the shf structure for a file descriptor. Doesn't fail. */
76 shf_fdopen(int fd
, int sflags
, struct shf
*shf
)
78 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
80 /* use fcntl() to figure out correct read/write flags */
81 if (sflags
& SHF_GETFL
) {
82 int flags
= fcntl(fd
, F_GETFL
);
85 /* will get an error on first read/write */
88 switch (flags
& O_ACCMODE
) {
102 if (!(sflags
& (SHF_RD
| SHF_WR
)))
103 internal_errorf("%s: missing read/write", __func__
);
107 shf
->buf
= alloc(bsize
, ATEMP
);
108 sflags
|= SHF_ALLOCB
;
112 shf
= alloc(sizeof(struct shf
) + bsize
, ATEMP
);
113 shf
->buf
= (unsigned char *) &shf
[1];
114 sflags
|= SHF_ALLOCS
;
118 shf
->rp
= shf
->wp
= shf
->buf
;
121 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
122 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
126 if (sflags
& SHF_CLEXEC
)
127 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
131 /* Set up an existing shf (and buffer) to use the given fd */
133 shf_reopen(int fd
, int sflags
, struct shf
*shf
)
135 int bsize
= sflags
& SHF_UNBUF
? (sflags
& SHF_RD
? 1 : 0) : SHF_BSIZE
;
137 /* use fcntl() to figure out correct read/write flags */
138 if (sflags
& SHF_GETFL
) {
139 int flags
= fcntl(fd
, F_GETFL
);
142 /* will get an error on first read/write */
145 switch (flags
& O_ACCMODE
) {
159 if (!(sflags
& (SHF_RD
| SHF_WR
)))
160 internal_errorf("%s: missing read/write", __func__
);
161 if (!shf
|| !shf
->buf
|| shf
->bsize
< bsize
)
162 internal_errorf("%s: bad shf/buf/bsize", __func__
);
164 /* assumes shf->buf and shf->bsize already set up */
166 shf
->rp
= shf
->wp
= shf
->buf
;
169 shf
->wnleft
= 0; /* force call to shf_emptybuf() */
170 shf
->wbsize
= sflags
& SHF_UNBUF
? 0 : bsize
;
171 shf
->flags
= (shf
->flags
& (SHF_ALLOCS
| SHF_ALLOCB
)) | sflags
;
173 if (sflags
& SHF_CLEXEC
)
174 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
178 /* Open a string for reading or writing. If reading, bsize is the number
179 * of bytes that can be read. If writing, bsize is the maximum number of
180 * bytes that can be written. If shf is not null, it is filled in and
181 * returned, if it is null, shf is allocated. If writing and buf is null
182 * and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
183 * used for the initial size). Doesn't fail.
184 * When writing, a byte is reserved for a trailing null - see shf_sclose().
187 shf_sopen(char *buf
, int bsize
, int sflags
, struct shf
*shf
)
189 /* can't have a read+write string */
190 if (!(sflags
& (SHF_RD
| SHF_WR
)) ||
191 (sflags
& (SHF_RD
| SHF_WR
)) == (SHF_RD
| SHF_WR
))
192 internal_errorf("%s: flags 0x%x", __func__
, sflags
);
195 shf
= alloc(sizeof(struct shf
), ATEMP
);
196 sflags
|= SHF_ALLOCS
;
199 if (!buf
&& (sflags
& SHF_WR
) && (sflags
& SHF_DYNAMIC
)) {
202 sflags
|= SHF_ALLOCB
;
203 buf
= alloc(bsize
, shf
->areap
);
206 shf
->buf
= shf
->rp
= shf
->wp
= (unsigned char *) buf
;
209 shf
->wnleft
= bsize
- 1; /* space for a '\0' */
211 shf
->flags
= sflags
| SHF_STRING
;
218 /* Flush and close file descriptor, free the shf structure */
220 shf_close(struct shf
*shf
)
225 ret
= shf_flush(shf
);
226 if (close(shf
->fd
) < 0)
229 if (shf
->flags
& SHF_ALLOCS
)
230 afree(shf
, shf
->areap
);
231 else if (shf
->flags
& SHF_ALLOCB
)
232 afree(shf
->buf
, shf
->areap
);
237 /* Flush and close file descriptor, don't free file structure */
239 shf_fdclose(struct shf
*shf
)
244 ret
= shf_flush(shf
);
245 if (close(shf
->fd
) < 0)
256 /* Close a string - if it was opened for writing, it is null terminated;
257 * returns a pointer to the string and frees shf if it was allocated
258 * (does not free string if it was allocated).
261 shf_sclose(struct shf
*shf
)
263 unsigned char *s
= shf
->buf
;
266 if (shf
->flags
& SHF_WR
) {
270 if (shf
->flags
& SHF_ALLOCS
)
271 afree(shf
, shf
->areap
);
275 /* Un-read what has been read but not examined, or write what has been
276 * buffered. Returns 0 for success, EOF for (write) error.
279 shf_flush(struct shf
*shf
)
281 if (shf
->flags
& SHF_STRING
)
282 return (shf
->flags
& SHF_WR
) ? EOF
: 0;
285 internal_errorf("%s: no fd", __func__
);
287 if (shf
->flags
& SHF_ERROR
) {
292 if (shf
->flags
& SHF_READING
) {
293 shf
->flags
&= ~(SHF_EOF
| SHF_READING
);
294 if (shf
->rnleft
> 0) {
295 lseek(shf
->fd
, (off_t
) -shf
->rnleft
, SEEK_CUR
);
300 } else if (shf
->flags
& SHF_WRITING
)
301 return shf_emptybuf(shf
, 0);
306 /* Write out any buffered data. If currently reading, flushes the read
307 * buffer. Returns 0 for success, EOF for (write) error.
310 shf_emptybuf(struct shf
*shf
, int flags
)
314 if (!(shf
->flags
& SHF_STRING
) && shf
->fd
< 0)
315 internal_errorf("%s: no fd", __func__
);
317 if (shf
->flags
& SHF_ERROR
) {
322 if (shf
->flags
& SHF_READING
) {
323 if (flags
& EB_READSW
) /* doesn't happen */
325 ret
= shf_flush(shf
);
326 shf
->flags
&= ~SHF_READING
;
328 if (shf
->flags
& SHF_STRING
) {
331 /* Note that we assume SHF_ALLOCS is not set if SHF_ALLOCB
332 * is set... (changing the shf pointer could cause problems)
334 if (!(flags
& EB_GROW
) || !(shf
->flags
& SHF_DYNAMIC
) ||
335 !(shf
->flags
& SHF_ALLOCB
))
337 /* allocate more space for buffer */
338 nbuf
= areallocarray(shf
->buf
, 2, shf
->wbsize
, shf
->areap
);
339 shf
->rp
= nbuf
+ (shf
->rp
- shf
->buf
);
340 shf
->wp
= nbuf
+ (shf
->wp
- shf
->buf
);
341 shf
->rbsize
+= shf
->wbsize
;
342 shf
->wnleft
+= shf
->wbsize
;
346 if (shf
->flags
& SHF_WRITING
) {
347 int ntowrite
= shf
->wp
- shf
->buf
;
348 unsigned char *buf
= shf
->buf
;
351 while (ntowrite
> 0) {
352 n
= write(shf
->fd
, buf
, ntowrite
);
354 if (errno
== EINTR
&&
355 !(shf
->flags
& SHF_INTERRUPT
))
357 shf
->flags
|= SHF_ERROR
;
360 if (buf
!= shf
->buf
) {
361 /* allow a second flush
363 memmove(shf
->buf
, buf
,
365 shf
->wp
= shf
->buf
+ ntowrite
;
372 if (flags
& EB_READSW
) {
375 shf
->flags
&= ~SHF_WRITING
;
380 shf
->wnleft
= shf
->wbsize
;
382 shf
->flags
|= SHF_WRITING
;
387 /* Fill up a read buffer. Returns EOF for a read error, 0 otherwise. */
389 shf_fillbuf(struct shf
*shf
)
391 if (shf
->flags
& SHF_STRING
)
395 internal_errorf("%s: no fd", __func__
);
397 if (shf
->flags
& (SHF_EOF
| SHF_ERROR
)) {
398 if (shf
->flags
& SHF_ERROR
)
403 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
406 shf
->flags
|= SHF_READING
;
410 shf
->rnleft
= blocking_read(shf
->fd
, (char *) shf
->buf
,
412 if (shf
->rnleft
< 0 && errno
== EINTR
&&
413 !(shf
->flags
& SHF_INTERRUPT
))
417 if (shf
->rnleft
<= 0) {
418 if (shf
->rnleft
< 0) {
419 shf
->flags
|= SHF_ERROR
;
425 shf
->flags
|= SHF_EOF
;
430 /* Read a buffer from shf. Returns the number of bytes read into buf,
431 * if no bytes were read, returns 0 if end of file was seen, EOF if
432 * a read error occurred.
435 shf_read(char *buf
, int bsize
, struct shf
*shf
)
437 int orig_bsize
= bsize
;
440 if (!(shf
->flags
& SHF_RD
))
441 internal_errorf("%s: flags %x", __func__
, shf
->flags
);
444 internal_errorf("%s: bsize %d", __func__
, bsize
);
447 if (shf
->rnleft
== 0 &&
448 (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
453 memcpy(buf
, shf
->rp
, ncopy
);
457 shf
->rnleft
-= ncopy
;
459 /* Note: fread(3S) returns 0 for errors - this doesn't */
460 return orig_bsize
== bsize
? (shf_error(shf
) ? EOF
: 0) :
464 /* Read up to a newline or EOF. The newline is put in buf; buf is always
465 * null terminated. Returns NULL on read error or if nothing was read before
466 * end of file, returns a pointer to the null byte in buf otherwise.
469 shf_getse(char *buf
, int bsize
, struct shf
*shf
)
473 char *orig_buf
= buf
;
475 if (!(shf
->flags
& SHF_RD
))
476 internal_errorf("%s: flags %x", __func__
, shf
->flags
);
481 --bsize
; /* save room for null */
483 if (shf
->rnleft
== 0) {
484 if (shf_fillbuf(shf
) == EOF
)
486 if (shf
->rnleft
== 0) {
488 return buf
== orig_buf
? NULL
: buf
;
491 end
= (unsigned char *) memchr((char *) shf
->rp
, '\n',
493 ncopy
= end
? end
- shf
->rp
+ 1 : shf
->rnleft
;
496 memcpy(buf
, (char *) shf
->rp
, ncopy
);
498 shf
->rnleft
-= ncopy
;
501 } while (!end
&& bsize
);
506 /* Returns the char read. Returns EOF for error and end of file. */
508 shf_getchar(struct shf
*shf
)
510 if (!(shf
->flags
& SHF_RD
))
511 internal_errorf("%s: flags %x", __func__
, shf
->flags
);
513 if (shf
->rnleft
== 0 && (shf_fillbuf(shf
) == EOF
|| shf
->rnleft
== 0))
519 /* Put a character back in the input stream. Returns the character if
520 * successful, EOF if there is no room.
523 shf_ungetc(int c
, struct shf
*shf
)
525 if (!(shf
->flags
& SHF_RD
))
526 internal_errorf("%s: flags %x", __func__
, shf
->flags
);
528 if ((shf
->flags
& SHF_ERROR
) || c
== EOF
||
529 (shf
->rp
== shf
->buf
&& shf
->rnleft
))
532 if ((shf
->flags
& SHF_WRITING
) && shf_emptybuf(shf
, EB_READSW
) == EOF
)
535 if (shf
->rp
== shf
->buf
)
536 shf
->rp
= shf
->buf
+ shf
->rbsize
;
537 if (shf
->flags
& SHF_STRING
) {
538 /* Can unget what was read, but not something different - we
539 * don't want to modify a string.
541 if (shf
->rp
[-1] != c
)
543 shf
->flags
&= ~SHF_EOF
;
548 shf
->flags
&= ~SHF_EOF
;
554 /* Write a character. Returns the character if successful, EOF if
555 * the char could not be written.
558 shf_putchar(int c
, struct shf
*shf
)
560 if (!(shf
->flags
& SHF_WR
))
561 internal_errorf("%s: flags %x", __func__
, shf
->flags
);
566 if (shf
->flags
& SHF_UNBUF
) {
571 internal_errorf("%s: no fd", __func__
);
572 if (shf
->flags
& SHF_ERROR
) {
576 while ((n
= write(shf
->fd
, &cc
, 1)) != 1)
578 if (errno
== EINTR
&&
579 !(shf
->flags
& SHF_INTERRUPT
))
581 shf
->flags
|= SHF_ERROR
;
586 /* Flush deals with strings and sticky errors */
587 if (shf
->wnleft
== 0 && shf_emptybuf(shf
, EB_GROW
) == EOF
)
596 /* Write a string. Returns the length of the string if successful, EOF if
597 * the string could not be written.
600 shf_puts(const char *s
, struct shf
*shf
)
605 return shf_write(s
, strlen(s
), shf
);
608 /* Write a buffer. Returns nbytes if successful, EOF if there is an error. */
610 shf_write(const char *buf
, int nbytes
, struct shf
*shf
)
612 int orig_nbytes
= nbytes
;
616 if (!(shf
->flags
& SHF_WR
))
617 internal_errorf("%s: flags %x", __func__
, shf
->flags
);
620 internal_errorf("%s: nbytes %d", __func__
, nbytes
);
622 /* Don't buffer if buffer is empty and we're writting a large amount. */
623 if ((ncopy
= shf
->wnleft
) &&
624 (shf
->wp
!= shf
->buf
|| nbytes
< shf
->wnleft
)) {
627 memcpy(shf
->wp
, buf
, ncopy
);
631 shf
->wnleft
-= ncopy
;
634 /* Flush deals with strings and sticky errors */
635 if (shf_emptybuf(shf
, EB_GROW
) == EOF
)
637 if (nbytes
> shf
->wbsize
) {
640 ncopy
-= nbytes
% shf
->wbsize
;
643 n
= write(shf
->fd
, buf
, ncopy
);
645 if (errno
== EINTR
&&
646 !(shf
->flags
& SHF_INTERRUPT
))
648 shf
->flags
|= SHF_ERROR
;
651 /* Note: fwrite(3S) returns 0 for
652 * errors - this doesn't */
660 memcpy(shf
->wp
, buf
, nbytes
);
662 shf
->wnleft
-= nbytes
;
670 shf_fprintf(struct shf
*shf
, const char *fmt
, ...)
676 n
= shf_vfprintf(shf
, fmt
, args
);
683 shf_snprintf(char *buf
, int bsize
, const char *fmt
, ...)
689 if (!buf
|| bsize
<= 0)
690 internal_errorf("%s: buf %lx, bsize %d",
691 __func__
, (long) buf
, bsize
);
693 shf_sopen(buf
, bsize
, SHF_WR
, &shf
);
695 n
= shf_vfprintf(&shf
, fmt
, args
);
697 shf_sclose(&shf
); /* null terminates */
702 shf_smprintf(const char *fmt
, ...)
707 shf_sopen(NULL
, 0, SHF_WR
|SHF_DYNAMIC
, &shf
);
709 shf_vfprintf(&shf
, fmt
, args
);
711 return shf_sclose(&shf
); /* null terminates */
714 #define FL_HASH 0x001 /* `#' seen */
715 #define FL_PLUS 0x002 /* `+' seen */
716 #define FL_RIGHT 0x004 /* `-' seen */
717 #define FL_BLANK 0x008 /* ` ' seen */
718 #define FL_SHORT 0x010 /* `h' seen */
719 #define FL_LONG 0x020 /* `l' seen */
720 #define FL_LLONG 0x040 /* `ll' seen */
721 #define FL_ZERO 0x080 /* `0' seen */
722 #define FL_DOT 0x100 /* '.' seen */
723 #define FL_UPPER 0x200 /* format character was uppercase */
724 #define FL_NUMBER 0x400 /* a number was formated %[douxefg] */
727 shf_vfprintf(struct shf
*shf
, const char *fmt
, va_list args
)
731 int field
, precision
;
734 unsigned long long llnum
;
735 /* %#o produces the longest output */
736 char numbuf
[(BITS(long long) + 2) / 3 + 1];
737 /* this stuff for dealing with the buffer */
743 while ((c
= *fmt
++)) {
750 * This will accept flags/fields in any order - not
751 * just the order specified in printf(3), but this is
752 * the way _doprnt() seems to work (on bsd and sysV).
753 * The only restriction is that the format character must
756 flags
= field
= precision
= 0;
757 for ( ; (c
= *fmt
++) ; ) {
776 if (!(flags
& FL_DOT
))
786 tmp
= va_arg(args
, int);
789 else if ((field
= tmp
) < 0) {
809 while (c
= *fmt
++, digit(c
))
810 tmp
= tmp
* 10 + c
- '0';
812 if (tmp
< 0) /* overflow? */
826 if (!c
) /* nasty format */
829 if (c
>= 'A' && c
<= 'Z') {
835 case 'p': /* pointer */
836 flags
&= ~(FL_LLONG
| FL_SHORT
);
845 s
= &numbuf
[sizeof(numbuf
)];
846 if (flags
& FL_LLONG
)
847 llnum
= va_arg(args
, unsigned long long);
848 else if (flags
& FL_LONG
) {
849 if (c
== 'd' || c
== 'i')
850 llnum
= va_arg(args
, long);
852 llnum
= va_arg(args
, unsigned long);
854 if (c
== 'd' || c
== 'i')
855 llnum
= va_arg(args
, int);
857 llnum
= va_arg(args
, unsigned int);
862 if (0 > (long long) llnum
)
863 llnum
= - (long long) llnum
, tmp
= 1;
870 *--s
= llnum
% 10 + '0';
877 else if (flags
& FL_PLUS
)
879 else if (flags
& FL_BLANK
)
886 *--s
= (llnum
& 0x7) + '0';
890 if ((flags
& FL_HASH
) && *s
!= '0')
897 const char *digits
= (flags
& FL_UPPER
) ?
901 *--s
= digits
[llnum
& 0xf];
905 if (flags
& FL_HASH
) {
906 *--s
= (flags
& FL_UPPER
) ? 'X' : 'x';
911 len
= &numbuf
[sizeof(numbuf
)] - s
;
912 if (flags
& FL_DOT
) {
913 if (precision
> len
) {
917 precision
= len
; /* no loss */
922 if (!(s
= va_arg(args
, char *)))
929 numbuf
[0] = va_arg(args
, int);
943 * At this point s should point to a string that is
944 * to be formatted, and len should be the length of the
947 if (!(flags
& FL_DOT
) || len
< precision
)
949 if (field
> precision
) {
951 if (!(flags
& FL_RIGHT
)) {
953 /* skip past sign or 0x when padding with 0 */
954 if ((flags
& FL_ZERO
) && (flags
& FL_NUMBER
)) {
955 if (*s
== '+' || *s
== '-' || *s
==' ') {
960 } else if (*s
== '0') {
964 if (--precision
> 0 &&
965 (*s
| 0x20) == 'x') {
974 c
= flags
& FL_ZERO
? '0' : ' ';
977 for ( ; field
< 0 ; field
++)
986 nwritten
+= precision
;
987 for ( ; precision
-- > 0 ; s
++)
992 for ( ; field
> 0 ; --field
)
997 return shf_error(shf
) ? EOF
: nwritten
;