CLibs
Loading...
Searching...
No Matches
filenames.h
Go to the documentation of this file.
1/*
2 * Utility functions and macros for working with file names.
3 *
4 * Trying to be platform-independent.
5 *
6 *
7 * Created by MacBook on 21.01.2025.
8 */
9
10#ifndef CLIBS_FILENAMES_H
11#define CLIBS_FILENAMES_H
12
13#include "attributes.h"
14
15#include <stdbool.h> /* bool */
16#include <string.h> /* strrchr */
17
18#ifndef __FILE_NAME__
19#define __FILE_NAME__ \
20 ( strrchr( __FILE__, '/' ) ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
21#endif //__FILE_NAME__
22
23
24#ifndef PATH_MAX
25#if defined( __APPLE__ ) // Get PATH_MAX
26#include <sys/syslimits.h>
27#elif defined( __linux__ )
28#include <linux/limits.h>
29#else
31#define PATH_MAX 4096
32#endif
33#endif //ndef PATH_MAX
34
40LibraryDefined char ProgName[ PATH_MAX + 1 ] = "current-program";
41
59LibraryDefined const char *get_file_name( const char *const full_path )
60{
61 const char *last_slash = strrchr( full_path, '/' );
62 if ( last_slash == NULL )
63 return full_path;
64
65 if ( *( last_slash + 1 ) != '\0' )
66 return last_slash + 1;
67
68 /* path ends with '/' -> return the name before if present */
69 for ( const char *ptr = last_slash - 1; ptr >= full_path; --ptr )
70 if ( *ptr == '/' )
71 return ptr + 1;
72
73 return full_path;
74}
75
81LibraryDefined inline const char *get_prog_name( void )
82{
83 return ProgName;
84}
85
92
93
94#if defined( __APPLE__ ) /* not sure if it actually works on iPhones :D */
95#include <err.h> /* warn */
96#include <mach-o/dyld.h> /* _NSGetExecutablePath() */
97#include <stdlib.h> /* malloc */
98
100{
101 char path[ PATH_MAX + 1 ];
102 uint32_t size = sizeof path;
103 if ( _NSGetExecutablePath( path, &size ) != 0 )
104 {
105 char *const alloced = malloc( size );
106 if ( alloced == NULL || _NSGetExecutablePath( path, &size ) != 0 )
107 {
108 warn( "not able to initialize prog name" ); // use std warn to avoid circular import
109 return false;
110 }
111 const char *name = get_file_name( path );
112 strncpy( ProgName, name, size );
113 free( alloced );
114 return true;
115 }
116 const char *name = get_file_name( path );
117 strncpy( ProgName, name, PATH_MAX );
118 return true;
119}
120
121#elif defined( __linux__ )
122#include <unistd.h> /* readlink */
123
125{
126 char path[ PATH_MAX ] = { 0 };
127
128 if ( readlink( "/proc/self/exe", path, PATH_MAX ) == -1 )
129 return true;
130
131 strncpy( ProgName, get_file_name( path ), PATH_MAX );
132 return false;
133}
134
135#endif // set_prog_name()
136
137
138#endif //CLIBS_FILENAMES_H
Function and variable attributes.
#define LibraryDefined
Definition attributes.h:107
#define BeforeMain
Function runs before entering main
Definition attributes.h:75
LibraryDefined char ProgName[PATH_MAX+1]
Definition filenames.h:40
LibraryDefined const char * get_prog_name(void)
Definition filenames.h:81
BeforeMain LibraryDefined bool set_prog_name(void)
LibraryDefined const char * get_file_name(const char *const full_path)
Definition filenames.h:59
#define PATH_MAX
Maximum length of a file name.
Definition filenames.h:31