3
3
*
4
4
* Copyright (c) 2000-2007, PostgreSQL Global Development Group
5
5
*
6
- * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.49 2007/01/05 22:19:49 momjian Exp $
6
+ * $PostgreSQL: pgsql/src/bin/psql/prompt.c,v 1.50 2007/02/08 11:10:27 petere Exp $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
@@ -96,18 +96,18 @@ get_prompt(promptStatus_t status)
96
96
destination [0 ] = '\0' ;
97
97
98
98
for (p = prompt_string ;
99
- * p && strlen (destination ) < MAX_PROMPT_SIZE ;
99
+ * p && strlen (destination ) < sizeof ( destination ) - 1 ;
100
100
p ++ )
101
101
{
102
- memset (buf , 0 , MAX_PROMPT_SIZE + 1 );
102
+ memset (buf , 0 , sizeof ( buf ) );
103
103
if (esc )
104
104
{
105
105
switch (* p )
106
106
{
107
107
/* Current database */
108
108
case '/' :
109
109
if (pset .db )
110
- strncpy (buf , PQdb (pset .db ), MAX_PROMPT_SIZE );
110
+ strlcpy (buf , PQdb (pset .db ), sizeof ( buf ) );
111
111
break ;
112
112
case '~' :
113
113
if (pset .db )
@@ -116,9 +116,9 @@ get_prompt(promptStatus_t status)
116
116
117
117
if (strcmp (PQdb (pset .db ), PQuser (pset .db )) == 0 ||
118
118
((var = getenv ("PGDATABASE" )) && strcmp (var , PQdb (pset .db )) == 0 ))
119
- strcpy (buf , "~" );
119
+ strlcpy (buf , "~" , sizeof ( buf ) );
120
120
else
121
- strncpy (buf , PQdb (pset .db ), MAX_PROMPT_SIZE );
121
+ strlcpy (buf , PQdb (pset .db ), sizeof ( buf ) );
122
122
}
123
123
break ;
124
124
@@ -132,7 +132,7 @@ get_prompt(promptStatus_t status)
132
132
/* INET socket */
133
133
if (host && host [0 ] && !is_absolute_path (host ))
134
134
{
135
- strncpy (buf , host , MAX_PROMPT_SIZE );
135
+ strlcpy (buf , host , sizeof ( buf ) );
136
136
if (* p == 'm' )
137
137
buf [strcspn (buf , "." )] = '\0' ;
138
138
}
@@ -143,22 +143,22 @@ get_prompt(promptStatus_t status)
143
143
if (!host
144
144
|| strcmp (host , DEFAULT_PGSOCKET_DIR ) == 0
145
145
|| * p == 'm' )
146
- strncpy (buf , "[local]" , MAX_PROMPT_SIZE );
146
+ strlcpy (buf , "[local]" , sizeof ( buf ) );
147
147
else
148
- snprintf (buf , MAX_PROMPT_SIZE , "[local:%s]" , host );
148
+ snprintf (buf , sizeof ( buf ) , "[local:%s]" , host );
149
149
}
150
150
#endif
151
151
}
152
152
break ;
153
153
/* DB server port number */
154
154
case '>' :
155
155
if (pset .db && PQport (pset .db ))
156
- strncpy (buf , PQport (pset .db ), MAX_PROMPT_SIZE );
156
+ strlcpy (buf , PQport (pset .db ), sizeof ( buf ) );
157
157
break ;
158
158
/* DB server user name */
159
159
case 'n' :
160
160
if (pset .db )
161
- strncpy (buf , session_username (), MAX_PROMPT_SIZE );
161
+ strlcpy (buf , session_username (), sizeof ( buf ) );
162
162
break ;
163
163
164
164
case '0' :
@@ -252,7 +252,7 @@ get_prompt(promptStatus_t status)
252
252
fd = popen (file , "r" );
253
253
if (fd )
254
254
{
255
- fgets (buf , MAX_PROMPT_SIZE - 1 , fd );
255
+ fgets (buf , sizeof ( buf ) , fd );
256
256
pclose (fd );
257
257
}
258
258
if (strlen (buf ) > 0 && buf [strlen (buf ) - 1 ] == '\n' )
@@ -274,7 +274,7 @@ get_prompt(promptStatus_t status)
274
274
name [nameend ] = '\0' ;
275
275
val = GetVariable (pset .vars , name );
276
276
if (val )
277
- strncpy (buf , val , MAX_PROMPT_SIZE );
277
+ strlcpy (buf , val , sizeof ( buf ) );
278
278
free (name );
279
279
p += nameend + 1 ;
280
280
break ;
@@ -312,9 +312,8 @@ get_prompt(promptStatus_t status)
312
312
}
313
313
314
314
if (!esc )
315
- strncat (destination , buf , MAX_PROMPT_SIZE - strlen (destination ));
315
+ strlcat (destination , buf , sizeof (destination ));
316
316
}
317
317
318
- destination [MAX_PROMPT_SIZE ] = '\0' ;
319
318
return destination ;
320
319
}
0 commit comments