int main(){ int fd; char buf[BUFSIZE]; int len, pos, size, ret; mytbf_t *tbf; fd = open("/etc/services", O_RDONLY); if(fd < 0){ perror("open()"); exit(1); } tbf = mytbf_init(10, 1000); if(tbf == NULL){ perror("mytbf_init()"); exit(1); } while(1){ size = mytbf_fetchtoken(tbf, BUFSIZE); len = read(fd, buf, size); if(len < 0){ perror("read()"); } else if(len == 0){ break; } mytbf_returntoken(tbf, size - len); pos = 0; while(len > 0){ ret = write(1, buf + pos, len); len -= ret; pos += ret; } } close(fd); return 0; }
static struct channel_context_st *path2entry(const char *path) { static chnid_t curr_id = MINCHNID; struct channel_context_st *me; FILE *fp; char pathstr[PATHSIZE], linebuf[BUFSIZE]; strncpy(pathstr, path, PATHSIZE); strncat(pathstr, "/desc.text", sizeof("/desc.text")); fp = fopen(pathstr, "r"); if (fp == NULL) { syslog(LOG_INFO, "%s is not a channel dir(Can't find desc.txt) ", path); return NULL; } if (fgets(linebuf, BUFSIZE, fp) == NULL) { syslog(LOG_INFO, "%s is not a channel dir(Can't get description text) ", path); fclose(fp); return NULL; } fclose(fp); me = malloc(sizeof(*me)); if (me == NULL) { syslog(LOG_ERR, "malloc(): %m"); return NULL; } me->tbf = mytbf_init(MP3_BITRATE / 8, MP3_BITRATE * 10 / 8); if (me->tbf == NULL) { syslog(LOG_ERR, "mytbf_init(): %m"); free(me); return NULL; } me->desc = strdup(linebuf); strncpy(pathstr, path, PATHSIZE); strncat(pathstr, "/*.mp3", PATHSIZE); if (glob(pathstr, 0, NULL, &me->mp3glob)) { syslog(LOG_ERR, "%s is not a channel dir(Can't find mp3 files) ", path); mytbf_destroy(me->tbf); free(me); return NULL; } me->pos = 0; me->offset = 0; printf("open mp3 file %s\n", me->mp3glob.gl_pathv[me->pos]); me->fd = open(me->mp3glob.gl_pathv[me->pos], O_RDONLY); if (me->fd < 0) { syslog(LOG_WARNING, "open(): %m"); mytbf_destroy(me->tbf); free(me); return NULL; } me->id = curr_id; curr_id++; return me; }
int main(int argc,char **argv) { int sfd,dfd = 1; char buf[BUFSIZE]; int len,ret,pos,size; mytbf_t *tbf; if(argc < 2) { fprintf(stderr, "Usage...\n"); exit(1); } tbf = mytbf_init(CPS, BURST); if(tbf == NULL) { fprintf(stderr,"mytbf_init() failed.\n"); exit(1); } do { sfd = open(argv[1], O_RDONLY); if(sfd < 0) { if(errno != EINTR) { perror("open()"); exit(1); } } } while(sfd < 0); while(1){ size = mytbf_fetchtoken(tbf,BUFSIZE); if(size < 0) { fprintf(stderr,"mytbf_fetchtoken():%s\n",strerror(-size)); exit(1); } while((len = read(sfd,buf,size)) < 0) { if(errno == EINTR) continue; perror("read()"); break; } if(len == 0) break; // len > 0 if(size - len > 0) mytbf_returntoken(tbf,size-len); pos = 0; while(len > 0) { ret = write(dfd,buf+pos,len); if(ret < 0) { if(errno == EINTR) continue; perror("write()"); exit(1); } pos += ret; len -= ret; } } close(sfd); mytbf_destroy(tbf); exit(0); }