The following program makes random files. By default, they are of the form /tmp/rfile-XXX
. Certain options allow for the user to specify where these files are placed(using the -d dir
flag) or what name is used to generate (using the -n name
flag, which generates name-XXX
) and then uses mkstemp(3)
followed by fopen(3)
In context of this review I am interested in the following:
How can I make this code more efficient? I have to repeat a lot of checks (like the
if(print_flag == USER)
to test if the user wants a more verbose output or having to testdir_flag
multiple times to determine what directory name and length thereof. Would it be better in the 2nd implementation to usepthreads
to create each file, and just generate a list of/dir/name-XXX
for each thread to act on?Error handling and security, what needs to be changed or added to handle other failures or problems? Are there other functions that I need to check for error? In version 2 I want to implement a check of
fullname_len
against the maximum pathname(I don't recall the variable/value). What else could potentially error out, or otherwise cause problems?I tried to keep the formatting consistent throughout but I would like some input on that. I know my code (intentionally) looks more like the BSD
style(9)
guide, and I don't want to hear if other style standards/guides are better. I just want to know if it's consistent and conducive to code reviewing/reading.
/* FILE: mkrfiles.c
AUTHOR: Jordan Effinger
DATE: 1 May 2023
VERSION: 0
LICENSE: BSD-3 Clause
COPYRIGHT<2023><Jordan Effinger>
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
COMMENT: This program was written as an exercise in argument parsing, error handling, user i/o and in printing verbose error messages. It was written on a MacOS 10.15.7, with Apple clang 12.0.0 on an x86_64 system.
*/
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/errno.h>
#define DFLT 0
#define USER 1
void
print_error(char *, pid_t, char *, unsigned, unsigned);
unsigned uerrno = 0;
int
main(int argc, char **argv)
{
// Debugging information:
pid_t main_pid = getpid();
char *exec_name = calloc( strlen(argv[0]), sizeof(char) );
if(exec_name == NULL)
{
print_error(argv[0], main_pid, __FILE__, __LINE__, errno);
return(errno);
}
// Default settings:
const char *dir_dflt = "/tmp";
const char *name_dflt = "rfile";
const char *fullname_dflt = "/tmp/rfile-XXX";
const size_t dir_len_dflt = strlen(dir_dflt);
const size_t name_len_dflt = strlen(name_dflt);
const size_t fullname_len_dflt = strlen(fullname_dflt)+1;
// User set settings:
char *dir_user, *name_user;
size_t dir_len, name_len, fullname_len = 0;
unsigned long count_user;
int count_flag =0;
int dir_flag = 0;
int name_flag = 0;
int print_flag = 0;
int ch;
while( (ch = getopt(argc, argv, "c:d:hn:p")) != -1)
{
switch(ch)
{
case 'c':
count_flag = USER;
count_user = strtoul(optarg,NULL,0);
if(errno != 0)
{
print_error(exec_name, main_pid, __FILE__, __LINE__, errno);
free(exec_name);
return(errno);
break;
}
break;
case 'd':
dir_flag = USER;
dir_len = strlen(optarg);
dir_user = calloc(dir_len, sizeof(char) );
if(dir_user == NULL)
{
print_error(exec_name, main_pid, __FILE__, __LINE__, errno);
free(exec_name);
return(errno);
break;
}
stpncpy(dir_user, optarg, dir_len);
break;
case 'h':
printf("%s -cdhnp\n", exec_name);
printf("-c file count\n");
printf("-d dir name\n");
printf("-h help\n");
printf("-n file name template\n");
printf("-p print (verbose)\n");
return(-1);
break;
case 'n':
name_flag = USER;
name_len = strlen(optarg);
name_user = calloc(name_len, sizeof(char) );
if(name_user == NULL)
{
print_error(exec_name, main_pid, __FILE__, __LINE__, errno);
free(exec_name);
return(errno);
break;
}
stpncpy(name_user, optarg, name_len);
break;
case 'p':
print_flag = USER;
break;
default:
print_error(exec_name, main_pid, __FILE__, __LINE__, EINVAL);
printf("Execute with -h for a usage menu.\n");
free(exec_name);
return(EINVAL);
break;
}
}
if(print_flag == USER)
{
printf("Determining filename length.\n");
}
if( dir_flag == USER && name_flag == USER)
{
fullname_len = dir_len + strlen("/") + name_len + strlen("-XXX") + 1;
}
else if(dir_flag == USER && name_flag == DFLT)
{
fullname_len = dir_len + strlen("/") + name_len_dflt + strlen("-XXX") + 1;
}
else if(dir_flag == DFLT && name_flag == USER)
{
fullname_len = dir_len_dflt + strlen("/") + name_len + strlen("-XXX") + 1;
}
else if(dir_flag == DFLT && name_flag == DFLT)
{
fullname_len = fullname_len_dflt;
}
if(print_flag == USER)
{
printf("Allocating space for filename: %zu = %zu(strlen) & %zu(char size)\n", fullname_len * sizeof(char), fullname_len, sizeof(char) );
}
char *fullname = calloc(fullname_len, sizeof(char));
if(fullname == NULL)
{
print_error(exec_name, main_pid, __FILE__, __LINE__, errno);
free(exec_name);
free(dir_user);
free(name_user);
return(uerrno);
}
if(print_flag == USER)
{
printf("Building filename:\n");
}
if(dir_flag == USER)
{
stpncpy(fullname, dir_user, dir_len);
}
else
{
stpncpy(fullname, dir_dflt, dir_len_dflt);
}
strcat(fullname,"/");
if(name_flag == USER)
{
strncat(fullname, name_user, name_len);
}
else
{
strncat(fullname, name_dflt, name_len_dflt);
}
strncat(fullname, "-XXX", strlen("-XXX") );
FILE *fd;
if(count_flag == USER)
{
if(print_flag == USER)
{
printf("Creating %zu random files.\n", count_user);
}
for( unsigned long idx = 0; idx < count_user; idx++ )
{
mkstemp(fullname);
fd = fopen(fullname, "w");
if(fd == NULL)
{
print_error(exec_name, main_pid, __FILE__, __LINE__, errno);
free(exec_name);
free(dir_user);
free(name_user);
free(fullname);
return(errno);
}
fclose(fd);
fullname[fullname_len-2] = 'X';
fullname[fullname_len-3] = 'X';
fullname[fullname_len-4] = 'X';
}
}
else
{
if(print_flag == USER)
{
printf("Creating random file.\n");
}
mkstemp(fullname);
fd = fopen(fullname, "w");
if(fd == NULL)
{
print_error(exec_name, main_pid, __FILE__, __LINE__, errno);
free(exec_name);
free(dir_user);
free(name_user);
free(fullname);
return(errno);
}
fclose(fd);
}
free(exec_name);
free(dir_user);
free(name_user);
free(fullname);
return(uerrno);
}
void
print_error(char *progname, pid_t main_pid, char *file, unsigned line, unsigned errnum)
{
fprintf(stderr, "[%s, %u] [%s, line:%u], Errno: %u, Error: %s\n",
progname, main_pid, file, line, errnum, strerror(errnum)
);
}
```
strncat
/strcat
is way too inefficient. See Shlemiel the painter’s algorithm: joelonsoftware.com/2001/12/11/back-to-basics \$\endgroup\$mkstemp()
? \$\endgroup\$