66#include <stdio.h>
77#include <stdlib.h>
88#include <string.h>
9+ #include <unistd.h>
10+ #include <limits.h>
911
1012#include "platform.h"
1113#include "romimg.h"
1214#include "SonyRX.h"
1315
16+ #define BUFCHK (X ) (X[0] == '\0') ? "" : X
17+ #define IMAGE_COMMENT_BASESIZE 31
18+
1419struct ROMImgStat
1520{
1621 void * image ;
@@ -139,12 +144,7 @@ static int GetExtInfoStat(const struct ROMImgStat *ImageStat, struct RomDirFileF
139144int CreateBlankROMImg (const char * filename , ROMIMG * ROMImg )
140145{
141146 unsigned int CommentLength ;
142- char LocalhostName [32 ], cwd [128 ];
143- #if defined(_WIN32 ) || defined(WIN32 )
144- char UserName [32 ] = "" ;
145- #else
146- char * UserName ;
147- #endif
147+ char LocalhostName [32 ] = "\0" , cwd [MAX_PATH ] = "\0" , UserName [32 ] = "\0" ;
148148 struct FileEntry * ResetFile ;
149149 struct ExtInfoFieldEntry * ExtInfoEntry ;
150150
@@ -154,14 +154,15 @@ int CreateBlankROMImg(const char *filename, ROMIMG *ROMImg)
154154#if defined(_WIN32 ) || defined(WIN32 )
155155 GetUsername (UserName , sizeof (UserName ));
156156#else
157- UserName = getenv ( "USER" );
157+ getlogin_r ( UserName , sizeof ( UserName ) );
158158#endif
159159 GetLocalhostName (LocalhostName , sizeof (LocalhostName ));
160160 GetCurrentWorkingDirectory (cwd , sizeof (cwd ));
161161 /* Comment format: YYYYMMDD-XXXYYY,conffile,<filename>,<user>@<localhost>/<image path> */
162- CommentLength = 31 + strlen (filename ) + strlen (UserName ) + strlen (LocalhostName ) + strlen (cwd );
163- ROMImg -> comment = (char * )malloc (CommentLength );
164- sprintf (ROMImg -> comment , "%08x,conffile,%s,%s@%s/%s" , ROMImg -> date , filename , (UserName [0 ] == '\0' )?"" :UserName , LocalhostName , cwd );
162+ CommentLength = IMAGE_COMMENT_BASESIZE + strlen (filename ) + sizeof (LocalhostName ) + sizeof (UserName ) + MAX_PATH ;
163+ ROMImg -> comment = (char * )calloc (0 , CommentLength + 1 );
164+ if (!ROMImg -> comment ) return ENOMEM ;
165+ snprintf (ROMImg -> comment , CommentLength , "%08x,conffile,%s,%s@%s/%s" , ROMImg -> date , filename , BUFCHK (UserName ), BUFCHK (LocalhostName ), cwd );
165166
166167 // Create a blank RESET file.
167168 ROMImg -> NumFiles = 1 ;
0 commit comments