📄 utils.c
字号:
voidExpandCommandLine(pargc, pargv) int *pargc; char ***pargv;{ int i; for (i = 1; i < *pargc; i++) { if ( (0 == strcmp((*pargv)[i], "-config")) && (i < (*pargc - 1)) ) { InsertFileIntoCommandLine(pargc, pargv, i, *pargv, (*pargv)[i+1], /* filename */ *pargc - i - 2, *pargv + i + 2); i--; } }} /* end ExpandCommandLine */#endif#if defined(TCPCONN) || defined(STREAMSCONN)#ifndef WIN32#include <netdb.h>#endif#endif/* Implement a simple-minded font authorization scheme. The authorization name is "hp-hostname-1", the contents are simply the host name. */intset_font_authorizations(authorizations, authlen, client)char **authorizations;int *authlen;pointer client;{#define AUTHORIZATION_NAME "hp-hostname-1"#if defined(TCPCONN) || defined(STREAMSCONN) static char result[1024]; static char *p = NULL; if (p == NULL) { char hname[1024], *hnameptr; struct hostent *host; int len; gethostname(hname, 1024); host = gethostbyname(hname); if (host == NULL) hnameptr = hname; else hnameptr = host->h_name; p = result; *p++ = sizeof(AUTHORIZATION_NAME) >> 8; *p++ = sizeof(AUTHORIZATION_NAME) & 0xff; *p++ = (len = strlen(hnameptr) + 1) >> 8; *p++ = (len & 0xff); memmove(p, AUTHORIZATION_NAME, sizeof(AUTHORIZATION_NAME)); p += sizeof(AUTHORIZATION_NAME); memmove(p, hnameptr, len); p += len; } *authlen = p - result; *authorizations = result; return 1;#else /* TCPCONN */ return 0;#endif /* TCPCONN */}/* XALLOC -- X's internal memory allocator. Why does it return unsigned * long * instead of the more common char *? Well, if you read K&R you'll * see they say that alloc must return a pointer "suitable for conversion" * to whatever type you really want. In a full-blown generic allocator * there's no way to solve the alignment problems without potentially * wasting lots of space. But we have a more limited problem. We know * we're only ever returning pointers to structures which will have to * be long word aligned. So we are making a stronger guarantee. It might * have made sense to make Xalloc return char * to conform with people's * expectations of malloc, but this makes lint happier. */#ifndef INTERNAL_MALLOCunsigned long * Xalloc (amount) unsigned long amount;{#if !defined(__STDC__) && !defined(AMOEBA) char *malloc();#endif register pointer ptr; if ((long)amount <= 0) { return (unsigned long *)NULL; } /* aligned extra on long word boundary */ amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1);#ifdef MEMBUG if (!Must_have_memory && Memory_fail && ((random() % MEM_FAIL_SCALE) < Memory_fail)) return (unsigned long *)NULL;#endif if (ptr = (pointer)malloc(amount)) { return (unsigned long *)ptr; } if (Must_have_memory) FatalError("Out of memory"); return (unsigned long *)NULL;}/***************** * XNFalloc * "no failure" realloc, alternate interface to Xalloc w/o Must_have_memory *****************/unsigned long *XNFalloc (amount) unsigned long amount;{#if !defined(__STDC__) && !defined(AMOEBA) char *malloc();#endif register pointer ptr; if ((long)amount <= 0) { return (unsigned long *)NULL; } /* aligned extra on long word boundary */ amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1); ptr = (pointer)malloc(amount); if (!ptr) { FatalError("Out of memory"); } return ((unsigned long *)ptr);}/***************** * Xcalloc *****************/unsigned long *Xcalloc (amount) unsigned long amount;{ unsigned long *ret; ret = Xalloc (amount); if (ret) bzero ((char *) ret, (int) amount); return ret;}/***************** * Xrealloc *****************/unsigned long *Xrealloc (ptr, amount) register pointer ptr; unsigned long amount;{#if !defined(__STDC__) && !defined(AMOEBA) char *malloc(); char *realloc();#endif#ifdef MEMBUG if (!Must_have_memory && Memory_fail && ((random() % MEM_FAIL_SCALE) < Memory_fail)) return (unsigned long *)NULL;#endif if ((long)amount <= 0) { if (ptr && !amount) free(ptr); return (unsigned long *)NULL; } amount = (amount + (sizeof(long) - 1)) & ~(sizeof(long) - 1); if (ptr) ptr = (pointer)realloc((char *)ptr, amount); else ptr = (pointer)malloc(amount); if (ptr) return (unsigned long *)ptr; if (Must_have_memory) FatalError("Out of memory"); return (unsigned long *)NULL;} /***************** * XNFrealloc * "no failure" realloc, alternate interface to Xrealloc w/o Must_have_memory *****************/unsigned long *XNFrealloc (ptr, amount) register pointer ptr; unsigned long amount;{ if (( ptr = (pointer)Xrealloc( ptr, amount ) ) == NULL) { FatalError( "Out of memory" ); } return ((unsigned long *)ptr);}/***************** * Xfree * calls free *****************/ voidXfree(ptr) register pointer ptr;{ if (ptr) free((char *)ptr); }voidOsInitAllocator (){#ifdef MEMBUG static int been_here; /* Check the memory system after each generation */ if (been_here) CheckMemory (); else been_here = 1;#endif}#endifvoidAuditPrefix(f) char *f;{#ifdef X_NOT_STDC_ENV long tm;#else time_t tm;#endif char *autime, *s; if (*f != ' ') { time(&tm); autime = ctime(&tm); if (s = strchr(autime, '\n')) *s = '\0'; if (s = strrchr(argvGlobal[0], '/')) s++; else s = argvGlobal[0]; ErrorF("AUDIT: %s: %d %s: ", autime, getpid(), s); }}/*VARARGS1*/voidAuditF(#if NeedVarargsPrototypes char * f, ...)#else f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */ char *f; char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;#endif{#if NeedVarargsPrototypes va_list args;#endif AuditPrefix(f);#if NeedVarargsPrototypes va_start(args, f); VErrorF(f, args); va_end(args);#else ErrorF(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);#endif}/*VARARGS1*/voidFatalError(#if NeedVarargsPrototypes char *f, ...)#elsef, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */ char *f; char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;#endif{#if NeedVarargsPrototypes va_list args;#endif ErrorF("\nFatal server error:\n");#if NeedVarargsPrototypes va_start(args, f); VErrorF(f, args); va_end(args);#else ErrorF(f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);#endif ErrorF("\n");#ifdef DDXOSFATALERROR OsVendorFatalError();#endif AbortServer(); /*NOTREACHED*/}#if NeedVarargsPrototypesvoidVErrorF(f, args) char *f; va_list args;{ vfprintf(stderr, f, args);}#endif/*VARARGS1*/voidErrorF(#if NeedVarargsPrototypes char * f, ...)#else f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */ char *f; char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;#endif{#if NeedVarargsPrototypes va_list args; va_start(args, f); VErrorF(f, args); va_end(args);#else#ifdef AMOEBA mu_lock(&print_lock);#endif fprintf( stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);#ifdef AMOEBA mu_unlock(&print_lock);#endif#endif}#if !defined(WIN32) && !defined(__EMX__)/* * "safer" versions of system(3), popen(3) and pclose(3) which give up * all privs before running a command. * * This is based on the code in FreeBSD 2.2 libc. */intSystem(command) char *command;{ int pid, p; void (*csig)(); int status; if (!command) return(1);#ifdef SIGCHLD csig = signal(SIGCHLD, SIG_DFL);#endif ErrorF("System: `%s'\n", command); switch (pid = fork()) { case -1: /* error */ p = -1; case 0: /* child */ setgid(getgid()); setuid(getuid()); execl("/bin/sh", "sh", "-c", command, (char *)NULL); _exit(127); default: /* parent */ do { p = waitpid(pid, &status, 0); } while (p == -1 && errno == EINTR); }#ifdef SIGCHLD signal(SIGCHLD, csig);#endif return p == -1 ? -1 : status;}static struct pid { struct pid *next; FILE *fp; int pid;} *pidlist;pointerPopen(command, type) char *command; char *type;{ struct pid *cur; FILE *iop; int pdes[2], pid; void (*csig)(); if (command == NULL || type == NULL) return NULL; if ((*type != 'r' && *type != 'w') || type[1]) return NULL; if ((cur = (struct pid *)xalloc(sizeof(struct pid))) == NULL) return NULL; if (pipe(pdes) < 0) { xfree(cur); return NULL; } switch (pid = fork()) { case -1: /* error */ close(pdes[0]); close(pdes[1]); xfree(cur); return NULL; case 0: /* child */ setgid(getgid()); setuid(getuid()); if (*type == 'r') { if (pdes[1] != 1) { /* stdout */ dup2(pdes[1], 1); close(pdes[1]); } close(pdes[0]); } else { if (pdes[0] != 0) { /* stdin */ dup2(pdes[0], 0); close(pdes[0]); } close(pdes[1]); } execl("/bin/sh", "sh", "-c", command, (char *)NULL); _exit(127); } /* parent */ if (*type == 'r') { iop = fdopen(pdes[0], type); close(pdes[1]); } else { iop = fdopen(pdes[1], type); close(pdes[0]); } cur->fp = iop; cur->pid = pid; cur->next = pidlist; pidlist = cur;#if 0 ErrorF("Popen: `%s', fp = %p\n", command, iop);#endif return iop;}intPclose(iop) pointer iop;{ struct pid *cur, *last; int omask; int pstat; int pid;#if 0 ErrorF("Pclose: fp = %p\n", iop);#endif fclose(iop); for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) if (cur->fp = iop) break; if (cur == NULL) return -1; do { pid = waitpid(cur->pid, &pstat, 0); } while (pid == -1 && errno == EINTR); if (last == NULL) pidlist = cur->next; else last->next = cur->next; xfree(cur); return pid == -1 ? -1 : pstat;}#endif /* !WIN32 && !__EMX__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -