1111#include "common/pproc.h"
1212#include "common/messages.h"
1313#include "common/inet.h"
14+
1415#include <limits.h>
16+ #include <dirent.h>
1517
1618/*
1719 * returns the last-modified time of the file
@@ -39,11 +41,9 @@ time_t sys_filetime(const char *file) {
3941int sys_search_path (const char * path , const char * file , char * retbuf ) {
4042 const char * ps , * p ;
4143 char cur_path [OS_PATHNAME_SIZE + 1 ];
44+ int found = 0 ;
4245
43- if (path == NULL ) {
44- return 0 ;
45- }
46- if (strlen (path ) == 0 ) {
46+ if (path == NULL || strlen (path ) == 0 ) {
4747 return 0 ;
4848 }
4949 ps = path ;
@@ -64,9 +64,7 @@ int sys_search_path(const char *path, const char *file, char *retbuf) {
6464
6565 // fix home directory
6666 if (cur_path [0 ] == '~' ) {
67- char * old_path ;
68-
69- old_path = malloc (strlen (cur_path ));
67+ char * old_path = malloc (strlen (cur_path ));
7068 strcpy (old_path , cur_path + 1 );
7169#if defined(_UnixOS )
7270 sprintf (cur_path , "%s/%s" , getenv ("HOME" ), old_path );
@@ -79,32 +77,38 @@ int sys_search_path(const char *path, const char *file, char *retbuf) {
7977#endif
8078 free (old_path );
8179 }
82- // build the final file-name
80+
81+ DIR * dp = opendir (cur_path );
82+ if (dp != NULL ) {
83+ struct dirent * entry ;
84+ while (!found && (entry = readdir (dp )) != NULL ) {
85+ if (strcasecmp (entry -> d_name , file ) == 0 ) {
8386#if defined(_UnixOS )
84- strcat (cur_path , "/" );
87+ strcat (cur_path , "/" );
8588#else
86- strcat (cur_path , "\\" );
89+ strcat (cur_path , "\\" );
8790#endif
88- strcat (cur_path , file );
89-
90- if (access (cur_path , R_OK ) == 0 ) {
91- if (retbuf ) {
92- strcpy (retbuf , cur_path );
91+ strcat (cur_path , entry -> d_name );
92+ if (access (cur_path , R_OK ) == 0 ) {
93+ if (retbuf ) {
94+ strcpy (retbuf , cur_path );
95+ }
96+ found = 1 ;
97+ }
98+ }
9399 }
94- return 1 ;
100+ closedir ( dp ) ;
95101 }
96-
97- } while (p );
98-
99- return 0 ;
102+ } while (p && !found );
103+ return found ;
100104}
101105
102106/*
103107 * execute a user's expression (using one variable)
104108 * (note: keyword USE)
105109 *
106110 * var - the variable (the X)
107- * ip - expression's address
111+ * ip - expression's address
108112 */
109113void exec_usefunc (var_t * var , bcip_t ip ) {
110114 var_t * old_x ;
@@ -129,7 +133,7 @@ void exec_usefunc(var_t *var, bcip_t ip) {
129133 *
130134 * var1 - the first variable (the X)
131135 * var2 - the second variable (the Y)
132- * ip - expression's address
136+ * ip - expression's address
133137 */
134138void exec_usefunc2 (var_t * var1 , var_t * var2 , bcip_t ip ) {
135139 var_t * old_x , * old_y ;
@@ -161,7 +165,7 @@ void exec_usefunc2(var_t *var1, var_t *var2, bcip_t ip) {
161165 * var1 - the first variable (the X)
162166 * var2 - the second variable (the Y)
163167 * var3 - the thrid variable (the Z)
164- * ip - expression's address
168+ * ip - expression's address
165169 */
166170void exec_usefunc3 (var_t * var1 , var_t * var2 , var_t * var3 , bcip_t ip ) {
167171 var_t * old_x , * old_y , * old_z ;
@@ -706,7 +710,7 @@ int par_getpoly(pt_t **poly_pp) {
706710 }
707711 return 0 ;
708712 }
709- //
713+ //
710714 el = v_elem (var , 0 );
711715 if (el -> type == V_ARRAY ) {
712716 style = 1 ; // nested --- [ [x1,y1], [x2,y2], ... ]
@@ -817,7 +821,7 @@ int par_getipoly(ipt_t **poly_pp) {
817821 }
818822 return 0 ;
819823 }
820- //
824+ //
821825 el = v_elem (var , 0 );
822826 if (el && el -> type == V_ARRAY ) {
823827 style = 1 ; // nested --- [ [x1,y1], [x2,y2], ... ]
@@ -901,7 +905,7 @@ int par_getipoly(ipt_t **poly_pp) {
901905
902906/*
903907 * returns true if the following code is descibing one var code
904- * usefull for optimization
908+ * usefull for optimization
905909 * (one var can be used by the pointer; more than one it must be evaluated)
906910 */
907911int par_isonevar () {
@@ -970,7 +974,7 @@ int par_getpartable(par_t **ptable_pp, const char *valid_sep) {
970974 case kwTYPE_LEVEL_END : // end of parameters
971975 ready = 1 ;
972976 break ;
973- case kwTYPE_SEP : // separator
977+ case kwTYPE_SEP : // separator
974978 code_skipnext ();
975979
976980 // check parameters separator
@@ -1068,7 +1072,7 @@ int par_massget_type_check(char fmt, par_t *par) {
10681072 * // the string is optional too
10691073 * pc = par_massget("Iis", &i1, &i2, &s1, &v);
10701074 *
1071- * if ( pc != -1 ) {
1075+ * if ( pc != -1 ) {
10721076 * // no error; also, you can use prog_error because par_massget() will call rt_raise() on error
10731077 * printf("required integer = %d\n", i1);
10741078 *
@@ -1098,7 +1102,7 @@ int par_massget(const char *fmt, ...) {
10981102 free (ptable );
10991103 return -1 ;
11001104 }
1101-
1105+
11021106 /*
11031107 * count pars
11041108 */
@@ -1112,7 +1116,7 @@ int par_massget(const char *fmt, ...) {
11121116 }
11131117 fmt_p ++ ;
11141118 }
1115-
1119+
11161120 if (rqcount > pcount ) {
11171121 err_parfmt (fmt );
11181122 }
@@ -1191,7 +1195,7 @@ int par_massget(const char *fmt, ...) {
11911195 case 'P' :
11921196 // variable
11931197 vt = va_arg (ap , var_t * * );
1194- if (ptable [curpar ].flags == 0 )// byref
1198+ if (ptable [curpar ].flags == 0 )// byref
11951199 * vt = ptable [curpar ].var ;
11961200 else {
11971201 err_syntax (-1 , "%P" );
0 commit comments