函数设计原则:
函数应该是无状态的,就是第一次调用和第二次调用是一样的。
getchar返回值是int型。
优秀代码欣赏:Eclipse代码
1 /******************************************************************************* 2 * Copyright (c) 2000, 2005 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Kevin Cornell (Rational Software Corporation) 11 *******************************************************************************/ 12 13 /* Eclipse Launcher Utility Methods */ 14 15 #include "eclipseOS.h" 16 #include "eclipseCommon.h" 17 #include "eclipseUtil.h" 18 19 #include <string.h> 20 #include <stdlib.h> 21 #include <stdio.h> 22 #include <sys/stat.h> 23 #ifdef _WIN32 24 #include <direct.h> 25 #else 26 #include <unistd.h> 27 #include <strings.h> 28 #endif 29 30 #define MAX_LINE_LENGTH 256 31 32 /* Is the given VM J9 */ 33 int isJ9VM( _TCHAR* vm ) 34 { 35 _TCHAR * ch = NULL, *ch2 = NULL; 36 int res = 0; 37 38 if (vm == NULL) 39 return 0; 40 41 ch = lastDirSeparator( vm ); 42 if (isVMLibrary(vm)) { 43 /* a library, call it j9 if the parent dir is j9vm */ 44 if(ch == NULL) 45 return 0; 46 ch[0] = 0; 47 ch2 = lastDirSeparator(vm); 48 if(ch2 != NULL) { 49 res = (_tcsicmp(ch2 + 1, _T_ECLIPSE("j9vm")) == 0); 50 } 51 ch[0] = dirSeparator; 52 return res; 53 } else { 54 if (ch == NULL) 55 ch = vm; 56 else 57 ch++; 58 return (_tcsicmp( ch, _T_ECLIPSE("j9") ) == 0); 59 } 60 } 61 62 int checkProvidedVMType( _TCHAR* vm ) 63 { 64 _TCHAR* ch = NULL; 65 struct _stat stats; 66 67 if (vm == NULL) return VM_NOTHING; 68 69 if (_tstat(vm, &stats) == 0 && (stats.st_mode & S_IFDIR) != 0) { 70 /* directory */ 71 return VM_DIRECTORY; 72 } 73 74 ch = _tcsrchr( vm, _T_ECLIPSE('.') ); 75 if(ch == NULL) 76 return VM_OTHER; 77 78 #ifdef _WIN32 79 if (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0) 80 #else 81 if ((_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0)) 82 #endif 83 { 84 return VM_LIBRARY; 85 } 86 87 if (_tcsicmp(ch, _T_ECLIPSE(".ee")) == 0) 88 return VM_EE_PROPS; 89 90 return VM_OTHER; 91 } 92 93 /* 94 * pathList is a pathSeparator separated list of paths, run each through 95 * checkPath and recombine the results. 96 * New memory is always allocated for the result 97 */ 98 _TCHAR * checkPathList( _TCHAR* pathList, _TCHAR* programDir, int reverseOrder) { 99 _TCHAR * c1, *c2; 100 _TCHAR * checked, *result; 101 size_t checkedLength = 0, resultLength = 0; 102 size_t bufferLength = _tcslen(pathList); 103 104 result = malloc(bufferLength * sizeof(_TCHAR)); 105 c1 = pathList; 106 while (c1 != NULL && *c1 != _T_ECLIPSE('