Joel Fernandes | d76a200 | 2018-10-16 13:19:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <iostream> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <android-base/strings.h> |
| 22 | |
| 23 | using android::base::Split; |
| 24 | using std::string; |
| 25 | using std::vector; |
| 26 | |
| 27 | static string pathToFilename(string path, bool noext = false) { |
| 28 | vector<string> spath = android::base::Split(path, "/"); |
| 29 | string ret = spath.back(); |
| 30 | |
| 31 | if (noext) { |
| 32 | size_t lastindex = ret.find_last_of("."); |
| 33 | return ret.substr(0, lastindex); |
| 34 | } |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | static int getMachineKvers(void) { |
| 39 | struct utsname un; |
| 40 | char* unameOut; |
| 41 | int nums[3]; // maj, min, sub |
| 42 | |
| 43 | if (uname(&un)) return -1; |
| 44 | unameOut = un.release; |
| 45 | |
| 46 | string s = unameOut; |
| 47 | string token; |
| 48 | size_t pos = 0; |
| 49 | int cur_num = 0; |
| 50 | |
| 51 | while ((pos = s.find(".")) != string::npos && cur_num < 3) { |
| 52 | token = s.substr(0, pos); |
| 53 | s.erase(0, pos + 1); |
| 54 | |
| 55 | if ((pos = token.find("-")) != string::npos) token = token.substr(0, pos); |
| 56 | |
| 57 | nums[cur_num++] = stoi(token); |
| 58 | } |
| 59 | |
| 60 | if ((pos = s.find("-")) != string::npos) |
| 61 | token = s.substr(0, pos); |
| 62 | else |
| 63 | token = s; |
| 64 | |
| 65 | if (token.length() > 0 && cur_num < 3) { |
| 66 | nums[cur_num++] = stoi(token); |
| 67 | } |
| 68 | |
| 69 | if (cur_num != 3) |
| 70 | return -1; |
| 71 | else |
| 72 | return (65536 * nums[0] + 256 * nums[1] + nums[2]); |
| 73 | } |
| 74 | |
| 75 | static void deslash(string& s) { |
| 76 | std::replace(s.begin(), s.end(), '/', '_'); |
| 77 | } |