[11904] | 1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
| 2 | * |
---|
| 3 | * Permission to use, copy, modify, and distribute this |
---|
| 4 | * software and its documentation for any purpose and without |
---|
| 5 | * fee is hereby granted, provided that the above copyright |
---|
| 6 | * notice appear in all copies and that both that copyright |
---|
| 7 | * notice and this permission notice appear in supporting |
---|
| 8 | * documentation, and that the name of M.I.T. not be used in |
---|
| 9 | * advertising or publicity pertaining to distribution of the |
---|
| 10 | * software without specific, written prior permission. |
---|
| 11 | * M.I.T. makes no representations about the suitability of |
---|
| 12 | * this software for any purpose. It is provided "as is" |
---|
| 13 | * without express or implied warranty. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /* This file implements utility functions used by larvnetd. */ |
---|
| 17 | |
---|
| 18 | static const char rcsid[] = "$Id: util.c,v 1.1 1998-09-01 20:57:48 ghudson Exp $"; |
---|
| 19 | |
---|
| 20 | #include <stdio.h> |
---|
| 21 | #include <stdlib.h> |
---|
| 22 | #include <string.h> |
---|
| 23 | #include <syslog.h> |
---|
| 24 | #include "larvnetd.h" |
---|
| 25 | |
---|
| 26 | void *emalloc(size_t size) |
---|
| 27 | { |
---|
| 28 | void *ptr; |
---|
| 29 | |
---|
| 30 | ptr = malloc(size); |
---|
| 31 | if (!ptr) |
---|
| 32 | { |
---|
| 33 | syslog(LOG_ALERT, "emalloc: malloc size %lu failed, aborting", |
---|
| 34 | (unsigned long) size); |
---|
| 35 | exit(1); |
---|
| 36 | } |
---|
| 37 | return ptr; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void *erealloc(void *ptr, size_t size) |
---|
| 41 | { |
---|
| 42 | ptr = realloc(ptr, size); |
---|
| 43 | if (!ptr) |
---|
| 44 | { |
---|
| 45 | syslog(LOG_ALERT, "erealloc: realloc size %lu failed, aborting", |
---|
| 46 | (unsigned long) size); |
---|
| 47 | exit(1); |
---|
| 48 | } |
---|
| 49 | return ptr; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | char *estrdup(const char *s) |
---|
| 53 | { |
---|
| 54 | char *new_s; |
---|
| 55 | |
---|
| 56 | new_s = emalloc(strlen(s) + 1); |
---|
| 57 | strcpy(new_s, s); |
---|
| 58 | return new_s; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | char *estrndup(const char *s, size_t n) |
---|
| 62 | { |
---|
| 63 | char *new_s; |
---|
| 64 | |
---|
| 65 | new_s = emalloc(n + 1); |
---|
| 66 | memcpy(new_s, s, n); |
---|
| 67 | new_s[n] = 0; |
---|
| 68 | return new_s; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /* Read a line from a file into a dynamically allocated buffer, |
---|
| 72 | * zeroing the trailing newline if there is one. The calling routine |
---|
| 73 | * may call read_line multiple times with the same buf and bufsize |
---|
| 74 | * pointers; *buf will be reallocated and *bufsize adjusted as |
---|
| 75 | * appropriate. The initial value of *buf should be NULL. After the |
---|
| 76 | * calling routine is done reading lines, it should free *buf. This |
---|
| 77 | * function returns 0 if a line was successfully read, 1 if the file |
---|
| 78 | * ended, and -1 if there was an I/O error. |
---|
| 79 | */ |
---|
| 80 | |
---|
| 81 | int read_line(FILE *fp, char **buf, int *bufsize) |
---|
| 82 | { |
---|
| 83 | char *newbuf; |
---|
| 84 | int offset = 0, len; |
---|
| 85 | |
---|
| 86 | if (*buf == NULL) |
---|
| 87 | { |
---|
| 88 | *buf = emalloc(128); |
---|
| 89 | *bufsize = 128; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | while (1) |
---|
| 93 | { |
---|
| 94 | if (!fgets(*buf + offset, *bufsize - offset, fp)) |
---|
| 95 | return (offset != 0) ? 0 : (ferror(fp)) ? -1 : 1; |
---|
| 96 | len = offset + strlen(*buf + offset); |
---|
| 97 | if ((*buf)[len - 1] == '\n') |
---|
| 98 | { |
---|
| 99 | (*buf)[len - 1] = 0; |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | offset = len; |
---|
| 103 | |
---|
| 104 | /* Allocate more space. */ |
---|
| 105 | newbuf = erealloc(*buf, *bufsize * 2); |
---|
| 106 | *buf = newbuf; |
---|
| 107 | *bufsize *= 2; |
---|
| 108 | } |
---|
| 109 | } |
---|