1
+ #include <errno.h>
1
2
#include <stdio.h>
2
3
#include <string.h>
4
+
3
5
#include <unistd.h>
4
6
#include <sys/stat.h>
5
7
@@ -14,71 +16,89 @@ int uppm_home_dir(char buf[], size_t bufSize, size_t * outSize) {
14
16
return UPPM_ERROR_ARG_IS_INVALID ;
15
17
}
16
18
17
- const char * const uppmHomeDir = getenv ("UPPM_HOME" );
19
+ const char * const uppmHomeDIR = getenv ("UPPM_HOME" );
18
20
19
- if (uppmHomeDir == NULL ) {
20
- const char * const userHomeDir = getenv ("HOME" );
21
+ if (uppmHomeDIR == NULL ) {
22
+ const char * const userHomeDIR = getenv ("HOME" );
21
23
22
- if (userHomeDir == NULL ) {
24
+ if (userHomeDIR == NULL ) {
23
25
return UPPM_ERROR_ENV_HOME_NOT_SET ;
24
26
}
25
27
26
- size_t userHomeDirLength = strlen (userHomeDir );
27
-
28
- if (userHomeDirLength == 0U ) {
28
+ if (userHomeDIR [0 ] == '\0' ) {
29
29
return UPPM_ERROR_ENV_HOME_NOT_SET ;
30
30
}
31
31
32
- size_t uppmHomeDirLength = userHomeDirLength + + 7U ;
33
- char uppmHomeDir [uppmHomeDirLength ];
34
- snprintf (uppmHomeDir , uppmHomeDirLength , "%s/.uppm" , userHomeDir );
32
+ size_t defaultUppmHomeDIRCapacity = strlen (userHomeDIR ) + 7U ;
33
+ char defaultUppmHomeDIR [defaultUppmHomeDIRCapacity ];
34
+
35
+ int ret = snprintf (defaultUppmHomeDIR , defaultUppmHomeDIRCapacity , "%s/.uppm" , userHomeDIR );
36
+
37
+ if (ret < 0 ) {
38
+ perror (NULL );
39
+ return UPPM_ERROR ;
40
+ }
41
+
42
+ size_t defaultUppmHomeDIRLength = ret ;
35
43
36
44
struct stat st ;
37
45
38
- if (stat (uppmHomeDir , & st ) == 0 ) {
46
+ if (stat (defaultUppmHomeDIR , & st ) == 0 ) {
39
47
if (!S_ISDIR (st .st_mode )) {
40
- fprintf (stderr , "'%s\n' was expected to be a directory, but it was not.\n" , uppmHomeDir );
48
+ fprintf (stderr , "%s was expected to be a directory, but it was not.\n" , defaultUppmHomeDIR );
41
49
return UPPM_ERROR ;
42
50
}
43
51
} else {
44
- if (mkdir (uppmHomeDir , S_IRWXU ) != 0 ) {
45
- perror (uppmHomeDir );
46
- return UPPM_ERROR ;
52
+ if (mkdir (defaultUppmHomeDIR , S_IRWXU ) != 0 ) {
53
+ if (errno != EEXIST ) {
54
+ perror (defaultUppmHomeDIR );
55
+ return UPPM_ERROR ;
56
+ }
47
57
}
48
58
}
49
59
50
- size_t n = (bufSize > uppmHomeDirLength ) ? uppmHomeDirLength : bufSize ;
60
+ size_t m = bufSize - 1U ;
61
+
62
+ size_t n = (m > defaultUppmHomeDIRLength ) ? defaultUppmHomeDIRLength : m ;
63
+
64
+ strncpy (buf , defaultUppmHomeDIR , n );
51
65
52
- strncpy ( buf , uppmHomeDir , n ) ;
66
+ buf [ n ] = '\0' ;
53
67
54
68
if (outSize != NULL ) {
55
69
(* outSize ) = n ;
56
70
}
57
71
} else {
58
- if (uppmHomeDir [0 ] == '\0' ) {
72
+ if (uppmHomeDIR [0 ] == '\0' ) {
59
73
fprintf (stderr , "'UPPM_HOME' environment variable's value was expected to be a non-empty string, but it was not.\n" );
60
74
return UPPM_ERROR ;
61
75
}
62
76
63
77
struct stat st ;
64
78
65
- if (stat (uppmHomeDir , & st ) == 0 ) {
79
+ if (stat (uppmHomeDIR , & st ) == 0 ) {
66
80
if (!S_ISDIR (st .st_mode )) {
67
- fprintf (stderr , "'%s\n' was expected to be a directory, but it was not.\n" , uppmHomeDir );
81
+ fprintf (stderr , "%s was expected to be a directory, but it was not.\n" , uppmHomeDIR );
68
82
return UPPM_ERROR ;
69
83
}
70
84
} else {
71
- if (mkdir (uppmHomeDir , S_IRWXU ) != 0 ) {
72
- perror (uppmHomeDir );
73
- return UPPM_ERROR ;
85
+ if (mkdir (uppmHomeDIR , S_IRWXU ) != 0 ) {
86
+ if (errno != EEXIST ) {
87
+ perror (uppmHomeDIR );
88
+ return UPPM_ERROR ;
89
+ }
74
90
}
75
91
}
76
92
77
- size_t uppmHomeDirLength = strlen (uppmHomeDir );
93
+ size_t uppmHomeDIRLength = strlen (uppmHomeDIR );
94
+
95
+ size_t m = bufSize - 1U ;
96
+
97
+ size_t n = (m > uppmHomeDIRLength ) ? uppmHomeDIRLength : m ;
78
98
79
- size_t n = ( bufSize > uppmHomeDirLength ) ? uppmHomeDirLength : bufSize ;
99
+ strncpy ( buf , uppmHomeDIR , n ) ;
80
100
81
- strncpy ( buf , uppmHomeDir , n ) ;
101
+ buf [ n ] = '\0' ;
82
102
83
103
if (outSize != NULL ) {
84
104
(* outSize ) = n ;
0 commit comments