VirtualBox

source: vbox/trunk/include/VBox/sysfs.h@ 15382

Last change on this file since 15382 was 14982, checked in by vboxsync, 16 years ago

include/VBox/sysfs.h: NULL pointer check

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.2 KB
Line 
1/* $Id: sysfs.h 14982 2008-12-04 13:49:02Z vboxsync $ */
2/** @file
3 * sysfs.h - convenience routines for getting values from sysfs on Linux.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31#ifndef ___VBox_sysfs_h
32#define ___VBox_sysfs_h
33
34#include <iprt/mem.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38__BEGIN_DECLS
39
40/**
41 * Read a string value from a sysfs file.
42 * @returns iprt status value.
43 * @returns VERR_BUFFER_OVERFLOW if @a cbBuffer was not sufficient to hold the
44 * value.
45 * @param pszSysfsPath Path to the sysfs directory to be read from.
46 * @param pszFile Name of the file in @a pszSysfsPath to read the value
47 * from.
48 * @param pszBuffer Where to write the data read.
49 * @param cbBuffer The size of the buffer in @pszbuffer.
50 * @param pcbBufActual On success, the size of the data written. On buffer
51 * overflow the size of the buffer needed. Optional.
52 */
53inline int VBoxSysfsGetString(const char *pszSysfsPath, const char *pszFile,
54 char *pszBuffer, size_t cbBuffer,
55 size_t *pcbBufActual)
56{
57 PRTSTREAM pStream;
58 size_t cbBufActual;
59 int rc = VINF_SUCCESS;
60
61 char *pszFullName = NULL;
62 RTMemAutoPtr<char, RTStrFree> FullName;
63 RTStrAPrintf(&pszFullName, "%s/%s", pszSysfsPath, pszFile);
64 FullName = pszFullName;
65 if (!FullName)
66 rc = VERR_NO_MEMORY;
67 if (RT_SUCCESS(rc))
68 rc = RTStrmOpen(pszFullName, "r", &pStream);
69 if (RT_SUCCESS(rc))
70 rc = RTStrmReadEx(pStream, pszBuffer, cbBuffer, &cbBufActual);
71 /* Remove trailing newlines and zero-terminate */
72 for (size_t i = 0; RT_SUCCESS(rc) && i < cbBufActual; ++i)
73 if (pszBuffer[i] == '\n')
74 cbBufActual = i;
75 if (RT_SUCCESS(rc))
76 {
77 if (cbBufActual + 1 > cbBuffer) /* +1 for the '\0' */
78 rc = VERR_BUFFER_OVERFLOW;
79 else
80 pszBuffer[cbBufActual] = '\0';
81 }
82 if (pcbBufActual != NULL && (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW))
83 *pcbBufActual = cbBufActual + 1; /* +1 for the '\0' */
84 if (pStream != NULL)
85 RTStrmClose(pStream);
86 return rc;
87}
88
89/**
90 * Read a uint32 value from a sysfs file.
91 * @returns iprt status value.
92 * See also return codes from RTStrToUInt32Full().
93 * @param pszSysfsPath Path to the sysfs directory to be read from.
94 * @param pszFile Name of the file in @a pszSysfsPath to read the value
95 * from.
96 * @paramm uBase The base of the representation used.
97 * If this is zero, the function will look for known
98 * prefixes before defaulting to 10.
99 * @param pu32Value Where to store the value read.
100 */
101inline int VBoxSysfsGetUInt32(const char *pszSysfsPath, const char *pszFile,
102 unsigned uBase, uint32_t *pu32Value)
103{
104 char szBuffer[256];
105 int rc = VBoxSysfsGetString(pszSysfsPath, pszFile, szBuffer,
106 sizeof(szBuffer), NULL);
107 if (RT_SUCCESS(rc))
108 rc = RTStrToUInt32Full(szBuffer, uBase, pu32Value);
109 return rc;
110}
111
112__END_DECLS
113
114#endif
115
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette