Message ID | 1362470363-7610-1-git-send-email-colin.king@canonical.com |
---|---|
State | Accepted |
Headers | show |
On 03/05/2013 03:59 PM, Colin King wrote: > From: Colin Ian King <colin.king@canonical.com> > > Add fwts_release_get() and fwts_release_free() to help get and free > the current disto release information. fwts_release_get() returns > NULL if it cannot get the distro release information. Currently only > supports debian systems. > > Example: > > fwts_release *release = fwts_release_get(); > if (release) { > printf("Distributor: <%s>\n", release->distributor); > printf("Description: <%s>\n", release->description); > printf("Release: <%s>\n", release->release); > printf("Codename: <%s>\n", release->codename); > fwts_release_free(release); > } > > Some fields may be empty strings "" when undefined. For unsupported > distros expect all fields to be empty strings if the release info > cannot be fetched. > > Signed-off-by: Colin Ian King <colin.king@canonical.com> > --- > src/lib/include/fwts.h | 1 + > src/lib/include/fwts_release.h | 33 +++++++++++ > src/lib/src/Makefile.am | 1 + > src/lib/src/fwts_release.c | 123 +++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 158 insertions(+) > create mode 100644 src/lib/include/fwts_release.h > create mode 100644 src/lib/src/fwts_release.c > > diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h > index cbf9dd9..680eab9 100644 > --- a/src/lib/include/fwts.h > +++ b/src/lib/include/fwts.h > @@ -79,5 +79,6 @@ > #include "fwts_button.h" > #include "fwts_json.h" > #include "fwts_ioport.h" > +#include "fwts_release.h" > > #endif > diff --git a/src/lib/include/fwts_release.h b/src/lib/include/fwts_release.h > new file mode 100644 > index 0000000..9a806c6 > --- /dev/null > +++ b/src/lib/include/fwts_release.h > @@ -0,0 +1,33 @@ > +/* > + * Copyright (C) 2013 Canonical > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. > + * > + */ > + > +#ifndef __FWTS_RELEASE_H__ > +#define __FWTS_RELEASE_H__ > + > +typedef struct { > + char *distributor; > + char *description; > + char *release; > + char *codename; > +} fwts_release; > + > +fwts_release *fwts_release_get(void); > +void fwts_release_free(fwts_release *release); > + > +#endif > diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am > index 6a44641..4d9ab38 100644 > --- a/src/lib/src/Makefile.am > +++ b/src/lib/src/Makefile.am > @@ -59,6 +59,7 @@ libfwts_la_SOURCES = \ > fwts_multiproc.c \ > fwts_oops.c \ > fwts_pipeio.c \ > + fwts_release.c \ > fwts_scan_efi_systab.c \ > fwts_set.c \ > fwts_smbios.c \ > diff --git a/src/lib/src/fwts_release.c b/src/lib/src/fwts_release.c > new file mode 100644 > index 0000000..5c06747 > --- /dev/null > +++ b/src/lib/src/fwts_release.c > @@ -0,0 +1,123 @@ > +/* > + * Copyright (C) 2013 Canonical > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. > + * > + */ > +#include <string.h> > + > +#include "fwts.h" > + > +/* > + * fwts_release_field_get() > + * attempt to match a field (needle) in the given text and update field > + * with any text after the delimiter, e.g. if text is "DISTRIB_ID=Ubuntu" > + * and needle is "DISTRIB_ID" and delimiter is "=", then the field gets set to "Ubuntu". > + */ > +static void fwts_release_field_get(char *needle, char *delimiter, char *text, char **field) > +{ > + char *str = strstr(text, delimiter); > + > + if (str == NULL) > + return; > + if (strstr(text, needle) == NULL) > + return; > + str++; > + while (*str == ' ') > + str++; > + > + if (str) > + *field = strdup(str); > +} > + > +/* > + * fwts_release_get_debian() > + * return release for debian based systems > + */ > +void fwts_release_get_debian(fwts_list *list, fwts_release *release) > +{ > + fwts_list_link *item; > + > + fwts_list_foreach(item, list) { > + char *line = fwts_text_list_text(item); > + > + fwts_release_field_get("DISTRIB_ID", "=", line, &release->distributor); > + fwts_release_field_get("DISTRIB_RELEASE", "=", line, &release->description); > + fwts_release_field_get("DISTRIB_CODENAME", "=", line, &release->release); > + fwts_release_field_get("DISTRIB_DESCRIPTION", "=", line, &release->codename); > + } > +} > + > +/* > + * fwts_release_field_null > + * convert NULL fields to "" > + */ > +void fwts_release_field_null_to_str(char **field) > +{ > + if (*field == NULL) > + *field = strdup(""); > +} > + > +/* > + * fwts_release_get() > + * get release info > + */ > +fwts_release *fwts_release_get(void) > +{ > + fwts_list *list; > + fwts_release *release; > + > + if ((release = calloc(1, sizeof(fwts_release))) == NULL) > + return NULL; > + > + /* > + * For the moment, check in /etc/lsb-release, we need to add in > + * support for different distros too. > + */ > + if ((list = fwts_file_open_and_read("/etc/lsb-release")) != NULL) { > + fwts_release_get_debian(list, release); > + fwts_list_free(list, free); > + } > + > + /* null fields to "" */ > + fwts_release_field_null_to_str(&release->distributor); > + fwts_release_field_null_to_str(&release->description); > + fwts_release_field_null_to_str(&release->release); > + fwts_release_field_null_to_str(&release->codename); > + > + if ((release->distributor == NULL) || > + (release->description == NULL) || > + (release->release == NULL) || > + (release->codename == NULL)) { > + fwts_release_free(release); > + return NULL; > + } > + return release; > +} > + > +/* > + * fwts_release_free() > + * free release info > + */ > +void fwts_release_free(fwts_release *release) > +{ > + if (release) { > + free(release->distributor); > + free(release->description); > + free(release->release); > + free(release->codename); > + free(release); > + } > +} > Acked-by: Ivan Hu <ivan.hu@canonical.com>
On Tue, Mar 5, 2013 at 3:59 PM, Colin King <colin.king@canonical.com> wrote: > From: Colin Ian King <colin.king@canonical.com> > > Add fwts_release_get() and fwts_release_free() to help get and free > the current disto release information. fwts_release_get() returns > NULL if it cannot get the distro release information. Currently only > supports debian systems. > > Example: > > fwts_release *release = fwts_release_get(); > if (release) { > printf("Distributor: <%s>\n", release->distributor); > printf("Description: <%s>\n", release->description); > printf("Release: <%s>\n", release->release); > printf("Codename: <%s>\n", release->codename); > fwts_release_free(release); > } > > Some fields may be empty strings "" when undefined. For unsupported > distros expect all fields to be empty strings if the release info > cannot be fetched. > > Signed-off-by: Colin Ian King <colin.king@canonical.com> > --- > src/lib/include/fwts.h | 1 + > src/lib/include/fwts_release.h | 33 +++++++++++ > src/lib/src/Makefile.am | 1 + > src/lib/src/fwts_release.c | 123 +++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 158 insertions(+) > create mode 100644 src/lib/include/fwts_release.h > create mode 100644 src/lib/src/fwts_release.c > > diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h > index cbf9dd9..680eab9 100644 > --- a/src/lib/include/fwts.h > +++ b/src/lib/include/fwts.h > @@ -79,5 +79,6 @@ > #include "fwts_button.h" > #include "fwts_json.h" > #include "fwts_ioport.h" > +#include "fwts_release.h" > > #endif > diff --git a/src/lib/include/fwts_release.h b/src/lib/include/fwts_release.h > new file mode 100644 > index 0000000..9a806c6 > --- /dev/null > +++ b/src/lib/include/fwts_release.h > @@ -0,0 +1,33 @@ > +/* > + * Copyright (C) 2013 Canonical > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. > + * > + */ > + > +#ifndef __FWTS_RELEASE_H__ > +#define __FWTS_RELEASE_H__ > + > +typedef struct { > + char *distributor; > + char *description; > + char *release; > + char *codename; > +} fwts_release; > + > +fwts_release *fwts_release_get(void); > +void fwts_release_free(fwts_release *release); > + > +#endif > diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am > index 6a44641..4d9ab38 100644 > --- a/src/lib/src/Makefile.am > +++ b/src/lib/src/Makefile.am > @@ -59,6 +59,7 @@ libfwts_la_SOURCES = \ > fwts_multiproc.c \ > fwts_oops.c \ > fwts_pipeio.c \ > + fwts_release.c \ > fwts_scan_efi_systab.c \ > fwts_set.c \ > fwts_smbios.c \ > diff --git a/src/lib/src/fwts_release.c b/src/lib/src/fwts_release.c > new file mode 100644 > index 0000000..5c06747 > --- /dev/null > +++ b/src/lib/src/fwts_release.c > @@ -0,0 +1,123 @@ > +/* > + * Copyright (C) 2013 Canonical > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. > + * > + */ > +#include <string.h> > + > +#include "fwts.h" > + > +/* > + * fwts_release_field_get() > + * attempt to match a field (needle) in the given text and update field > + * with any text after the delimiter, e.g. if text is "DISTRIB_ID=Ubuntu" > + * and needle is "DISTRIB_ID" and delimiter is "=", then the field gets set to "Ubuntu". > + */ > +static void fwts_release_field_get(char *needle, char *delimiter, char *text, char **field) > +{ > + char *str = strstr(text, delimiter); > + > + if (str == NULL) > + return; > + if (strstr(text, needle) == NULL) > + return; > + str++; > + while (*str == ' ') > + str++; > + > + if (str) > + *field = strdup(str); > +} > + > +/* > + * fwts_release_get_debian() > + * return release for debian based systems > + */ > +void fwts_release_get_debian(fwts_list *list, fwts_release *release) > +{ > + fwts_list_link *item; > + > + fwts_list_foreach(item, list) { > + char *line = fwts_text_list_text(item); > + > + fwts_release_field_get("DISTRIB_ID", "=", line, &release->distributor); > + fwts_release_field_get("DISTRIB_RELEASE", "=", line, &release->description); > + fwts_release_field_get("DISTRIB_CODENAME", "=", line, &release->release); > + fwts_release_field_get("DISTRIB_DESCRIPTION", "=", line, &release->codename); > + } > +} > + > +/* > + * fwts_release_field_null > + * convert NULL fields to "" > + */ > +void fwts_release_field_null_to_str(char **field) > +{ > + if (*field == NULL) > + *field = strdup(""); > +} > + > +/* > + * fwts_release_get() > + * get release info > + */ > +fwts_release *fwts_release_get(void) > +{ > + fwts_list *list; > + fwts_release *release; > + > + if ((release = calloc(1, sizeof(fwts_release))) == NULL) > + return NULL; > + > + /* > + * For the moment, check in /etc/lsb-release, we need to add in > + * support for different distros too. > + */ > + if ((list = fwts_file_open_and_read("/etc/lsb-release")) != NULL) { > + fwts_release_get_debian(list, release); > + fwts_list_free(list, free); > + } > + > + /* null fields to "" */ > + fwts_release_field_null_to_str(&release->distributor); > + fwts_release_field_null_to_str(&release->description); > + fwts_release_field_null_to_str(&release->release); > + fwts_release_field_null_to_str(&release->codename); > + > + if ((release->distributor == NULL) || > + (release->description == NULL) || > + (release->release == NULL) || > + (release->codename == NULL)) { > + fwts_release_free(release); > + return NULL; > + } > + return release; > +} > + > +/* > + * fwts_release_free() > + * free release info > + */ > +void fwts_release_free(fwts_release *release) > +{ > + if (release) { > + free(release->distributor); > + free(release->description); > + free(release->release); > + free(release->codename); > + free(release); > + } > +} > -- > 1.8.1.2 > Acked-by: Keng-Yu Lin <kengyu@canonical.com>
diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h index cbf9dd9..680eab9 100644 --- a/src/lib/include/fwts.h +++ b/src/lib/include/fwts.h @@ -79,5 +79,6 @@ #include "fwts_button.h" #include "fwts_json.h" #include "fwts_ioport.h" +#include "fwts_release.h" #endif diff --git a/src/lib/include/fwts_release.h b/src/lib/include/fwts_release.h new file mode 100644 index 0000000..9a806c6 --- /dev/null +++ b/src/lib/include/fwts_release.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2013 Canonical + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef __FWTS_RELEASE_H__ +#define __FWTS_RELEASE_H__ + +typedef struct { + char *distributor; + char *description; + char *release; + char *codename; +} fwts_release; + +fwts_release *fwts_release_get(void); +void fwts_release_free(fwts_release *release); + +#endif diff --git a/src/lib/src/Makefile.am b/src/lib/src/Makefile.am index 6a44641..4d9ab38 100644 --- a/src/lib/src/Makefile.am +++ b/src/lib/src/Makefile.am @@ -59,6 +59,7 @@ libfwts_la_SOURCES = \ fwts_multiproc.c \ fwts_oops.c \ fwts_pipeio.c \ + fwts_release.c \ fwts_scan_efi_systab.c \ fwts_set.c \ fwts_smbios.c \ diff --git a/src/lib/src/fwts_release.c b/src/lib/src/fwts_release.c new file mode 100644 index 0000000..5c06747 --- /dev/null +++ b/src/lib/src/fwts_release.c @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2013 Canonical + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +#include <string.h> + +#include "fwts.h" + +/* + * fwts_release_field_get() + * attempt to match a field (needle) in the given text and update field + * with any text after the delimiter, e.g. if text is "DISTRIB_ID=Ubuntu" + * and needle is "DISTRIB_ID" and delimiter is "=", then the field gets set to "Ubuntu". + */ +static void fwts_release_field_get(char *needle, char *delimiter, char *text, char **field) +{ + char *str = strstr(text, delimiter); + + if (str == NULL) + return; + if (strstr(text, needle) == NULL) + return; + str++; + while (*str == ' ') + str++; + + if (str) + *field = strdup(str); +} + +/* + * fwts_release_get_debian() + * return release for debian based systems + */ +void fwts_release_get_debian(fwts_list *list, fwts_release *release) +{ + fwts_list_link *item; + + fwts_list_foreach(item, list) { + char *line = fwts_text_list_text(item); + + fwts_release_field_get("DISTRIB_ID", "=", line, &release->distributor); + fwts_release_field_get("DISTRIB_RELEASE", "=", line, &release->description); + fwts_release_field_get("DISTRIB_CODENAME", "=", line, &release->release); + fwts_release_field_get("DISTRIB_DESCRIPTION", "=", line, &release->codename); + } +} + +/* + * fwts_release_field_null + * convert NULL fields to "" + */ +void fwts_release_field_null_to_str(char **field) +{ + if (*field == NULL) + *field = strdup(""); +} + +/* + * fwts_release_get() + * get release info + */ +fwts_release *fwts_release_get(void) +{ + fwts_list *list; + fwts_release *release; + + if ((release = calloc(1, sizeof(fwts_release))) == NULL) + return NULL; + + /* + * For the moment, check in /etc/lsb-release, we need to add in + * support for different distros too. + */ + if ((list = fwts_file_open_and_read("/etc/lsb-release")) != NULL) { + fwts_release_get_debian(list, release); + fwts_list_free(list, free); + } + + /* null fields to "" */ + fwts_release_field_null_to_str(&release->distributor); + fwts_release_field_null_to_str(&release->description); + fwts_release_field_null_to_str(&release->release); + fwts_release_field_null_to_str(&release->codename); + + if ((release->distributor == NULL) || + (release->description == NULL) || + (release->release == NULL) || + (release->codename == NULL)) { + fwts_release_free(release); + return NULL; + } + return release; +} + +/* + * fwts_release_free() + * free release info + */ +void fwts_release_free(fwts_release *release) +{ + if (release) { + free(release->distributor); + free(release->description); + free(release->release); + free(release->codename); + free(release); + } +}