Created
December 4, 2014 04:32
-
-
Save chai2010/e8163ae2b8123fa6b564 to your computer and use it in GitHub Desktop.
迷你printf实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Copyright (C) 1999 Lucent Technologies */ | |
| /* Excerpted from 'The Practice of Programming' */ | |
| /* by Brian W. Kernighan and Rob Pike */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "eprintf.h" | |
| #include <stdarg.h> | |
| #include <string.h> | |
| #include <errno.h> | |
| static char *name = NULL; /* program name for messages */ | |
| /* eprintf: print error message and exit */ | |
| void eprintf(char *fmt, ...) | |
| { | |
| va_list args; | |
| fflush(stdout); | |
| if (progname() != NULL) | |
| fprintf(stderr, "%s: ", progname()); | |
| va_start(args, fmt); | |
| vfprintf(stderr, fmt, args); | |
| va_end(args); | |
| if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') | |
| fprintf(stderr, " %s", strerror(errno)); | |
| fprintf(stderr, "\n"); | |
| exit(2); /* conventional value for failed execution */ | |
| } | |
| /* weprintf: print warning message */ | |
| void weprintf(char *fmt, ...) | |
| { | |
| va_list args; | |
| fflush(stdout); | |
| fprintf(stderr, "warning: "); | |
| if (progname() != NULL) | |
| fprintf(stderr, "%s: ", progname()); | |
| va_start(args, fmt); | |
| vfprintf(stderr, fmt, args); | |
| va_end(args); | |
| if (fmt[0] != '\0' && fmt[strlen(fmt)-1] == ':') | |
| fprintf(stderr, " %s\n", strerror(errno)); | |
| else | |
| fprintf(stderr, "\n"); | |
| } | |
| /* emalloc: malloc and report if error */ | |
| void *emalloc(size_t n) | |
| { | |
| void *p; | |
| p = malloc(n); | |
| if (p == NULL) | |
| eprintf("malloc of %u bytes failed:", n); | |
| return p; | |
| } | |
| /* erealloc: realloc and report if error */ | |
| void *erealloc(void *vp, size_t n) | |
| { | |
| void *p; | |
| p = realloc(vp, n); | |
| if (p == NULL) | |
| eprintf("realloc of %u bytes failed:", n); | |
| return p; | |
| } | |
| /* estrdup: duplicate a string, report if error */ | |
| char *estrdup(char *s) | |
| { | |
| char *t; | |
| t = (char *) malloc(strlen(s)+1); | |
| if (t == NULL) | |
| eprintf("estrdup(\"%.20s\") failed:", s); | |
| strcpy(t, s); | |
| return t; | |
| } | |
| /* progname: return stored name of program */ | |
| char *progname(void) | |
| { | |
| return name; | |
| } | |
| /* setprogname: set stored name of program */ | |
| void setprogname(char *str) | |
| { | |
| name = estrdup(str); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Copyright (C) 1999 Lucent Technologies */ | |
| /* Excerpted from 'The Practice of Programming' */ | |
| /* by Brian W. Kernighan and Rob Pike */ | |
| /* eprintf.h: error wrapper functions */ | |
| extern void eprintf(char *, ...); | |
| extern void weprintf(char *, ...); | |
| extern char *estrdup(char *); | |
| extern void *emalloc(size_t); | |
| extern void *erealloc(void *, size_t); | |
| extern char *progname(void); | |
| extern void setprogname(char *); | |
| #define NELEMS(a) (sizeof(a) / sizeof(a[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment