qLibc
qsystem.c
Go to the documentation of this file.
1 /******************************************************************************
2  * qLibc
3  *
4  * Copyright (c) 2010-2015 Seungyoung Kim.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
28 
29 /**
30  * @file qsystem.c System APIs.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <netdb.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include "qinternal.h"
43 #include "utilities/qfile.h"
44 #include "utilities/qsystem.h"
45 
46 /**
47  * Get system environment variable
48  *
49  * @param envname environment name
50  * @param defstr if not found, return this string
51  *
52  * @return a pointer of environment variable
53  */
54 const char *qgetenv(const char *envname, const char *defstr) {
55  const char *envstr = getenv(envname);
56  return (envstr) ? envstr : defstr;
57 }
58 
59 /**
60  * Get the result string of external command execution
61  *
62  * @param cmd external command
63  *
64  * @return malloced string pointer which contains result if successful,
65  * otherwise returns NULL
66  *
67  * @note
68  * If the command does not report result but it is executed successfully,
69  * this will returns empty string(not null)
70  */
71 char *qsyscmd(const char *cmd) {
72  FILE *fp = popen(cmd, "r");
73  if (fp == NULL)
74  return NULL;
75  char *str = qfile_read(fp, NULL);
76  pclose(fp);
77 
78  if (str == NULL)
79  str = strdup("");
80  return str;
81 }
82 
const char * qgetenv(const char *envname, const char *defstr)
Get system environment variable.
Definition: qsystem.c:54
static void * cmd(qhttpclient_t *client, const char *method, const char *uri, void *data, size_t size, int *rescode, size_t *contentslength, qlisttbl_t *reqheaders, qlisttbl_t *resheaders)
qhttpclient->cmd(): Sends a custom request(method) to the remote host and reads it's response...
Definition: qhttpclient.c:1091
char * qsyscmd(const char *cmd)
Get the result string of external command execution.
Definition: qsystem.c:71
void * qfile_read(FILE *fp, size_t *nbytes)
Read data from a file stream.
Definition: qfile.c:214