1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 /*
19 FUNCTION
20 <<perror>>---print an error message on standard error
21 
22 INDEX
23 	perror
24 INDEX
25 	_perror_r
26 
27 SYNOPSIS
28 	#include <stdio.h>
29 	void perror(char *<[prefix]>);
30 
31 	void perror( char *<[prefix]>);
32 
33 DESCRIPTION
34 Use <<perror>> to print (on standard error) an error message
35 corresponding to the current value of the global variable <<errno>>.
36 Unless you use <<NULL>> as the value of the argument <[prefix]>, the
37 error message will begin with the string at <[prefix]>, followed by a
38 colon and a space (<<: >>). The remainder of the error message is one
39 of the strings described for <<strerror>>.
40 
41 The alternate function <<_perror_r>> is a reentrant version.  The
42 extra argument <[reent]> is a pointer to a reentrancy structure.
43 
44 RETURNS
45 <<perror>> returns no result.
46 
47 PORTABILITY
48 ANSI C requires <<perror>>, but the strings issued vary from one
49 implementation to another.
50 
51 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
52 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
53 */
54 
55 #define _DEFAULT_SOURCE
56 #define _DEFAULT_SOURCE
57 #include <_ansi.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include "local.h"
61 #include <errno.h>
62 
63 #define WRITE_STR(str) \
64 { \
65   const char *p = (str); \
66   size_t len = strlen (p); \
67   while (len) \
68     { \
69       ssize_t len1 = write (fileno (fp), p, len); \
70       if (len1 < 0) \
71 	break; \
72       len -= len1; \
73       p += len1; \
74     } \
75 }
76 
77 void
perror(const char * s)78 perror (
79        const char *s)
80 {
81   char *error;
82   int dummy;
83   FILE *fp = stderr;
84 
85   CHECK_INIT (ptr, fp);
86 
87   _newlib_flockfile_start(fp);
88   fflush ( fp);
89   if (s != NULL && *s != '\0')
90     {
91       WRITE_STR (s);
92       WRITE_STR (": ");
93     }
94 
95   if ((error = _strerror_r (_REENT_ERRNO(ptr), 1, &dummy)) != NULL)
96     WRITE_STR (error);
97 
98 #ifdef __SCLE
99   WRITE_STR ((fp->_flags & __SCLE) ? "\r\n" : "\n");
100 #else
101   WRITE_STR ("\n");
102 #endif
103   fp->_flags &= ~__SOFF;
104   _newlib_flockfile_end(fp);
105 }
106