|
|
1.1 ! root 1: /****************************************************************************** ! 2: * Copyright (c) 2004, 2008 IBM Corporation ! 3: * All rights reserved. ! 4: * This program and the accompanying materials ! 5: * are made available under the terms of the BSD License ! 6: * which accompanies this distribution, and is available at ! 7: * http://www.opensource.org/licenses/bsd-license.php ! 8: * ! 9: * Contributors: ! 10: * IBM Corporation - initial implementation ! 11: *****************************************************************************/ ! 12: ! 13: #include <types.h> ! 14: #include <ctype.h> ! 15: #include <stdlib.h> ! 16: #include <string.h> ! 17: ! 18: /** ! 19: * Returns pointer of the n'th argument within a string. ! 20: * ! 21: * @param arg_str string with arguments, seperated with ',' ! 22: * @param index index of the requested arguments whithin arg_str ! 23: * @return pointer of argument[index] on success ! 24: * NULL if index is out of range ! 25: */ ! 26: const char * ! 27: get_arg_ptr(const char *arg_str, unsigned int index) ! 28: { ! 29: unsigned int i; ! 30: ! 31: for (i = 0; i < index; ++i) { ! 32: for (; *arg_str != ',' && *arg_str != 0; ++arg_str); ! 33: if (*arg_str == 0) ! 34: return 0; ! 35: ++arg_str; ! 36: } ! 37: return arg_str; ! 38: } ! 39: ! 40: /** ! 41: * Returns number of arguments within a string. ! 42: * ! 43: * @param arg_str string with arguments, seperated with ',' ! 44: * @return number of arguments ! 45: */ ! 46: unsigned int ! 47: get_args_count(const char *arg_str) ! 48: { ! 49: unsigned int count = 1; ! 50: ! 51: while ((arg_str = get_arg_ptr(arg_str, 1)) != 0) ! 52: ++count; ! 53: return count; ! 54: } ! 55: ! 56: /** ! 57: * Returns the length of the first argument. ! 58: * ! 59: * @param arg_str string with arguments, seperated with ',' ! 60: * @return length of first argument ! 61: */ ! 62: unsigned int ! 63: get_arg_length(const char *arg_str) ! 64: { ! 65: unsigned int i; ! 66: ! 67: for (i = 0; *arg_str != ',' && *arg_str != 0; ++i) ! 68: ++arg_str; ! 69: return i; ! 70: } ! 71: ! 72: /** ! 73: * Copy the n'th argument within a string into a buffer in respect ! 74: * to a limited buffer size ! 75: * ! 76: * @param arg_str string with arguments, seperated with ',' ! 77: * @param index index of the requested arguments whithin arg_str ! 78: * @param buffer pointer to the buffer ! 79: * @param length size of the buffer ! 80: * @return pointer of buffer on success ! 81: * NULL if index is out of range. ! 82: */ ! 83: char * ! 84: argncpy(const char *arg_str, unsigned int index, char *buffer, ! 85: unsigned int length) ! 86: { ! 87: const char *ptr = get_arg_ptr(arg_str, index); ! 88: unsigned int len; ! 89: ! 90: if (!ptr) ! 91: return 0; ! 92: len = get_arg_length(ptr); ! 93: if (!strncpy(buffer, ptr, length)) ! 94: return 0; ! 95: buffer[len] = 0; ! 96: return buffer; ! 97: } ! 98: ! 99: /** ! 100: * Converts "255.255.255.255" -> char[4] = { 0xff, 0xff, 0xff, 0xff } ! 101: * ! 102: * @param str string to be converted ! 103: * @param ip in case of SUCCESS - 32-bit long IP ! 104: in case of FAULT - zero ! 105: * @return TRUE - IP converted successfully; ! 106: * FALSE - error condition occurs (e.g. bad format) ! 107: */ ! 108: int ! 109: strtoip(const char *str, char ip[4]) ! 110: { ! 111: char octet[10]; ! 112: int res; ! 113: unsigned int i = 0, len; ! 114: ! 115: while (*str != 0) { ! 116: if (i > 3 || !isdigit(*str)) ! 117: return 0; ! 118: if (strstr(str, ".") != NULL) { ! 119: len = (int16_t) (strstr(str, ".") - str); ! 120: if (len >= 10) ! 121: return 0; ! 122: strncpy(octet, str, len); ! 123: octet[len] = 0; ! 124: str += len; ! 125: } else { ! 126: strncpy(octet, str, 9); ! 127: octet[9] = 0; ! 128: str += strlen(octet); ! 129: } ! 130: res = strtol(octet, NULL, 10); ! 131: if ((res > 255) || (res < 0)) ! 132: return 0; ! 133: ip[i] = (char) res; ! 134: i++; ! 135: if (*str == '.') ! 136: str++; ! 137: } ! 138: ! 139: if (i != 4) ! 140: return 0; ! 141: return -1; ! 142: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.