FWadLump::FWadLump(int lumpnum, FResourceLump *lump) : FileReader() { FileReader *f = lump->GetReader(); if (f != NULL && f->GetFile() != NULL) { // Uncompressed lump in a file. For this we will have to open a new FILE, since we need it for streaming int fileno = Wads.GetLumpFile(lumpnum); const char *filename = Wads.GetWadFullName(fileno); File = openfd(filename); if (File != NULL) { Length = lump->LumpSize; StartPos = FilePos = lump->GetFileOffset(); Lump = NULL; CloseOnDestruct = true; Seek(0, SEEK_SET); return; } } File = NULL; Length = lump->LumpSize; StartPos = FilePos = 0; Lump = lump; Lump->CacheLump(); }
/* * get command line flags, initialize keywords & traps. * get values from environment. * set $pid, $cflag, $* * fabricate bootstrap code and start it (*=(argv);. /usr/lib/rcmain $*) * start interpreting code */ int main(int argc, char *argv[]) { code bootstrap[32]; char num[12], *rcmain; int i; /* needed for rcmain later */ putenv("PLAN9", unsharp("#9")); argc = getflags(argc, argv, "ftjSsrdiIlxepvVc:1m:1[command]", 1); if(argc==-1) usage("[file [arg ...]]"); if(argv[0][0]=='-') flag['l'] = flagset; if(flag['I']) flag['i'] = 0; else if(flag['i']==0 && argc==1 && Isatty(0)) flag['i'] = flagset; rcmain = flag['m'] ? flag['m'][0] : Rcmain(); err = openfd(2); kinit(); Trapinit(); Vinit(); inttoascii(num, mypid = getpid()); pathinit(); setvar("pid", newword(num, (word *)0)); setvar("cflag", flag['c']?newword(flag['c'][0], (word *)0) :(word *)0); setvar("rcname", newword(argv[0], (word *)0)); i = 0; bootstrap[i++].i = 1; bootstrap[i++].f = Xmark; bootstrap[i++].f = Xword; bootstrap[i++].s="*"; bootstrap[i++].f = Xassign; bootstrap[i++].f = Xmark; bootstrap[i++].f = Xmark; bootstrap[i++].f = Xword; bootstrap[i++].s="*"; bootstrap[i++].f = Xdol; bootstrap[i++].f = Xword; bootstrap[i++].s = rcmain; bootstrap[i++].f = Xword; bootstrap[i++].s="."; bootstrap[i++].f = Xsimple; bootstrap[i++].f = Xexit; bootstrap[i].i = 0; start(bootstrap, 1, (var *)0); /* prime bootstrap argv */ pushlist(); argv0 = strdup(argv[0]); for(i = argc-1;i!=0;--i) pushword(argv[i]); for(;;){ if(flag['r']) pfnc(err, runq); runq->pc++; (*runq->code[runq->pc-1].f)(); if(ntrap) dotrap(); } }
void addenv(var *v) { char envname[Maxenvname]; word *w; int f; io *fd; if(v->changed){ v->changed = 0; snprint(envname, sizeof envname, "/env/%s", v->name); if((f = Creat(envname))<0) pfmt(err, "rc: can't open %s: %r\n", envname); else{ for(w = v->val;w;w = w->next) write(f, w->word, strlen(w->word)+1L); close(f); } } if(v->fnchanged){ v->fnchanged = 0; snprint(envname, sizeof envname, "/env/fn#%s", v->name); if((f = Creat(envname))<0) pfmt(err, "rc: can't open %s: %r\n", envname); else{ if(v->fn){ fd = openfd(f); pfmt(fd, "fn %q %s\n", v->name, v->fn[v->pc-1].s); closeio(fd); } close(f); } } }
void Xrdfn(void) { int f, len; Dir *e; char envname[Maxenvname]; static Dir *ent, *allocent; static int nent; for(;;){ if(nent == 0){ free(allocent); nent = dirread(envdir, &allocent); ent = allocent; } if(nent <= 0) break; while(nent){ e = ent++; nent--; len = e->length; if(len && strncmp(e->name, "fn#", 3)==0){ snprint(envname, sizeof envname, "/env/%s", e->name); if((f = open(envname, 0))>=0){ execcmds(openfd(f)); return; } } } } close(envdir); Xreturn(); }
//! Sets PCA9685 mode to 00 void PCA9685::reset() { int fd = openfd(); if (fd != -1) { write_byte(fd, MODE1, 0x00); //Normal mode write_byte(fd, MODE2, 0x04); //Normal mode close(fd); } }
_STD_BEGIN FILE *_freopen(const char *filename, const char *mode, FILE *fp, int large) { int oflag, fd, err; if ((oflag = __parse_oflag(mode)) == -1) { fclose(fp); errno = EINVAL; return(NULL); } _Lockfileatomic(fp); fflush(fp); if (filename == NULL) { if ((fd = openfd(_FD_NO(fp), oflag | large)) == -1) { err = (errno == EACCES) ? EBADF : errno; _Unlockfileatomic(fp); fclose(fp); errno = err; return(NULL); } /* * Attempt to keep the same file descriptor since the POSIX * spec says that freopen(NULL,...) does not need to close * the existing file descriptor. * * The filename case requires the old file descriptor to be * closed and a new file descriptor allocated as the lowest * numbered unused descriptor. */ if (fd != _FD_NO(fp) && dup2(fd, _FD_NO(fp)) != -1) { close(fd); fd = _FD_NO(fp); } } else { close(_FD_NO(fp)); if ((fd = open(filename, oflag | large, 0666)) == -1) { err = errno; _Unlockfileatomic(fp); fclose(fp); errno = err; return(NULL); } } _Locksyslock(_LOCK_STREAM); if (fp->_Mode & _MALBUF) free(fp->_Buf); fp->_Mode &= _MALFIL; __fpinit(fp, fd, oflag); _Unlockfileatomic(fp); _Unlocksyslock(_LOCK_STREAM); return(fp); }
/*! \param led channel to set PWM value for \param on_value 0-4095 value to turn on the pulse \param off_value 0-4095 value to turn off the pulse */ void PCA9685::setPWM(uint8_t led, int on_value, int off_value) { int fd = openfd(); if (fd != -1) { write_byte(fd, LED0_ON_L + LED_MULTIPLYER * led, on_value & 0xFF); write_byte(fd, LED0_ON_H + LED_MULTIPLYER * led, on_value >> 8); write_byte(fd, LED0_OFF_L + LED_MULTIPLYER * led, off_value & 0xFF); write_byte(fd, LED0_OFF_H + LED_MULTIPLYER * led, off_value >> 8); close(fd); }
/* * Create a fan sensor descriptor. */ struct fan_sensor *create_fan_sensor(char *s) { struct fan_sensor *newval = (struct fan_sensor*)malloc(sizeof(struct fan_sensor)); if(newval == NULL) return NULL; memset(newval, 0, sizeof(struct fan_sensor)); newval->address = s; newval->value = -1; newval->fd = openfd(s, O_RDONLY); newval->next = fan_sensors_head; fan_sensors_head = newval; return newval; }
int main(void) { int ifd = openfd(); if (ifd < 0) { ALOGE("could not find any tablet mode switch, exiting."); return -1; } sleep(10); //wait some time or otherwise EventHub might not pick up our events correctly!? int ufd = open("/dev/uinput", O_WRONLY | O_NDELAY); if (ufd >= 0) { struct uinput_user_dev ud; memset(&ud, 0, sizeof(struct uinput_user_dev)); strcpy(ud.name, "Android Tablet Lid Switch"); write(ufd, &ud, sizeof(struct uinput_user_dev)); ioctl(ufd, UI_SET_EVBIT, EV_SW); ioctl(ufd, UI_SET_SWBIT, SW_LID); ioctl(ufd, UI_DEV_CREATE, 0); } else { ALOGE("could not open uinput device: %s", strerror(errno)); return -1; } // send initial switch state unsigned long sw_state[NBITS(SW_TABLET_MODE+1)]; memset(sw_state, 0, sizeof(sw_state)); if (ioctl(ifd, EVIOCGSW(sizeof(sw_state)), sw_state) >= 0) { send_switch(ufd, test_bit(SW_TABLET_MODE, sw_state) ? 1 : 0); } // read events and pass them on modified while (1) { struct input_event iev; size_t res = read(ifd, &iev, sizeof(struct input_event)); if (res < sizeof(struct input_event)) { ALOGW("insufficient input data(%d)? fd=%d", res, ifd); continue; } //LOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, ifd); if (iev.type == EV_SW && iev.code == SW_TABLET_MODE) { send_switch(ufd, iev.value); } } return 0; }
/*! \param freq desired frequency. 40Hz to 1000Hz using internal 25MHz oscillator. */ void PCA9685::setPWMFreq(int freq) { int fd = openfd(); if (fd != -1) { uint8_t prescale = (CLOCK_FREQ / 4096 / freq) - 1; //printf ("Setting prescale value to: %d\n", prescale); //printf ("Using Frequency: %d\n", freq); uint8_t oldmode = read_byte(fd, MODE1); uint8_t newmode = (oldmode & 0x7F) | 0x10; //sleep write_byte(fd, MODE1, newmode); // go to sleep write_byte(fd, PRE_SCALE, prescale); write_byte(fd, MODE1, oldmode); usleep(10*1000); write_byte(fd, MODE1, oldmode | 0x80); close(fd); } }
/* * Create a temperature sensor descriptor. */ struct temp_sensor *create_temp_sensor(char *s) { struct temp_sensor *newval = (struct temp_sensor*)malloc(sizeof(struct temp_sensor)); if(newval == NULL) return NULL; memset(newval, 0, sizeof(struct temp_sensor)); newval->address = s; newval->value = -1; newval->mintemp_present = 0; newval->mintemp = 0; newval->maxtemp_present = 0; newval->maxtemp = 60; newval->fd = openfd(s, O_RDONLY); newval->next = temp_sensors_head; temp_sensors_head = newval; return newval; }
/* * Create a fan control descriptor. */ struct fan_control *create_fan_control(char *s) { struct fan_control *newval = (struct fan_control*)malloc(sizeof(struct fan_control)); if(newval == NULL) return NULL; memset(newval, 0, sizeof(struct fan_control)); newval->address = s; newval->value = -1; newval->minstop = 0; newval->minstart = 128; newval->minpwm = 0; newval->maxpwm = 255; newval->mintemp = 0; newval->maxtemp = 60; newval->fd = openfd(s, O_RDWR); newval->next = fan_controls_head; fan_controls_head = newval; return newval; }
//return 0: continue, //return -1(<0): error //return 1(>0): exit success static inline int init_command(App & app, const char * pidfile){ int ret = 0; if (app.cmdopt().hasopt("stop")){ if (!pidfile){ fprintf(stderr, "lacking command line option pid-file ...\n"); return -1; } int killpid = lockpidfile(pidfile, SIGTERM, true); fprintf(stderr, "stoped process with normal stop mode [%d]\n", killpid); return 1; } if (app.cmdopt().hasopt("restart")){ if (!pidfile){ fprintf(stderr, "lacking command line option pid-file ...\n"); return -1; } int killpid = lockpidfile(pidfile, SIGUSR1, true); fprintf(stderr, "stoped process with restart mode [%d]\n", killpid); return 1; } if (app.cmdopt().hasopt("reload")){ if (!pidfile){ fprintf(stderr, "lacking command line option pid-file ...\n"); return -1; } int killpid = lockpidfile(pidfile, SIGUSR2, true, nullptr, true); fprintf(stderr, "reloaded process [%d]\n", killpid); return 1; } if (app.cmdopt().hasopt("console-shell")){ const char * console = app.cmdopt().getoptstr("console-listen"); if (!console){ fprintf(stderr, "has no console-listen option open console shell error !\n"); return -1; } string console_server = "tcp://"; console_server += console; printf("connecting to %s ...\n", console_server.c_str()); int confd = openfd(console_server.c_str(), "w", 3000); if (!confd){ fprintf(stderr, "connect error %s!\n", strerror(errno)); return -1; } enum { CONSOLE_BUFFER_SIZE = 1024*1024}; char * console_buffer = new char[CONSOLE_BUFFER_SIZE]; printf("console server connected ! command <quit> will exit shell\n"); while (true){ int n = readfd(confd, console_buffer, CONSOLE_BUFFER_SIZE, "token:\r\n\r\n", 1000 * 3600); if (n < 0){ fprintf(stderr, "console server closed [ret=%d]!\n", n); break; } printf("%s\n%s$", console_buffer, console); const char * command = fgets(console_buffer, CONSOLE_BUFFER_SIZE, stdin); if (strcasecmp(command, "quit") == 0){ break; } dcs::writefd(confd, command, 0, "token:\r\n\r\n"); } closefd(confd); delete console_buffer; return 1; } /////////////////////////////////////////////////////////////////// ret = _shm_command(app); if (ret < 0){ GLOG_ERR("shm command check error:%d !", ret); return -1; } if (ret > 0){ return 1; } return app.on_cmd_opt(); }
void execdot(void) { int iflag = 0; int fd; list *av; thread *p = runq; char *zero; static int first = 1; char file[512]; word *path; if(first){ dotcmds[0].i = 1; dotcmds[1].f = Xmark; dotcmds[2].f = Xword; dotcmds[3].s="0"; dotcmds[4].f = Xlocal; dotcmds[5].f = Xmark; dotcmds[6].f = Xword; dotcmds[7].s="*"; dotcmds[8].f = Xlocal; dotcmds[9].f = Xrdcmds; dotcmds[10].f = Xunlocal; dotcmds[11].f = Xunlocal; dotcmds[12].f = Xreturn; first = 0; } else eflagok = 1; popword(); if(p->argv->words && strcmp(p->argv->words->word, "-i")==0){ iflag = 1; popword(); } /* get input file */ if(p->argv->words==0){ Xerror1("Usage: . [-i] file [arg ...]"); return; } zero = strdup(p->argv->words->word); popword(); fd=-1; for(path = searchpath(zero);path;path = path->next){ strcpy(file, path->word); if(file[0]) strcat(file, "/"); strcat(file, zero); if((fd = open(file, 0))>=0) break; if(strcmp(file, "/dev/stdin")==0){ /* for sun & ucb */ fd = Dup1(0); if(fd>=0) break; } } if(fd<0){ pfmt(err, "%s: ", zero); setstatus("can't open"); Xerror(".: can't open"); return; } /* set up for a new command loop */ start(dotcmds, 1, (struct var *)0); pushredir(RCLOSE, fd, 0); runq->cmdfile = zero; runq->cmdfd = openfd(fd); runq->iflag = iflag; runq->iflast = 0; /* push $* value */ pushlist(); runq->argv->words = p->argv->words; /* free caller's copy of $* */ av = p->argv; p->argv = av->next; efree((char *)av); /* push $0 value */ pushlist(); pushword(zero); ndot++; }