1: /* $Header: getcourierent.c,v 2.0 85/11/21 07:22:07 jqj Exp $ */ 2: 3: #include <stdio.h> 4: #include "courierdb.h" 5: #include <ctype.h> 6: 7: /* 8: * 9: */ 10: #ifdef CSERVICES 11: static char *COURIERDB = CSERVICES; 12: #else 13: static char *COURIERDB = "/usr/new/lib/xnscourier/Courierservices"; 14: #endif 15: static FILE *courierdbf = NULL; 16: static char line[BUFSIZ+1]; 17: static struct courierdbent service; 18: int _courier_stayopen; 19: static char *skipspace(), *skipitem(); 20: 21: setcourierdbent(f) 22: int f; 23: { 24: if (courierdbf != NULL) 25: rewind(courierdbf); 26: _courier_stayopen |= f; 27: } 28: 29: endcourierdbent() 30: { 31: if (courierdbf != NULL) { 32: fclose(courierdbf); 33: courierdbf = NULL; 34: } 35: _courier_stayopen = 0; 36: } 37: 38: struct courierdbent * 39: getcourierdbent() 40: { 41: char *p; 42: register char *cp, c; 43: 44: if (courierdbf == NULL 45: && (courierdbf = fopen(COURIERDB, "r" )) == NULL) 46: return (NULL); 47: 48: do { 49: if ((p = fgets(line, BUFSIZ, courierdbf)) == NULL) 50: return (NULL); 51: p = skipspace(p); 52: cp = p; /* end of whitespace */ 53: while ((c = *cp) != '\0' && c != '\n' && c != '#') 54: cp++; 55: *cp = '\0'; /* end of data */ 56: } while (*p == '\0'); 57: 58: service.cr_programname = p; /* a string */ 59: cp = skipitem(p); 60: if (*cp != '\0') { 61: *cp = '\0'; 62: cp = skipspace(++cp); 63: } 64: service.cr_programnumber = (unsigned long) atol(cp); /* a long */ 65: cp = skipitem(cp); cp = skipspace(cp); 66: service.cr_version = (unsigned short) atoi(cp); /* an int */ 67: cp = skipitem(cp); cp = skipspace(cp); 68: service.cr_description = (*cp) ? cp : (char*) 0; 69: cp = skipitem(cp); 70: if (*cp != '\0') { 71: *cp = '\0'; 72: cp = skipspace(++cp); 73: } 74: service.cr_serverbin = (*cp) ? cp : (char*) 0; 75: cp = skipitem(cp); 76: if (*cp != '\0') { 77: *cp = '\0'; 78: /* etc. for more fields */ 79: } 80: return (&service); 81: } 82: 83: static char * skipspace(p) 84: /* move the pointer past leading whitespace, returning the updated ptr */ 85: register char *p; 86: { 87: register char c; 88: while ((c = *p) == ' ' || c == '\t') 89: p++; 90: return(p); 91: } 92: 93: static char* skipitem(p) 94: /* move the pointer, p, past non-whitespace */ 95: register char *p; 96: { 97: register char c; 98: while ((c = *p) && c != ' ' && c != '\t') 99: p++; 100: return(p); 101: } 102: 103: setcourierdbfile(file) 104: char *file; 105: { 106: COURIERDB = file; 107: }