/* * Implemented strerror_r in Solaris 10 to comply with SUSv3 2001. */ int _strerror_r(int errnum, char *strerrbuf, size_t buflen) { char *buf; int ret = 0; if (errnum < _sys_num_err && errnum >= 0) { buf = _libc_gettext((char *)&_sys_errs[_sys_index[errnum]]); } else { buf = _libc_gettext("Unknown error"); ret = errno = EINVAL; } /* * At compile time, there is no way to determine the max size of * language-dependent error message. */ if (buflen < (strlen(buf) + 1)) { ret = errno = ERANGE; } else { (void) strcpy(strerrbuf, buf); } return (ret); }
char * _strerror(int errnum) { if (errnum < _sys_num_err && errnum >= 0) return (_libc_gettext((char *)&_sys_errs[_sys_index[errnum]])); errno = EINVAL; return (_libc_gettext("Unknown error")); }
void psiginfo(const siginfo_t *sip, const char *s) { char buf[256]; char *c; const struct siginfolist *listp; if (sip == 0) return; if (sip->si_code <= 0) { (void) snprintf(buf, sizeof (buf), _libc_gettext("%s : %s ( from process %d )\n"), s, strsignal(sip->si_signo), sip->si_pid); } else if (((listp = &_sys_siginfolist[sip->si_signo-1]) != NULL) && sip->si_code <= listp->nsiginfo) { c = _libc_gettext(listp->vsiginfo[sip->si_code-1]); switch (sip->si_signo) { case SIGSEGV: case SIGBUS: case SIGILL: case SIGFPE: (void) snprintf(buf, sizeof (buf), _libc_gettext("%s : %s ( [%p] %s)\n"), s, strsignal(sip->si_signo), sip->si_addr, c); break; default: (void) snprintf(buf, sizeof (buf), _libc_gettext("%s : %s (%s)\n"), s, strsignal(sip->si_signo), c); break; } } else { (void) snprintf(buf, sizeof (buf), _libc_gettext("%s : %s\n"), s, strsignal(sip->si_signo)); } (void) write(2, buf, strlen(buf)); }
static const entry_t * get_pri_dirs(void) { int fd, state; size_t sz; ssize_t size; struct stat64 sb; char *buf, *ebuf; entry_t *vec; /* * if no /etc/ttysrch, use defaults */ if ((fd = open(TTYSRCH, 0)) < 0) return (def_srch_dirs); if (fstat64(fd, &sb) < 0) { (void) close(fd); return (def_srch_dirs); } sz = (size_t)sb.st_size; if (dir_vec != NULL && sz == dir_size && sb.st_mtim.tv_sec == dir_mtim.tv_sec && sb.st_mtim.tv_nsec == dir_mtim.tv_nsec) { /* * size & modification time match * no need to reread TTYSRCH * just return old pointer */ (void) close(fd); return (dir_vec); } buf = realloc(dir_buf, sz + 1); if (buf != NULL) { dir_buf = buf; size = read(fd, dir_buf, sz); } (void) close(fd); if (buf == NULL || size < 0) { if (dir_vec != NULL) { free(dir_vec); dir_vec = NULL; } return ((entry_t *)def_srch_dirs); } dir_size = sz; dir_mtim = sb.st_mtim; /* * ensure newline termination for buffer. Add an extra * entry to dir_vec for null terminator */ ebuf = &dir_buf[size]; *ebuf++ = '\n'; for (sz = 1, buf = dir_buf; buf < ebuf; ++buf) if (*buf == '\n') ++sz; sz *= sizeof (*dir_vec); vec = realloc(dir_vec, sz); if (vec == NULL) { if (dir_vec != NULL) { free(dir_vec); dir_vec = NULL; } return (def_srch_dirs); } dir_vec = vec; state = START_STATE; for (buf = dir_buf; buf < ebuf; ++buf) { switch (state) { case START_STATE: if (*buf == COMMENT_CHAR) { state = COMMENT_STATE; break; } if (!isspace(*buf)) /* skip leading white space */ state = DIRNAME_STATE; vec->name = buf; vec->flags = 0; break; case COMMENT_STATE: if (*buf == EOLN_CHAR) state = START_STATE; break; case DIRNAME_STATE: if (*buf == EOLN_CHAR) { state = CHECK_STATE; *buf = '\0'; } else if (isspace(*buf)) { /* skip trailing white space */ state = FLAG_STATE; *buf = '\0'; } break; case FLAG_STATE: switch (*buf) { case 'M': vec->flags |= MATCH_MM; break; case 'F': vec->flags |= MATCH_FS; break; case 'I': vec->flags |= MATCH_INO; break; case EOLN_CHAR: state = CHECK_STATE; break; } break; case CHECK_STATE: if (strncmp(vec->name, DEV, strlen(DEV)) != 0) { int tfd = open("/dev/console", O_WRONLY); if (tfd >= 0) { char buf[256]; /* LINTED variable format specifier */ (void) snprintf(buf, sizeof (buf), _libc_gettext( "ERROR: Entry '%s' in /etc/ttysrch ignored.\n"), vec->name); (void) write(tfd, buf, strlen(buf)); (void) close(tfd); } } else { char *slash; slash = vec->name + strlen(vec->name) - 1; while (*slash == '/') *slash-- = '\0'; if (vec->flags == 0) vec->flags = MATCH_ALL; vec++; } state = START_STATE; /* * This state does not consume a character, so * reposition the pointer. */ buf--; break; } } vec->name = NULL; return (dir_vec); }
/* * External function entry point. */ int getopt(int argc, char *const *argv, const char *optstring) { char c; char *cp; int longopt; char *longoptarg; /* * Has the end of the options been encountered? The following * implements the SUS requirements: * * If, when getopt() is called: * argv[optind] is a null pointer * *argv[optind] is not the character '-' * argv[optind] points to the string "-" * getopt() returns -1 without changing optind. If * argv[optind] points to the string "--" * getopt() returns -1 after incrementing optind. */ if (_sp == 1) { if (optind >= argc || argv[optind][0] != '-' || argv[optind] == NULL || argv[optind][1] == '\0') return (EOF); else if (strcmp(argv[optind], "--") == NULL) { optind++; return (EOF); } } /* * Getting this far indicates that an option has been encountered. * Note that the syntax of optstring applies special meanings to * the characters ':' and '(', so they are not permissible as * option letters. A special meaning is also applied to the ')' * character, but its meaning can be determined from context. * Note that the specification only requires that the alnum * characters be accepted. * * If the second character of the argument is a '-' this must be * a long-option, otherwise it must be a short option. Scan for * the option in optstring by the appropriate algorithm. Either * scan will return a pointer to the short-option character in * optstring if the option is found and NULL otherwise. * * For an unrecognized long-option, optopt will equal 0, but * since long-options can't aggregate the failing option can * be identified by argv[optind-1]. */ optopt = c = (unsigned char)argv[optind][_sp]; optarg = NULL; longopt = (_sp == 1 && c == '-'); if (!(longopt ? ((cp = parselong(optstring, argv[optind]+2, &longoptarg)) != NULL) : ((cp = parseshort(optstring, c)) != NULL))) { /* LINTED: variable format specifier */ ERR(_libc_gettext("%s: illegal option -- %s\n"), c, (longopt ? optind : 0)); /* * Note: When the long option is unrecognized, optopt * will be '-' here, which matches the specification. */ if (argv[optind][++_sp] == '\0' || longopt) { optind++; _sp = 1; } return ('?'); } optopt = c = *cp; /* * A valid option has been identified. If it should have an * option-argument, process that now. SUS defines the setting * of optarg as follows: * * 1. If the option was the last character in the string pointed to * by an element of argv, then optarg contains the next element * of argv, and optind is incremented by 2. If the resulting * value of optind is not less than argc, this indicates a * missing option-argument, and getopt() returns an error * indication. * * 2. Otherwise, optarg points to the string following the option * character in that element of argv, and optind is incremented * by 1. * * The second clause allows -abcd (where b requires an option-argument) * to be interpreted as "-a -b cd". * * Note that the option-argument can legally be an empty string, * such as: * command --option= operand * which explicitly sets the value of --option to nil */ if (*(cp + 1) == ':') { /* The option takes an argument */ if (!longopt && argv[optind][_sp+1] != '\0') { optarg = &argv[optind++][_sp+1]; } else if (longopt && longoptarg) { /* * The option argument was explicitly set to * the empty string on the command line (--option=) */ optind++; optarg = longoptarg; } else if (++optind >= argc) { /* LINTED: variable format specifier */ ERR(_libc_gettext("%s: option requires an argument" \ " -- %s\n"), c, (longopt ? optind - 1 : 0)); _sp = 1; optarg = NULL; return (optstring[0] == ':' ? ':' : '?'); } else optarg = argv[optind++]; _sp = 1; } else { /* The option does NOT take an argument */ if (longopt && (longoptarg != NULL)) { /* User supplied an arg to an option that takes none */ /* LINTED: variable format specifier */ ERR(_libc_gettext( "%s: option doesn't take an argument -- %s\n"), 0, (longopt ? optind : 0)); optarg = longoptarg = NULL; c = '?'; } if (longopt || argv[optind][++_sp] == '\0') { _sp = 1; optind++; } optarg = NULL; } return (c); } /* getopt() */