Optimisation

Jul 16, 2010 at 7:01pm
Hi every one!

I've wrote this (see code below), and i have some questions:

* is this code safe and even useful?
* how can i rewrite this code so that i just need to log one time as root and not have to check password each time i use a command?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <stdlib.h>



using namespace std;



int main() {

    int choice;


    cout << "*******************\n";
    cout << "|    LAMP MENU    |\n";
    cout << "*******************\n\n";
    cout << "1. Start Apache\n";
    cout << "2. Start Mysql\n";
    cout << "3. Stop Apache\n";
    cout << "4. Stop Mysql\n";
    cout << "5. Reload Apache\n";
    cout << "6. Reload Mysql\n";
    cout << "7. Quitter\n\n"; // "Quitter" is "Exit" in french
    cout << "Choix: ";

    cin >> choice;

    if (choice == 1) {

        system("su -lc 'service httpd start'");
        cout << "\n\n";

    }
    else if (choice == 2) {

        system("su -lc 'service mysqld start'");
        cout << "\n\n";

    }
    else if (choice == 3) {

        system("su -lc 'service httpd stop'");
        cout << "\n\n";

    }
    else if (choice == 4) {

        system("su -lc 'service mysqld stop'");
        cout << "\n\n";

    }

    else if (choice == 5) {

        system("su -lc 'service httpd reload'");
        cout << "\n\n";

    }
    else if (choice == 6) {

        system("su -lc 'service mysqld reload'");
        cout << "\n\n";

    }
    else if (choice == 7) {

        exit(0);

    }
    else {

        cout << "\n\nChoisis entre 1 et 7...\n\n";

    }

    main();


}


Thanks for your help and lights! :)
Last edited on Jul 16, 2010 at 7:02pm
Jul 16, 2010 at 7:47pm
Why isn't this a shell script?
Jul 16, 2010 at 10:06pm
I don't like the way scripts shell are written...
Jul 17, 2010 at 12:01am
Never call main. Put your code in a loop instead.
Jul 17, 2010 at 6:38am
Thanks for the help Disch! But why not call main?
Jul 17, 2010 at 6:43am
Your program will crash eventually due to infinite recursion. It also makes your program harder to follow.
Jul 17, 2010 at 7:06am
Ok, thanks :)
Jul 17, 2010 at 3:57pm
It is also a violation of the standard to call main. A conformant compiler should generate
a compile error on line 77.
Jul 17, 2010 at 9:56pm
It is also a violation of the standard to call main.


! Didn't know that...interesting.
Topic archived. No new replies allowed.