Konrad Rzeszutek wrote: > + * Helper routines. > + */ > +static int file_exist(const char *file) > +{ > + > + struct stat bootpath_stat; > + > + return !stat(file, &bootpath_stat); > +} > + > +static int read_file(const char *file, char **contents) > +{ > + int error, fd, bytes_read; > + struct stat bootpath_stat; > + > + error = stat(file, &bootpath_stat); > + if (error < 0) { > + fprintf(stderr, "(%s:%d) stat %s, %s\n", __FILE__, __LINE__, > + file, strerror(errno)); > + return error; > + } > + > + *contents = malloc(bootpath_stat.st_size); > + if (!*contents) { > + error = ENOMEM; > + fprintf(stderr, "(%s:%d) Could not allocate enough memory for "\ > + "%s: %s (%d)\n", > + __FILE__, __LINE__, file, strerror(error), error); > + return errno; > + } > + > + fd = open(file, O_RDONLY); > + if (fd < 0) { > + fprintf(stderr, "(%s:%d): Could not open %s: %s (%d)\n", > + __FILE__, __LINE__, file, strerror(errno), errno); > + free(*contents); > + return errno; > + } > + > + bytes_read = read(fd, *contents, bootpath_stat.st_size); > + close(fd); > + if (bytes_read > bootpath_stat.st_size) { > + fprintf(stderr, "(%s:%d) Read more data in than expected for "\ > + "%s: %s (%d)\n", > + __FILE__, __LINE__, file, strerror(EIO), EIO); > + free(*contents); > + return errno; > + } > + /* chop() implementation */ > + if (*(*contents + (ssize_t)(bytes_read - 1)) == '\n') > + *(*contents + (ssize_t) (bytes_read - 1)) = 0; > + > + return 0; > +} > + > +static int read_data(const char *dir, const char *name, char *dst, ssize_t > size) > +{ > + char *data = NULL; > + char file[FILENAMESZ]; > + int rc = 0; > + > + memset(file, 0, FILENAMESZ); > + strncat(file, dir, FILENAMESZ); > + strncat(file, name, FILENAMESZ); > + > + if (file_exist(file)) { > + rc = read_file(file, &data); > + if (debug) > + fprintf(stderr, "(%s:%d) Read from %s:[%s]\n", > + __FILE__, __LINE__, file, data); > + if (!rc) > + memcpy(dst, data, size); > + free(data); > + } > + > + return rc; > +} > + > +static int read_int_data(const char *dir, const char *name, int *dst) > +{ > + int rc = 0; > + char contents[5]; /* The flag is a 1 byte value */ > + > + rc = read_data(dir, name, (char *)&contents, sizeof(contents)); > + if (!rc) > + *dst = atoi(contents); > + > + return rc; > +} > +
Konrad, could you convert this to the sysfs api in the open-iscsi git tree? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "open-iscsi" group. To post to this group, send email to open-iscsi@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/open-iscsi -~----------~----~----~----~------~----~------~--~---