From patchwork Thu Jun 16 20:29:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 1997 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 0EAAD23E54 for ; Thu, 16 Jun 2011 20:31:50 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id D19C7A18585 for ; Thu, 16 Jun 2011 20:31:49 +0000 (UTC) Received: by mail-vx0-f180.google.com with SMTP id 12so2092646vxk.11 for ; Thu, 16 Jun 2011 13:31:49 -0700 (PDT) Received: by 10.52.112.106 with SMTP id ip10mr1886273vdb.127.1308256309354; Thu, 16 Jun 2011 13:31:49 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs211213vdc; Thu, 16 Jun 2011 13:31:49 -0700 (PDT) Received: by 10.227.11.67 with SMTP id s3mr1356246wbs.62.1308256305614; Thu, 16 Jun 2011 13:31:45 -0700 (PDT) Received: from smtp.smtpout.orange.fr (smtp08.smtpout.orange.fr [80.12.242.130]) by mx.google.com with ESMTP id b3si1376671wbh.5.2011.06.16.13.31.45; Thu, 16 Jun 2011 13:31:45 -0700 (PDT) Received-SPF: neutral (google.com: 80.12.242.130 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) client-ip=80.12.242.130; Authentication-Results: mx.google.com; spf=neutral (google.com: 80.12.242.130 is neither permitted nor denied by best guess record for domain of daniel.lezcano@linaro.org) smtp.mail=daniel.lezcano@linaro.org Received: from monster.dhcp.lxc ([92.134.76.78]) by mwinf5d16 with ME id wkXf1g0031hMfSL03kXkJk; Thu, 16 Jun 2011 22:31:44 +0200 From: Daniel Lezcano To: patches@linaro.org Subject: [PATCH 13/28] function helper to read the files Date: Thu, 16 Jun 2011 22:29:42 +0200 Message-Id: <1308256197-29155-13-git-send-email-daniel.lezcano@linaro.org> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1308256197-29155-1-git-send-email-daniel.lezcano@linaro.org> References: <1308256197-29155-1-git-send-email-daniel.lezcano@linaro.org> Signed-off-by: Daniel Lezcano --- clocks.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) diff --git a/clocks.c b/clocks.c index 8a2dc97..848c52e 100644 --- a/clocks.c +++ b/clocks.c @@ -13,7 +13,11 @@ * - initial API and implementation *******************************************************************************/ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE #include +#undef _GNU_SOURCE +#endif #include #include @@ -83,6 +87,42 @@ int clock_init(void) return access(clk_dir_path, F_OK); } +/* + * This functions is a helper to read a specific file content and store + * the content inside a variable pointer passed as parameter, the format + * parameter gives the variable type to be read from the file. + * + * @path : directory path containing the file + * @name : name of the file to be read + * @format : the format of the format + * @value : a pointer to a variable to store the content of the file + * Returns 0 on success, -1 otherwise + */ +int file_read_value(const char *path, const char *name, + const char *format, void *value) +{ + FILE *file; + char *rpath; + int ret; + + ret = asprintf(&rpath, "%s/%s", path, name); + if (ret < 0) + return ret; + + file = fopen(rpath, "r"); + if (!file) { + ret = -1; + goto out_free; + } + + ret = fscanf(file, format, value) == EOF ? -1 : 0; + + fclose(file); +out_free: + free(rpath); + return ret; +} + static int file_read_from_format(char *file, int *value, const char *format) { FILE *f;