| 1 |
//===- NonLLVMMisc.cpp - Implement misc functions that don“t depends on LLVM *- C++ -*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// MISC functions that is used through the program and do not use any LLVM |
| 11 |
/// datastructure. |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
/* Author: Giuliano Belinassi */ |
| 16 |
|
| 17 |
/* MISC functions that is used through the program but do not use any LLVM |
| 18 |
datastructure. This is put separately because we may want to build tools |
| 19 |
that do not use any LLVM datastructures, and because not including llvm |
| 20 |
headers compiles faster. */ |
| 21 |
|
| 22 |
#include "NonLLVMMisc.hh" |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
|
| 29 |
#include <iostream> |
| 30 |
|
| 31 |
/** @brief Handle some quirks of getline. */ |
| 32 |
char *getline_easy(FILE *file) |
| 33 |
{ |
| 34 |
ssize_t ret; |
| 35 |
size_t n = 0; |
| 36 |
char *line = nullptr; |
| 37 |
|
| 38 |
ret = getline(&line, &n, file); |
| 39 |
|
| 40 |
if (n == 0 || ret == -1) { |
| 41 |
free(line); |
| 42 |
return nullptr; |
| 43 |
} |
| 44 |
|
| 45 |
/* Remove any newline from the string. */ |
| 46 |
if (line[ret-1] == '\n') |
| 47 |
line[ret-1] = '\0'; |
| 48 |
|
| 49 |
return line; |
| 50 |
} |
| 51 |
|
| 52 |
bool Is_Directory(const char *path) |
| 53 |
{ |
| 54 |
struct stat s; |
| 55 |
if (stat(path, &s)) { |
| 56 |
return false; |
| 57 |
} else { |
| 58 |
return S_ISDIR(s.st_mode); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
std::vector<std::string> Extract_Args(const char *str) |
| 63 |
{ |
| 64 |
std::vector<std::string> arg_list; |
| 65 |
|
| 66 |
const char *params = strchr(str, '=') + 1; |
| 67 |
char buf[strlen(params) + 1]; |
| 68 |
const char *tok; |
| 69 |
|
| 70 |
strcpy(buf, params); |
| 71 |
tok = strtok(buf, ","); |
| 72 |
|
| 73 |
while (tok != nullptr) { |
| 74 |
arg_list.push_back(std::string(tok)); |
| 75 |
tok = strtok(nullptr, ","); |
| 76 |
} |
| 77 |
|
| 78 |
return arg_list; |
| 79 |
} |
| 80 |
|
| 81 |
std::string Extract_Single_Arg(const char *str) |
| 82 |
{ |
| 83 |
const char *params = Extract_Single_Arg_C(str); |
| 84 |
char buf[strlen(params) + 1]; |
| 85 |
const char *tok; |
| 86 |
|
| 87 |
strcpy(buf, params); |
| 88 |
tok = strtok(buf, ","); |
| 89 |
|
| 90 |
return std::string(tok); |
| 91 |
} |
| 92 |
|
| 93 |
const char *Extract_Single_Arg_C(const char *str) |
| 94 |
{ |
| 95 |
const char *params = strchr(str, '=') + 1; |
| 96 |
return params; |
| 97 |
} |
| 98 |
|
| 99 |
bool check_color_available(void) |
| 100 |
{ |
| 101 |
/* Check if NO_COLOR env variable is set. */ |
| 102 |
if (getenv("NO_COLOR")) |
| 103 |
return false; |
| 104 |
|
| 105 |
const char *term = getenv("TERM"); |
| 106 |
return term && strcmp(term, "dumb") != 0 && isatty(STDOUT_FILENO); |
| 107 |
} |
| 108 |
|
| 109 |
enum FileHandling::FileType FileHandling::Get_File_Type(int fd) |
| 110 |
{ |
| 111 |
|
| 112 |
unsigned char buf[4] = { 0 }; |
| 113 |
|
| 114 |
/* Make sure that we are in the beginning of the file */ |
| 115 |
lseek(fd, 0, SEEK_SET); |
| 116 |
|
| 117 |
int ret = read(fd, buf, 4); |
| 118 |
if (ret < 0) { |
| 119 |
throw std::runtime_error("error reading ELF file: " + std::to_string(ret)); |
| 120 |
} else if (ret == 0) { |
| 121 |
throw std::runtime_error("error reading ELF file: empty file?"); |
| 122 |
} |
| 123 |
|
| 124 |
/* reset file pointer */ |
| 125 |
lseek(fd, 0, SEEK_SET); |
| 126 |
|
| 127 |
const unsigned char elf_magic[4] = { 0x7f, 0x45, 0x4c, 0x46 }; |
| 128 |
const unsigned char zlib_magic[2] = { 0x1f, 0x8b }; |
| 129 |
const unsigned char zstd_magic[4] = { 0x28, 0xb5, 0x2f, 0xfd }; |
| 130 |
|
| 131 |
/* Check if the file is an ELF */ |
| 132 |
if (!memcmp(buf, elf_magic, 4)) |
| 133 |
return FILE_TYPE_ELF; |
| 134 |
|
| 135 |
else if (!memcmp(buf, zlib_magic, 2)) |
| 136 |
return FILE_TYPE_GZ; |
| 137 |
|
| 138 |
else if (!memcmp(buf, zstd_magic, 4)) |
| 139 |
return FILE_TYPE_ZSTD; |
| 140 |
|
| 141 |
return FILE_TYPE_UNKNOWN; |
| 142 |
} |
| 143 |
|
| 144 |
/** Get basename of a string. Works like the gnu version. */ |
| 145 |
const char *get_basename(const char *path) |
| 146 |
{ |
| 147 |
const char *base = strrchr(path, '/'); |
| 148 |
#ifdef _WIN32 |
| 149 |
const char *backslash = strrchr(path, '\\'); |
| 150 |
if (backslash > base) |
| 151 |
base = backslash; |
| 152 |
#endif |
| 153 |
return base ? base+1 : path; |
| 154 |
} |