/********************************************************************************************************* ** Function name: socket_attach ** Descriptions: 联结 socket ** input parameters: sock_fd socket 的私有文件描述符 ** output parameters: NONE ** Returned value: IO 系统文件描述符 *********************************************************************************************************/ int socket_attach(int sock_fd) { char path[PATH_MAX]; int fd; privinfo_t *priv; reg_t reg; file_t *file; int err; priv = kmalloc(sizeof(privinfo_t), GFP_KERNEL); if (priv != NULL) { priv->sock_fd = sock_fd; device_init(priv); sprintf(path, "/dev/socket%d", sock_fd); reg = interrupt_disable(); if (device_create(path, "socket", priv) < 0) { interrupt_resume(reg); kfree(priv); return -1; } fd = vfs_open(path, O_RDWR, 0666); if (fd < 0) { geterrno(err); vfs_unlink(path); seterrno(err); interrupt_resume(reg); kfree(priv); return -1; } file = vfs_get_file(fd); if (file == NULL) { geterrno(err); vfs_close(fd); vfs_unlink(path); seterrno(err); interrupt_resume(reg); kfree(priv); return -1; } file->type = VFS_FILE_TYPE_SOCK; vfs_put_file(file); lwip_socket_set_ctx(sock_fd, priv); interrupt_resume(reg); seterrno(0); return fd; } else { seterrno(ENOMEM); return -1; } }
/** \brief Initialise a \e libcaca canvas. * * Initialise internal \e libcaca structures and the backend that will * be used for subsequent graphical operations. It must be the first * \e libcaca function to be called in a function. caca_free_canvas() * should be called at the end of the program to free all allocated resources. * * Both the cursor and the canvas' handle are initialised at the top-left * corner. * * If an error occurs, NULL is returned and \b errno is set accordingly: * - \c EINVAL Specified width or height is invalid. * - \c ENOMEM Not enough memory for the requested canvas size. * * \param width The desired canvas width * \param height The desired canvas height * \return A libcaca canvas handle upon success, NULL if an error occurred. */ caca_canvas_t * caca_create_canvas(int width, int height) { caca_canvas_t *cv; if(width < 0 || height < 0) { seterrno(EINVAL); return NULL; } cv = malloc(sizeof(caca_canvas_t)); if(!cv) goto nomem; cv->refcount = 0; cv->autoinc = 0; cv->resize_callback = NULL; cv->resize_data = NULL; cv->frame = 0; cv->framecount = 1; cv->frames = malloc(sizeof(struct caca_frame)); if(!cv->frames) { free(cv); goto nomem; } cv->frames[0].width = cv->frames[0].height = 0; cv->frames[0].chars = NULL; cv->frames[0].attrs = NULL; cv->frames[0].x = cv->frames[0].y = 0; cv->frames[0].handlex = cv->frames[0].handley = 0; cv->frames[0].curattr = 0; cv->frames[0].name = strdup("frame#00000000"); _caca_load_frame_info(cv); caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT); cv->ndirty = 0; cv->dirty_disabled = 0; cv->ff = NULL; if(caca_resize(cv, width, height) < 0) { int saved_errno = geterrno(); free(cv->frames[0].name); free(cv->frames); free(cv); seterrno(saved_errno); return NULL; } return cv; nomem: seterrno(ENOMEM); return NULL; }
cucul_buffer_t *cucul_load_file(char const *file) { cucul_buffer_t *buf; caca_file_t *f; int ret; f = caca_file_open(file, "rb"); if(!f) return NULL; buf = malloc(sizeof(cucul_buffer_t)); if(!buf) { caca_file_close(f); return NULL; } buf->data = NULL; buf->size = 0; while(!caca_file_eof(f)) { buf->data = realloc(buf->data, buf->size + 1024); if(!buf->data) { int saved_errno = geterrno(); free(buf); caca_file_close(f); seterrno(saved_errno); return NULL; } ret = caca_file_read(f, buf->data + buf->size, 1024); if(ret >= 0) buf->size += ret; } caca_file_close(f); return buf; }