1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <stdlib.h> 6 #include <iostream> 7 #include <string> 8 9 namespace { 10 11 // Returns the current user username. Username()12std::string Username() { 13 const char* username = getenv("USER"); 14 return username ? std::string(username) : std::string(); 15 } 16 17 // Writes |string| to |stream| while escaping all C escape sequences. EscapeString(std::ostream * stream,const std::string & string)18void EscapeString(std::ostream* stream, const std::string& string) { 19 for (char c : string) { 20 switch (c) { 21 case 0: 22 *stream << "\\0"; 23 break; 24 case '\a': 25 *stream << "\\a"; 26 break; 27 case '\b': 28 *stream << "\\b"; 29 break; 30 case '\e': 31 *stream << "\\e"; 32 break; 33 case '\f': 34 *stream << "\\f"; 35 break; 36 case '\n': 37 *stream << "\\n"; 38 break; 39 case '\r': 40 *stream << "\\r"; 41 break; 42 case '\t': 43 *stream << "\\t"; 44 break; 45 case '\v': 46 *stream << "\\v"; 47 break; 48 case '\\': 49 *stream << "\\\\"; 50 break; 51 case '\"': 52 *stream << "\\\""; 53 break; 54 default: 55 *stream << c; 56 break; 57 } 58 } 59 } 60 61 } // namespace 62 main(int argc,char ** argv)63int main(int argc, char** argv) { 64 std::string username = Username(); 65 66 std::cout << "{\"username\": \""; 67 EscapeString(&std::cout, username); 68 std::cout << "\"}" << std::endl; 69 70 return 0; 71 } 72