11#define _GNU_SOURCE
2+ #include <linux/limits.h>
23#include <stdbool.h>
34#include <stdio.h>
45#include <stdlib.h>
56#include <string.h>
6- #include <unistd.h>
7- #include <sys/types.h>
87#include <sys/stat.h>
9- #include <linux/limits.h>
10- #include <pwd.h>
11- #include <libgen.h>
8+ #include <sys/types.h>
9+ #include <unistd.h>
10+
11+ #include <dlfcn.h>
1212#include <errno.h>
13+ #include <libgen.h>
14+ #include <pwd.h>
1315#include <sys/inotify.h>
1416#include <sys/types.h>
15- #include <dlfcn.h>
1617
1718struct Entry {
1819 char const * category ;
@@ -25,38 +26,35 @@ struct Failure {
2526 char * info ;
2627};
2728void fail (struct Failure failure ) {
28- #define RED "\033[0;31m"
29- #define BLUE "\033[0;34m"
30- #define CYAN "\033[0;36m"
31- #define YELLOW "\033[0;33m"
32- #define MAGENTA "\033[0;35m"
33- #define RESET "\033[0m"
29+ #define RED "\033[0;31m"
30+ #define BLUE "\033[0;34m"
31+ #define CYAN "\033[0;36m"
32+ #define YELLOW "\033[0;33m"
33+ #define MAGENTA "\033[0;35m"
34+ #define RESET "\033[0m"
3435
3536 fprintf (stderr ,
36- "Application reached a " RED "terminating failure" RESET "...\n"
37- BLUE "Operation: " RESET "Failed to %s\n"
38- CYAN "Reason: " RESET "%s\n" ,
39- failure .operation , failure .reason
40- );
37+ "Application reached a " RED "terminating failure" RESET "...\n" BLUE "Operation: " RESET
38+ "Failed to %s\n" CYAN "Reason: " RESET "%s\n" ,
39+ failure .operation , failure .reason );
4140 if (failure .info != NULL ) {
42- fprintf (stderr , YELLOW "Extra Information: " RESET "%s\n" , failure .info );
41+ fprintf (stderr , YELLOW "Extra Information: " RESET "%s\n" , failure .info );
4342 }
4443
4544 exit (EXIT_FAILURE );
4645}
4746void deploy (char * , char * , bool );
4847
49- static char * So_file = "/home/edwin/.dotfiles/os-unix/data/libdotfiles.so" ; // TODO
48+ static char * So_file = "/home/edwin/.dotfiles/os-unix/data/libdotfiles.so" ; // TODO
5049
5150int main (int argc , char * argv []) {
5251 static bool debug = false;
5352 if (getenv ("DEBUG" ) != NULL ) {
5453 debug = true;
5554 }
5655
57- char * help_menu =
58- "d: A dotfile manager.\n"
59- "Commands: <deploy | undeploy | print | compile>" ;
56+ char * help_menu = "d: A dotfile manager.\n"
57+ "Commands: <deploy | undeploy | print | compile>" ;
6058
6159 enum Command {
6260 CommandNone ,
@@ -68,8 +66,8 @@ int main(int argc, char *argv[]) {
6866
6967 if (argc < 2 ) {
7068 fail ((struct Failure ){
71- .operation = "validate arguments" ,
72- .reason = "Must pass a subcommand" ,
69+ .operation = "validate arguments" ,
70+ .reason = "Must pass a subcommand" ,
7371 });
7472 }
7573 if (strcmp (argv [1 ], "deploy" ) == 0 ) {
@@ -84,32 +82,26 @@ int main(int argc, char *argv[]) {
8482 char * strp = NULL ;
8583 if (asprintf (& strp , "\nSubcommand: %s\n---\nHELP MENU:\n%s" , argv [1 ], help_menu ) == -1 ) {
8684 fail ((struct Failure ){
87- .operation = "format error with asprintf" ,
88- .reason = strerror (errno ),
85+ .operation = "format error with asprintf" ,
86+ .reason = strerror (errno ),
8987 });
9088 }
9189 fail ((struct Failure ){
92- .operation = "validate arguments" ,
93- .reason = "Subcommand not recognized" ,
94- .info = strp ,
90+ .operation = "validate arguments" ,
91+ .reason = "Subcommand not recognized" ,
92+ .info = strp ,
9593 });
9694 }
9795
9896 if (command == CommandCompile ) {
9997 char * cmd = NULL ;
100- if (!asprintf (& cmd , "cd /home/edwin/.dotfiles/os-unix/data && gcc -Werror=unused-variable -g -fPIC -c ./dotfiles.c -o ./dotfiles.o && gcc -shared -o ./libdotfiles.so ./dotfiles.o" )) { // TODO
101- fail ((struct Failure ){
102- .operation = "asprintf" ,
103- .reason = strerror (errno ),
104- .info = cmd
105- });
98+ if (!asprintf (& cmd , "cd /home/edwin/.dotfiles/os-unix/data && gcc -Werror=unused-variable -g -fPIC -c "
99+ "./dotfiles.c -o ./dotfiles.o && gcc -shared -o ./libdotfiles.so ./dotfiles.o" )) { // TODO
100+ fail ((struct Failure ){.operation = "asprintf" , .reason = strerror (errno ), .info = cmd });
106101 }
107102
108103 if (system (cmd ) == -1 ) {
109- fail ((struct Failure ) {
110- .operation = "system" ,
111- .reason = strerror (errno )
112- });
104+ fail ((struct Failure ){.operation = "system" , .reason = strerror (errno )});
113105 };
114106
115107 printf ("Compiled configuration file\n" );
@@ -124,29 +116,31 @@ int main(int argc, char *argv[]) {
124116 }
125117 dlerror ();
126118
127- struct Entry (* * configuration ) = (struct Entry * * ) dlsym (handle , "configuration" );
119+ struct Entry (* * configuration ) = (struct Entry * * )dlsym (handle , "configuration" );
128120 char * error = dlerror ();
129- if (error != NULL ) {
130- fprintf (stderr , "%s\n" , error );
131- exit (1 );
132- }
121+ if (error != NULL ) {
122+ fprintf (stderr , "%s\n" , error );
123+ exit (1 );
124+ }
133125
134- if (command == CommandPrint ) printf ("[\n" );
135- for (int i = 0 ; ; i += 1 ) {
126+ if (command == CommandPrint )
127+ printf ("[\n" );
128+ for (int i = 0 ;; i += 1 ) {
136129 struct Entry * entrygroup = configuration [i ];
137130 if (entrygroup == NULL ) {
138131 break ;
139132 }
140133
141- for (int j = 0 ; ; j += 1 ) {
134+ for (int j = 0 ;; j += 1 ) {
142135 struct Entry entry = entrygroup [j ];
143136 if (entry .source == NULL && entry .destination == NULL && entry .category == NULL ) {
144137 break ;
145138 }
146139
147140 if (command == CommandPrint ) {
148- printf ("\t{ \"category\": \"%s\", \"source\": \"%s\", \"destination\": \"%s\" }" , entry .category , entry .source , entry .destination );
149- if (configuration [i + 1 ] == NULL ) {
141+ printf ("\t{ \"category\": \"%s\", \"source\": \"%s\", \"destination\": \"%s\" }" , entry .category ,
142+ entry .source , entry .destination );
143+ if (configuration [i + 1 ] == NULL ) {
150144 printf ("\n" );
151145 } else {
152146 printf (",\n" );
@@ -172,25 +166,29 @@ int main(int argc, char *argv[]) {
172166 }
173167 }
174168 }
175- }
176- if (command == CommandPrint ) printf ("]\n" );
177- dlclose (handle );
169+ }
170+ if (command == CommandPrint )
171+ printf ("]\n" );
172+ dlclose (handle );
178173}
179174
180175void deploy (char * source_path , char * destination_path , bool debug ) {
181176 // Check trailing slash.
182177 {
183178 if (destination_path [strlen (destination_path ) - 1 ] == '/' ) {
184179 if (source_path [strlen (source_path ) - 1 ] != '/' ) {
185- fprintf (stderr , "Error: If destination path does have trailing slash, then source path must have it too.\n" );
180+ fprintf (stderr ,
181+ "Error: If destination path does have trailing slash, then source path must have it too.\n" );
186182 exit (1 );
187183 }
188184
189185 destination_path [strlen (destination_path ) - 1 ] = '\0' ;
190186 source_path [strlen (source_path ) - 1 ] = '\0' ;
191187 } else {
192188 if (source_path [strlen (source_path ) - 1 ] == '/' ) {
193- fprintf (stderr , "Error: If destination path does not have trailing slash, then source path must not have it either.\n" );
189+ fprintf (
190+ stderr ,
191+ "Error: If destination path does not have trailing slash, then source path must not have it either.\n" );
194192 exit (1 );
195193 }
196194 }
@@ -249,10 +247,10 @@ void deploy(char *source_path, char *destination_path, bool debug) {
249247 free (dir );
250248
251249 goto end ;
252- error :
250+ error :
253251 free (dir );
254252 exit (1 );
255- end :
253+ end :
256254 }
257255
258256 struct stat st = {0 };
0 commit comments