github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/lib9/errstr.c (about)

     1  // +build !plan9
     2  
     3  /*
     4  Plan 9 from User Space src/lib9/errstr.c
     5  http://code.swtch.com/plan9port/src/tip/src/lib9/errstr.c
     6  
     7  Copyright 2001-2007 Russ Cox.  All Rights Reserved.
     8  
     9  Permission is hereby granted, free of charge, to any person obtaining a copy
    10  of this software and associated documentation files (the "Software"), to deal
    11  in the Software without restriction, including without limitation the rights
    12  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    13  copies of the Software, and to permit persons to whom the Software is
    14  furnished to do so, subject to the following conditions:
    15  
    16  The above copyright notice and this permission notice shall be included in
    17  all copies or substantial portions of the Software.
    18  
    19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    24  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    25  THE SOFTWARE.
    26  */
    27  
    28  /*
    29   * We assume there's only one error buffer for the whole system.
    30   * If you use ffork, you need to provide a _syserrstr.  Since most
    31   * people will use libthread (which provides a _syserrstr), this is
    32   * okay.
    33   */
    34  
    35  #include <u.h>
    36  #include <errno.h>
    37  #include <libc.h>
    38  
    39  enum
    40  {
    41  	EPLAN9 = 0x19283745
    42  };
    43  
    44  char *(*_syserrstr)(void);
    45  static char xsyserr[ERRMAX];
    46  static char*
    47  getsyserr(void)
    48  {
    49  	char *s;
    50  
    51  	s = nil;
    52  	if(_syserrstr)
    53  		s = (*_syserrstr)();
    54  	if(s == nil)
    55  		s = xsyserr;
    56  	return s;
    57  }
    58  
    59  int
    60  errstr(char *err, uint n)
    61  {
    62  	char tmp[ERRMAX];
    63  	char *syserr;
    64  
    65  	strecpy(tmp, tmp+ERRMAX, err);
    66  	rerrstr(err, n);
    67  	syserr = getsyserr();
    68  	strecpy(syserr, syserr+ERRMAX, tmp);
    69  	errno = EPLAN9;
    70  	return 0;
    71  }
    72  
    73  void
    74  rerrstr(char *err, uint n)
    75  {
    76  	char *syserr;
    77  
    78  	syserr = getsyserr();
    79  	if(errno == EINTR)
    80  		strcpy(syserr, "interrupted");
    81  	else if(errno != EPLAN9)
    82  		strcpy(syserr, strerror(errno));
    83  	strecpy(err, err+n, syserr);
    84  }
    85  
    86  /* replaces __errfmt in libfmt */
    87  
    88  int
    89  __errfmt(Fmt *f)
    90  {
    91  	if(errno == EPLAN9)
    92  		return fmtstrcpy(f, getsyserr());
    93  	return fmtstrcpy(f, strerror(errno));
    94  }
    95  
    96  void
    97  werrstr(char *fmt, ...)
    98  {
    99  	va_list arg;
   100  	char buf[ERRMAX];
   101  
   102  	va_start(arg, fmt);
   103  	vseprint(buf, buf+ERRMAX, fmt, arg);
   104  	va_end(arg);
   105  	errstr(buf, ERRMAX);
   106  }
   107