📄 compiling.on.unix
字号:
X char **env = mkenv();X char **argv = mkargv(args);X char file[512];X for(;path;path = path->next){X strcpy(file, path->word);X if(file[0])X strcat(file, "/");X strcat(file, argv[1]);X#ifdef ETXTBSYReExec:X#endifX execve(file, argv+1, env);X switch(errno){X case ENOEXEC:X pfmt(err, "%s: Bourne again\n", argv[1]);X argv[0]="sh";X argv[1] = strdup(file);X execve("/bin/sh", argv, env);X goto Bad;X#ifdef ETXTBSYX case ETXTBSY:X if(++txtbusy!=5){X sleep(txtbusy);X goto ReExec;X }X msg="text busy"; goto Bad;X#endifX case EACCES:X msg="no access";X break;X case ENOMEM:X msg="not enough memory"; goto Bad;X case E2BIG:X msg="too big"; goto Bad;X }X }Bad:X pfmt(err, "%s: %s\n", argv[1], msg);X efree((char *)env);X efree((char *)argv);X}X#define NDIR 14 /* should get this from param.h */Globsize(p)register char *p;X{X int isglob = 0, globlen = NDIR+1;X for(;*p;p++){X if(*p==GLOB){X p++;X if(*p!=GLOB)X isglob++;X globlen+=*p=='*'?NDIR:1;X }X elseX globlen++;X }X return isglob?globlen:0;X}X#include <sys/types.h>X#include <dirent.h>X#define NDIRLIST 50DIR *dirlist[NDIRLIST];Opendir(name)char *name;X{X DIR **dp;X for(dp = dirlist;dp!=&dirlist[NDIRLIST];dp++)X if(*dp==0){X *dp = opendir(name);X return *dp?dp-dirlist:-1;X }X return -1;X}intReaddir(int f, char *p, int /* onlydirs */ )X{X struct dirent *dp = readdir(dirlist[f]);X if(dp==0)X return 0;X strcpy(p, dp->d_name);X return 1;X}voidClosedir(int f)X{X closedir(dirlist[f]);X dirlist[f] = 0;X}char *Signame[] = {X "sigexit", "sighup", "sigint", "sigquit",X "sigill", "sigtrap", "sigiot", "sigemt",X "sigfpe", "sigkill", "sigbus", "sigsegv",X "sigsys", "sigpipe", "sigalrm", "sigterm",X "sig16", "sigstop", "sigtstp", "sigcont",X "sigchld", "sigttin", "sigttou", "sigtint",X "sigxcpu", "sigxfsz", "sig26", "sig27",X "sig28", "sig29", "sig30", "sig31",X 0,X};voidgettrap(int sig)X{X signal(sig, gettrap);X trap[sig]++;X ntrap++;X if(ntrap>=NSIG){X pfmt(err, "rc: Too many traps (trap %d), dumping core\n", sig);X signal(SIGABRT, (void (*)())0);X kill(getpid(), SIGABRT);X }X}voidTrapinit(void)X{X int i;X void (*sig)();X if(1 || flag['d']){ /* wrong!!! */X sig = signal(SIGINT, gettrap);X if(sig==SIG_IGN)X signal(SIGINT, SIG_IGN);X }X else{X for(i = 1;i<=NSIG;i++) if(i!=SIGCHLD){X sig = signal(i, gettrap);X if(sig==SIG_IGN)X signal(i, SIG_IGN);X }X }X}Unlink(name)char *name;X{X return unlink(name);X}Write(fd, buf, cnt)char *buf;X{X return write(fd, buf, cnt);X}Read(fd, buf, cnt)char *buf;X{X return read(fd, buf, cnt);X}Seek(fd, cnt, whence)long cnt;X{X return lseek(fd, cnt, whence);X}Executable(file)char *file;X{X return(access(file, 01)==0);X}Creat(file)char *file;X{X return creat(file, 0666);X}Dup(a, b){X return dup2(a, b);X}Dup1(a){X return dup(a);X}X/*X * Wrong: should go through components of a|b|c and return the maximum.X */voidExit(char *stat)X{X int n = 0;X while(*stat){X if(*stat!='|'){X if(*stat<'0' || '9'<*stat)X exit(1);X else n = n*10+*stat-'0';X }X stat++;X }X exit(n);X}Eintr(){X return errno==EINTR;X}voidNoerror()X{X errno = 0;X}Isatty(fd){X return isatty(fd);X}voidAbort()X{X abort();X}voidexecumask(void) /* wrong -- should fork before writing */X{X int m;X struct io out[1];X switch(count(runq->argv->words)){X default:X pfmt(err, "Usage: umask [umask]\n");X setstatus("umask usage");X poplist();X return;X case 2:X umask(octal(runq->argv->words->next->word));X break;X case 1: X umask(m = umask(0));X out->fd = mapfd(1);X out->bufp = out->buf;X out->ebuf=&out->buf[NBUF];X out->strp = 0;X pfmt(out, "%o\n", m);X break;X }X setstatus("");X poplist();X}voidMemcpy(a, b, n)char *a, *b;X{X memmove(a, b, n);X}void*Malloc(unsigned long n)X{X return (void *)malloc(n);X}voiderrstr(char *buf, int len)X{X strncpy(buf, strerror(errno), len);X}!From geoff@collyer.net Fri Dec 19 01:23:26 EST 2003Received: from plan9.cs.bell-labs.com ([135.104.9.2]) by plan9; Fri Dec 19 01:23:25 EST 2003Received: from collyer.net ([63.192.14.226]) by plan9; Fri Dec 19 01:23:22 EST 2003Message-ID: <0b5ea130198a21a49139759d00d69939@collyer.net>subject: rc on unix, part 2From: Geoff Collyer <geoff@collyer.net>Date: Thu, 18 Dec 2003 22:23:21 -0800To: presotto@plan9.bell-labs.com, rsc@plan9.bell-labs.com, geoff@collyer.netMIME-Version: 1.0Content-Type: text/plain; charset="US-ASCII"Content-Transfer-Encoding: 7bitThese are the include files I used to emulate plan 9's includefiles on Unix (APE).# To unbundle, run this filemkdir includeecho include/bio.hsed 's/^X//' >include/bio.h <<'!'X#ifndef _BIOH_X#define _BIOH_ 1X#include <sys/types.h> /* for off_t */X#include <fcntl.h> /* for O_RDONLY, O_WRONLY */typedef struct Biobuf Biobuf;enumX{X Bsize = 8*1024,X Bungetsize = 4, /* space for ungetc */X Bmagic = 0x314159,X Beof = -1,X Bbad = -2,X Binactive = 0, /* states */X Bractive,X Bwactive,X Bracteof,X BendX};struct BiobufX{X int icount; /* neg num of bytes at eob */X int ocount; /* num of bytes at bob */X int rdline; /* num of bytes after rdline */X int runesize; /* num of bytes of last getrune */X int state; /* r/w/inactive */X int fid; /* open file */X int flag; /* magic if malloc'ed */X off_t offset; /* offset of buffer in file */X int bsize; /* size of buffer */X unsigned char* bbuf; /* pointer to beginning of buffer */X unsigned char* ebuf; /* pointer to end of buffer */X unsigned char* gbuf; /* pointer to good data in buf */X unsigned char b[Bungetsize+Bsize];X};X#define BGETC(bp)\X ((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp)))X#define BPUTC(bp,c)\X ((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c)))X#define BOFFSET(bp)\X (((bp)->state==Bractive)?\X (bp)->offset + (bp)->icount:\X (((bp)->state==Bwactive)?\X (bp)->offset + ((bp)->bsize + (bp)->ocount):\X -1))X#define BLINELEN(bp)\X (bp)->rdlineX#define BFILDES(bp)\X (bp)->fidint Bbuffered(Biobuf*);int Bfildes(Biobuf*);int Bflush(Biobuf*);int Bgetc(Biobuf*);int Bgetd(Biobuf*, double*);int Binit(Biobuf*, int, int);int Binits(Biobuf*, int, int, unsigned char*, int);int Blinelen(Biobuf*);off_t Boffset(Biobuf*);Biobuf* Bopen(char*, int);int Bprint(Biobuf*, char*, ...);int Bputc(Biobuf*, int);void* Brdline(Biobuf*, int);long Bread(Biobuf*, void*, long);off_t Bseek(Biobuf*, off_t, int);int Bterm(Biobuf*);int Bungetc(Biobuf*);long Bwrite(Biobuf*, void*, long);long Bgetrune(Biobuf*);int Bputrune(Biobuf*, long);int Bungetrune(Biobuf*);X#endif!echo include/fmt.hsed 's/^X//' >include/fmt.h <<'!'X/*X * The authors of this software are Rob Pike and Ken Thompson.X * Copyright (c) 2002 by Lucent Technologies.X * Permission to use, copy, modify, and distribute this software for anyX * purpose without fee is hereby granted, provided that this entire noticeX * is included in all copies of any software which is or includes a copyX * or modification of this software and in all copies of the supportingX * documentation for such software.X * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIEDX * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANYX * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITYX * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.X */X#ifndef _FMTH_X#define _FMTH_ 1X#include <stdarg.h>X#ifndef _UTFH_X#include <utf.h>X#endiftypedef struct Fmt Fmt;struct Fmt{X unsigned char runes; /* output buffer is runes or chars? */X void *start; /* of buffer */X void *to; /* current place in the buffer */X void *stop; /* end of the buffer; overwritten if flush fails */X int (*flush)(Fmt *); /* called when to == stop */X void *farg; /* to make flush a closure */X int nfmt; /* num chars formatted so far */X va_list args; /* args passed to dofmt */X int r; /* % format Rune */X int width;X int prec;X unsigned long flags;X};enum{X FmtWidth = 1,X FmtLeft = FmtWidth << 1,X FmtPrec = FmtLeft << 1,X FmtSharp = FmtPrec << 1,X FmtSpace = FmtSharp << 1,X FmtSign = FmtSpace << 1,X FmtZero = FmtSign << 1,X FmtUnsigned = FmtZero << 1,X FmtShort = FmtUnsigned << 1,X FmtLong = FmtShort << 1,X FmtVLong = FmtLong << 1,X FmtComma = FmtVLong << 1,X FmtByte = FmtComma << 1,X FmtLDouble = FmtByte << 1,X FmtFlag = FmtLDouble << 1X};extern int print(char*, ...);extern char* seprint(char*, char*, char*, ...);extern char* vseprint(char*, char*, char*, va_list);extern int snprint(char*, int, char*, ...);extern int vsnprint(char*, int, char*, va_list);extern char* smprint(char*, ...);extern char* vsmprint(char*, va_list);extern int sprint(char*, char*, ...);extern int fprint(int, char*, ...);extern int vfprint(int, char*, va_list);extern int runesprint(Rune*, char*, ...);extern int runesnprint(Rune*, int, char*, ...);extern int runevsnprint(Rune*, int, char*, va_list);extern Rune* runeseprint(Rune*, Rune*, char*, ...);extern Rune* runevseprint(Rune*, Rune*, char*, va_list);extern Rune* runesmprint(char*, ...);extern Rune* runevsmprint(char*, va_list);extern int fmtfdinit(Fmt*, int, char*, int);extern int fmtfdflush(Fmt*);extern int fmtstrinit(Fmt*);extern char* fmtstrflush(Fmt*);extern int quotestrfmt(Fmt *f);extern void quotefmtinstall(void);extern int (*fmtdoquote)(int);extern int fmtinstall(int, int (*)(Fmt*));extern int dofmt(Fmt*, char*);extern int fmtprint(Fmt*, char*, ...);extern int fmtvprint(Fmt*, char*, va_list);extern int fmtrune(Fmt*, int);extern int fmtstrcpy(Fmt*, char*);extern double fmtstrtod(const char *, char **);extern double fmtcharstod(int(*)(void*), void*);X#endif!echo include/lib9.hsed 's/^X//' >include/lib9.h <<'!'X#include <string.h>X#include "utf.h"X#define nil ((void*)0)X#define uchar _fmtucharX#define ushort _fmtushortX#define uint _fmtuintX#define ulong _fmtulongX#define vlong _fmtvlongX#define uvlong _fmtuvlongtypedef unsigned char uchar;typedef unsigned short ushort;typedef unsigned int uint;typedef unsigned long ulong;X#define OREAD O_RDONLYX#define OWRITE O_WRONLYX#define ORDWR O_RDWRX#define OCEXEC 0!echo include/regexp9.hsed 's/^X//' >include/regexp9.h <<'!'X#ifndef _REGEXP9H_X#define _REGEXP9H_ 1X#include <utf.h>typedef struct Resub Resub;typedef struct Reclass Reclass;typedef struct Reinst Reinst;typedef struct Reprog Reprog;X/*X * Sub expression matchesX */struct Resub{X unionX {X char *sp;X Rune *rsp;X }s;X unionX {X char *ep;X Rune *rep;X }e;X};X/*X * character class, each pair of rune's defines a rangeX */struct Reclass{X Rune *end;X Rune spans[64];X};X/*X * Machine instructionsX */struct Reinst{X int type;X union {X Reclass *cp; /* class pointer */X Rune r; /* character */X int subid; /* sub-expression id for RBRA and LBRA */X Reinst *right; /* right child of OR */X }u1;X union { /* regexp relies on these two being in the same union */X Reinst *left; /* left child of OR */X Reinst *next; /* next instruction for CAT & LBRA */X }u2;X};X/*X * Reprogram definitionX */struct Reprog{X Reinst *startinst; /* start pc */X Reclass class[16]; /* .data */X Reinst firstinst[5]; /* .text */X};extern Reprog *regcomp(char*);extern Reprog *regcomplit(char*);extern Reprog *regcompnl(char*);extern void regerror(char*);extern int regexec(Reprog*, char*, Resub*, int);extern void regsub(char*, char*, int, Resub*, int);extern int rregexec(Reprog*, Rune*, Resub*, int);extern void rregsub(Rune*, Rune*, Resub*, int);X#endif!echo include/utf.hsed 's/^X//' >include/utf.h <<'!'X#ifndef _UTFH_X#define _UTFH_ 1typedef unsigned short Rune; /* 16 bits */enumX{X UTFmax = 3, /* maximum bytes per rune */X Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */X Runeself = 0x80, /* rune and UTF sequences are the same (<) */X Runeerror = 0x80, /* decoding error in UTF */X};X/*X * rune routinesX */extern int runetochar(char*, Rune*);extern int chartorune(Rune*, char*);extern int runelen(long);extern int runenlen(Rune*, int);extern int fullrune(char*, int);extern int utflen(char*);extern int utfnlen(char*, long);extern char* utfrune(char*, long);extern char* utfrrune(char*, long);extern char* utfutf(char*, char*);extern char* utfecpy(char*, char*, char*);extern Rune* runestrcat(Rune*, Rune*);extern Rune* runestrchr(Rune*, Rune);extern int runestrcmp(Rune*, Rune*);extern Rune* runestrcpy(Rune*, Rune*);extern Rune* runestrncpy(Rune*, Rune*, long);extern Rune* runestrecpy(Rune*, Rune*, Rune*);extern Rune* runestrdup(Rune*);extern Rune* runestrncat(Rune*, Rune*, long);extern int runestrncmp(Rune*, Rune*, long);extern Rune* runestrrchr(Rune*, Rune);extern long runestrlen(Rune*);extern Rune* runestrstr(Rune*, Rune*);extern Rune tolowerrune(Rune);extern Rune totitlerune(Rune);extern Rune toupperrune(Rune);extern int isalpharune(Rune);extern int islowerrune(Rune);extern int isspacerune(Rune);extern int istitlerune(Rune);extern int isupperrune(Rune);X#endif!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -