1 // =================================
 2 // Copyright (c) 2022 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 using System.Os;
 8 using System.Security;
 9 
10 void PrintHelp()
11 {
12     Console.Out() << "Usage: whoami [options]" << endl() << endl();
13     Console.Out() << "Prints the user ID and user name of the current user to standard output." << endl();
14     Console.Out() << "Options:" << endl() << endl();
15     Console.Out() << "--help | -h" << endl();
16     Console.Out() << "  Print help and exit." << endl() << endl();
17 }
18 
19 int main(int argcconst char** argv)
20 {
21     try
22     {
23         for (int i = 1; i < argc; ++i;)
24         {
25             string arg = argv[i];
26             if (arg.StartsWith("--"))
27             {
28                 if (arg == "--help")
29                 {
30                     PrintHelp();
31                     return 1;
32                 }
33                 else
34                 {
35                     throw Exception("unknown option '" + arg + "'");
36                 }
37             }
38             else if (arg.StartsWith("-"))
39             {
40                 string options = arg.Substring(1);
41                 if (options.IsEmpty())
42                 {
43                     throw Exception("unknown argument '" + arg + "'");
44                 }
45                 else
46                 {
47                     bool unknown = false;
48                     string uo;
49                     for (char o : options)
50                     {
51                         switch (o)
52                         {
53                             case 'h':
54                             {
55                                 PrintHelp();
56                                 return 1;
57                             }
58                             default:
59                             {
60                                 unknown = true;
61                                 uo.Append(o);
62                                 break;
63                             }
64                         }
65                         if (unknown)
66                         {
67                             throw Exception("unknown option '-" + uo + "'");
68                         }
69                     }
70                 }
71             }
72             else
73             {
74                 throw Exception("unknown argument '" + arg + "'");
75             }
76         }
77         int uid = GetUID();
78         Users users = GetUsers();
79         User* user = users.GetUser(uid);
80         if (user != null)
81         {
82             Console.Out() << user->UID() << ":" << user->Name() << endl();
83         }
84         else
85         {
86             throw SystemError(ENOTFOUND"user id " + ToString(uid) + " not found");
87         }
88     }
89     catch (const Exception& ex)
90     {
91         Console.Error() << ex.ToString() << endl();
92         return 1;
93     }
94     return 0;
95 }