allascii.c revision 1.2
1/* $NetBSD: allascii.c,v 1.2 2017/02/14 01:16:48 christos Exp $ */ 2 3/*++ 4/* NAME 5/* allascii 3 6/* SUMMARY 7/* predicate if string is all ASCII 8/* SYNOPSIS 9/* #include <stringops.h> 10/* 11/* int allascii(buffer) 12/* const char *buffer; 13/* 14/* int allascii_len(buffer, len) 15/* const char *buffer; 16/* ssize_t len; 17/* DESCRIPTION 18/* allascii() determines if its argument is an all-ASCII string. 19/* 20/* Arguments: 21/* .IP buffer 22/* The null-terminated input string. 23/* .IP len 24/* The string length, -1 to determine the length dynamically. 25/* LICENSE 26/* .ad 27/* .fi 28/* The Secure Mailer license must be distributed with this software. 29/* AUTHOR(S) 30/* Wietse Venema 31/* IBM T.J. Watson Research 32/* P.O. Box 704 33/* Yorktown Heights, NY 10598, USA 34/*--*/ 35 36/* System library. */ 37 38#include <sys_defs.h> 39#include <ctype.h> 40#include <string.h> 41 42/* Utility library. */ 43 44#include "stringops.h" 45 46/* allascii_len - return true if string is all ASCII */ 47 48int allascii_len(const char *string, ssize_t len) 49{ 50 const char *cp; 51 int ch; 52 53 if (len < 0) 54 len = strlen(string); 55 if (len == 0) 56 return (0); 57 for (cp = string; cp < string + len 58 && (ch = *(unsigned char *) cp) != 0; cp++) 59 if (!ISASCII(ch)) 60 return (0); 61 return (1); 62} 63