bulk changes
This commit is contained in:
36
core/file.c
36
core/file.c
@@ -1,4 +1,5 @@
|
||||
#include "file.h"
|
||||
#include "../config.h"
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
@@ -6,38 +7,39 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
int
|
||||
touch (char *path, char *format, ...)
|
||||
create_file_with_content (char *path, char *format, ...)
|
||||
{
|
||||
FILE *fp = fopen (path, "w");
|
||||
if (!fp)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
vfprintf (fp, format, args);
|
||||
va_end (args);
|
||||
return -1;
|
||||
}
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
vfprintf (fp, format, args);
|
||||
va_end (args);
|
||||
|
||||
fclose (fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
dir (char *format, ...)
|
||||
create_directory (char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
|
||||
char path[1024];
|
||||
vsnprintf (path, sizeof (path), format, args);
|
||||
|
||||
char path[MAX_PATH_LENGTH];
|
||||
int result = vsnprintf (path, sizeof (path), format, args);
|
||||
va_end (args);
|
||||
|
||||
if (mkdir (path, 0777) < 0)
|
||||
/* Check if the path was truncated */
|
||||
if (result >= (int)sizeof (path))
|
||||
{
|
||||
return ENAMETOOLONG;
|
||||
}
|
||||
|
||||
if (mkdir (path, DEFAULT_DIR_PERMISSIONS) < 0)
|
||||
{
|
||||
return errno;
|
||||
}
|
||||
@@ -46,9 +48,9 @@ dir (char *format, ...)
|
||||
}
|
||||
|
||||
int
|
||||
take (const char *dirname)
|
||||
create_and_enter_directory (const char *dirname)
|
||||
{
|
||||
int err = dir ("%s", dirname);
|
||||
int err = create_directory ("%s", dirname);
|
||||
if (err)
|
||||
{
|
||||
return err;
|
||||
|
||||
29
core/file.h
29
core/file.h
@@ -3,9 +3,32 @@
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int take(const char *dirname);
|
||||
/* Constants for file operations */
|
||||
#define DEFAULT_DIR_PERMISSIONS 0755
|
||||
#define MAX_PATH_LENGTH 1024
|
||||
|
||||
int touch(char *, char *, ...);
|
||||
int dir(char *, ...);
|
||||
/**
|
||||
* Create directory and change into it
|
||||
* @param dirname Directory name to create and enter
|
||||
* @return 0 on success, errno on failure
|
||||
*/
|
||||
int create_and_enter_directory(const char *dirname);
|
||||
|
||||
/**
|
||||
* Create a file with formatted content
|
||||
* @param path File path to create
|
||||
* @param format Format string for file content
|
||||
* @param ... Variable arguments for formatting
|
||||
* @return 0 on success, -1 on failure
|
||||
*/
|
||||
int create_file_with_content(char *path, char *format, ...);
|
||||
|
||||
/**
|
||||
* Create a directory with formatted path
|
||||
* @param format Format string for directory path
|
||||
* @param ... Variable arguments for formatting
|
||||
* @return 0 on success, errno on failure
|
||||
*/
|
||||
int create_directory(char *format, ...);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
printfn (char *format, ...)
|
||||
print_error_with_prefix (char *format, ...)
|
||||
{
|
||||
int len;
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
fprintf (stderr, "yait: ");
|
||||
len = vfprintf (stderr, format, args);
|
||||
putchar ('\n');
|
||||
fprintf (stderr, "\n"); /* Use stderr consistently */
|
||||
va_end (args);
|
||||
return len;
|
||||
}
|
||||
|
||||
11
core/print.h
11
core/print.h
@@ -4,6 +4,15 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int printfn(char *, ...);
|
||||
/**
|
||||
* Print a formatted message to stderr with program prefix and newline
|
||||
* @param format Format string (printf-style)
|
||||
* @param ... Variable arguments for formatting
|
||||
* @return Number of characters printed
|
||||
*/
|
||||
int print_error_with_prefix(char *format, ...);
|
||||
|
||||
/* Legacy function name for backward compatibility */
|
||||
#define printfn print_error_with_prefix
|
||||
|
||||
#endif
|
||||
|
||||
@@ -12,7 +12,7 @@ parse_standard_options (void (*usage) (int), int argc, char **argv)
|
||||
if (strcmp (argv[i], "--help") == 0)
|
||||
{
|
||||
usage (0);
|
||||
exit (0);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
else if (strcmp (argv[i], "--version") == 0)
|
||||
{
|
||||
@@ -20,8 +20,8 @@ parse_standard_options (void (*usage) (int), int argc, char **argv)
|
||||
"you are free to change and redistribute it.\nThere is NO "
|
||||
"WARRNTY, to the extent permitted by law.\n",
|
||||
PROGRAM, VERSION, COMMIT, YEAR, AUTHORS, LICENSE_LINE);
|
||||
exit (0);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
return HELP_REQUESTED;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
#ifndef STANDARD_H
|
||||
#define STANDARD_H
|
||||
|
||||
int parse_standard_options(void (*)(), int argc, char **argv);
|
||||
/**
|
||||
* Parse standard command line options (--help, --version)
|
||||
* @param usage_func Function pointer to usage display function
|
||||
* @param argc Argument count
|
||||
* @param argv Argument vector
|
||||
* @return 0 on success, 1 if help/version requested, errno on error
|
||||
*/
|
||||
int parse_standard_options(void (*usage_func)(), int argc, char **argv);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user