my_manage.c 18 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
  Copyright (c) 2003 Novell, Inc. All Rights Reserved. 

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, or 
  (at your option) any later version. 

  This program is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  GNU General Public License for more details. 

  You should have received a copy of the GNU General Public License 
  along with this program; if not, write to the Free Software 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ 

#include <stdio.h>
#include <errno.h>
#ifndef __WIN__
#include <dirent.h>
#endif
#include <string.h>
#ifdef __NETWARE__
#include <screen.h>
#include <proc.h>
#else
#include <sys/types.h>
#ifndef __WIN__
#include <sys/wait.h>
#include <unistd.h>
33 34
#include <signal.h>
#include <fnmatch.h>                            /* FIXME HAVE_FNMATCH_H or something */
unknown's avatar
unknown committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#else
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#endif
#endif
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>

#include "my_manage.h"

#ifndef __NETWARE__
#define ASSERT assert
extern char **environ;
#endif



/******************************************************************************

unknown's avatar
unknown committed
57 58
        macros

unknown's avatar
unknown committed
59 60 61 62
******************************************************************************/

/******************************************************************************

unknown's avatar
unknown committed
63 64
        global variables

unknown's avatar
unknown committed
65 66 67 68
******************************************************************************/

/******************************************************************************

unknown's avatar
unknown committed
69 70
        functions

unknown's avatar
unknown committed
71 72 73 74
******************************************************************************/

/******************************************************************************

unknown's avatar
unknown committed
75 76 77
        init_args()

        Init an argument list.
unknown's avatar
unknown committed
78 79

******************************************************************************/
unknown's avatar
unknown committed
80

unknown's avatar
unknown committed
81 82 83
void init_args(arg_list_t *al)
{
  ASSERT(al != NULL);
unknown's avatar
unknown committed
84 85 86 87

  al->argc= 0;
  al->size= ARG_BUF;
  al->argv= malloc(al->size * sizeof(char *));
unknown's avatar
unknown committed
88 89 90 91 92 93 94
  ASSERT(al->argv != NULL);

  return;
}

/******************************************************************************

unknown's avatar
unknown committed
95 96 97
        add_arg()

        Add an argument to a list.
unknown's avatar
unknown committed
98 99

******************************************************************************/
unknown's avatar
unknown committed
100

unknown's avatar
unknown committed
101 102 103
void add_arg(arg_list_t *al, const char *format, ...)
{
  va_list ap;
104
  char temp[FN_REFLEN];
unknown's avatar
unknown committed
105 106 107

  ASSERT(al != NULL);

unknown's avatar
unknown committed
108
  /* increase size */
unknown's avatar
unknown committed
109 110
  if (al->argc >= (int)al->size)
  {
unknown's avatar
unknown committed
111 112
    al->size+= ARG_BUF;
    al->argv= realloc(al->argv, al->size * sizeof(char *));
unknown's avatar
unknown committed
113 114 115 116 117 118 119 120 121
    ASSERT(al->argv != NULL);
  }

  if (format)
  {
    va_start(ap, format);
    vsprintf(temp, format, ap);
    va_end(ap);

unknown's avatar
unknown committed
122
    al->argv[al->argc]= malloc(strlen(temp)+1);
unknown's avatar
unknown committed
123 124
    ASSERT(al->argv[al->argc] != NULL);
    strcpy(al->argv[al->argc], temp);
unknown's avatar
merge  
unknown committed
125
    
unknown's avatar
unknown committed
126 127 128 129
    ++(al->argc);
  }
  else
  {
unknown's avatar
unknown committed
130
    al->argv[al->argc]= NULL;
unknown's avatar
unknown committed
131 132 133 134 135 136 137
  }

  return;
}

/******************************************************************************

unknown's avatar
unknown committed
138 139 140
        free_args()

        Free an argument list.
unknown's avatar
unknown committed
141 142

******************************************************************************/
unknown's avatar
unknown committed
143

unknown's avatar
unknown committed
144 145 146 147 148 149
void free_args(arg_list_t *al)
{
  int i;

  ASSERT(al != NULL);

unknown's avatar
unknown committed
150
  for (i= 0; i < al->argc; i++)
unknown's avatar
unknown committed
151 152 153
  {
    ASSERT(al->argv[i] != NULL);
    free(al->argv[i]);
unknown's avatar
unknown committed
154
    al->argv[i]= NULL;
unknown's avatar
unknown committed
155 156 157
  }

  free(al->argv);
unknown's avatar
unknown committed
158 159
  al->argc= 0;
  al->argv= NULL;
unknown's avatar
unknown committed
160 161 162 163 164 165

  return;
}

/******************************************************************************

unknown's avatar
unknown committed
166 167 168
        sleep_until_file_deleted()

        Sleep until the given file is no longer found.
unknown's avatar
unknown committed
169 170

******************************************************************************/
unknown's avatar
unknown committed
171

unknown's avatar
unknown committed
172 173 174 175 176 177
#ifndef __WIN__
int sleep_until_file_deleted(char *pid_file)
#else
int sleep_until_file_deleted(HANDLE pid_file)
#endif
{
unknown's avatar
unknown committed
178
  int err= 0;            /* Initiate to supress warning */
unknown's avatar
unknown committed
179
#ifndef __WIN__
unknown's avatar
unknown committed
180 181 182 183 184 185
  struct stat buf;
  int i;

  for (i= 0; (i < TRY_MAX) && (err= !stat(pid_file, &buf)); i++) sleep(1);

  if (err != 0) err= errno;
unknown's avatar
unknown committed
186 187 188
#else
  err= (WaitForSingleObject(pid_file, TRY_MAX*1000) == WAIT_TIMEOUT);
#endif
unknown's avatar
unknown committed
189
  return err;
unknown's avatar
unknown committed
190 191 192 193
}

/******************************************************************************

unknown's avatar
unknown committed
194
        sleep_until_file_exists()
unknown's avatar
unknown committed
195

unknown's avatar
unknown committed
196
        Sleep until the given file exists.
unknown's avatar
unknown committed
197 198

******************************************************************************/
unknown's avatar
unknown committed
199

unknown's avatar
unknown committed
200 201 202 203 204 205
#ifndef __WIN__
int sleep_until_file_exists(char *pid_file)
#else
int sleep_until_file_exists(HANDLE pid_file)
#endif
{
unknown's avatar
unknown committed
206
  int err= 0;            /* Initiate to supress warning */
unknown's avatar
unknown committed
207
#ifndef __WIN__
unknown's avatar
unknown committed
208 209 210 211 212 213
  struct stat buf;
  int i;

  for (i= 0; (i < TRY_MAX) && (err= stat(pid_file, &buf)); i++) sleep(1);

  if (err != 0) err= errno;
unknown's avatar
unknown committed
214 215 216
#else
  err= (WaitForSingleObject(pid_file, TRY_MAX*1000) == WAIT_TIMEOUT);
#endif
unknown's avatar
unknown committed
217
  return err;
unknown's avatar
unknown committed
218 219 220 221
}

/******************************************************************************

unknown's avatar
unknown committed
222 223 224
        wait_for_server_start()

        Wait for the server on the given port to start.
unknown's avatar
unknown committed
225 226

******************************************************************************/
unknown's avatar
unknown committed
227 228 229 230

int wait_for_server_start(char *bin_dir __attribute__((unused)),
                          char *mysqladmin_file,
                          char *user, char *password, int port,char *tmp_dir)
unknown's avatar
unknown committed
231 232
{
  arg_list_t al;
unknown's avatar
unknown committed
233
  int err= 0, i;
234
  char trash[FN_REFLEN];
unknown's avatar
unknown committed
235 236

  /* mysqladmin file */
237
  snprintf(trash, FN_REFLEN, "%s/trash.out",tmp_dir);
unknown's avatar
unknown committed
238 239

  /* args */
unknown's avatar
unknown committed
240 241 242 243 244 245 246 247
  init_args(&al);
  add_arg(&al, "%s", mysqladmin_file);
  add_arg(&al, "--no-defaults");
  add_arg(&al, "--port=%u", port);
  add_arg(&al, "--user=%s", user);
  add_arg(&al, "--password=%s", password);
  add_arg(&al, "--silent");

unknown's avatar
unknown committed
248
/* #ifdef NOT_USED */
unknown's avatar
unknown committed
249 250 251 252 253 254 255 256 257
#ifndef __NETWARE__
  add_arg(&al, "-O");
  add_arg(&al, "connect_timeout=10");
  add_arg(&al, "-w");
#endif

  add_arg(&al, "--host=localhost");
#ifndef __NETWARE__
  add_arg(&al, "--protocol=tcp");
unknown's avatar
unknown committed
258
#endif
unknown's avatar
unknown committed
259 260
  add_arg(&al, "ping");

unknown's avatar
unknown committed
261 262 263 264
  /*
    NetWare does not support the connect timeout in the TCP/IP stack
    -- we will try the ping multiple times
  */
unknown's avatar
unknown committed
265
#ifndef __WIN__
unknown's avatar
unknown committed
266 267 268
  for (i= 0; (i < TRY_MAX)
         && (err= spawn(mysqladmin_file, &al, TRUE, NULL,
                        trash, NULL, NULL)); i++) sleep(1);
unknown's avatar
unknown committed
269
#else
unknown's avatar
unknown committed
270
  err= spawn(mysqladmin_file, &al, TRUE, NULL,trash, NULL, NULL);
unknown's avatar
unknown committed
271 272
#endif

unknown's avatar
unknown committed
273
  /* free args */
unknown's avatar
unknown committed
274 275 276 277 278 279 280
  free_args(&al);

  return err;
}

/******************************************************************************

unknown's avatar
unknown committed
281 282 283
        spawn()

        Spawn the given path with the given arguments.
unknown's avatar
unknown committed
284 285

******************************************************************************/
unknown's avatar
unknown committed
286

unknown's avatar
unknown committed
287 288 289 290
#ifdef __NETWARE__
int spawn(char *path, arg_list_t *al, int join, char *input,
          char *output, char *error, char *pid_file)
{
unknown's avatar
unknown committed
291 292 293 294
  pid_t pid;
  int result= 0;
  wiring_t wiring= { FD_UNUSED, FD_UNUSED, FD_UNUSED };
  unsigned long flags= PROC_CURRENT_SPACE | PROC_INHERIT_CWD;
unknown's avatar
unknown committed
295

unknown's avatar
unknown committed
296
  /* open wiring */
unknown's avatar
unknown committed
297
  if (input)
unknown's avatar
unknown committed
298
    wiring.infd= open(input, O_RDONLY);
unknown's avatar
unknown committed
299 300

  if (output)
unknown's avatar
unknown committed
301
    wiring.outfd= open(output, O_WRONLY | O_CREAT | O_TRUNC);
unknown's avatar
unknown committed
302 303

  if (error)
unknown's avatar
unknown committed
304
    wiring.errfd= open(error, O_WRONLY | O_CREAT | O_TRUNC);
unknown's avatar
unknown committed
305

unknown's avatar
unknown committed
306
  /* procve requires a NULL */
unknown's avatar
unknown committed
307 308
  add_arg(al, NULL);

unknown's avatar
unknown committed
309 310 311
  /* go */
  pid= procve(path, flags, NULL, &wiring, NULL, NULL, 0,
              NULL, (const char **)al->argv);
unknown's avatar
unknown committed
312

unknown's avatar
unknown committed
313
  /* close wiring */
unknown's avatar
unknown committed
314 315 316 317 318 319 320 321 322
  if (wiring.infd != -1)
    close(wiring.infd);

  if (wiring.outfd != -1)
    close(wiring.outfd);

  if (wiring.errfd != -1)
    close(wiring.errfd);

unknown's avatar
unknown committed
323
  return result;
unknown's avatar
unknown committed
324 325 326 327 328 329
}
#elif __WIN__

int spawn(char *path, arg_list_t *al, int join, char *input,
          char *output, char *error, HANDLE *pid)
{
unknown's avatar
unknown committed
330
  bool result;
unknown's avatar
unknown committed
331 332 333 334 335 336
  int i;
  STARTUPINFO startup_info;
  PROCESS_INFORMATION process_information;
  DWORD exit_code;
  char win_args[1024]= "";

unknown's avatar
unknown committed
337 338
  /* Skip the first parameter */
  for (i= 1; i < al->argc; i++)
unknown's avatar
unknown committed
339 340 341 342
  {
    ASSERT(al->argv[i] != NULL);
    strcat(win_args,al->argv[i]);
    strcat(win_args," ");
unknown's avatar
unknown committed
343
  }
unknown's avatar
unknown committed
344 345

  memset(&startup_info,0,sizeof(STARTUPINFO));
unknown's avatar
unknown committed
346
  startup_info.cb= sizeof(STARTUPINFO);
unknown's avatar
unknown committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

  if (input)
    freopen(input, "rb", stdin);

  if (output)
    freopen(output, "wb", stdout);

  if (error)
    freopen(error, "wb", stderr);

  result= CreateProcess(
    path,
    (LPSTR)&win_args,
    NULL,
    NULL,
    TRUE,
    0,
    NULL,
    NULL,
    &startup_info,
    &process_information
  );

  if (result && process_information.hProcess)
  {
    if (join)
    {
unknown's avatar
unknown committed
374 375
      if (WaitForSingleObject(process_information.hProcess, mysqld_timeout)
          == WAIT_TIMEOUT)
unknown's avatar
unknown committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
      {
        exit_code= -1;
      }
      else
      {
        GetExitCodeProcess(process_information.hProcess, &exit_code);
      }
      CloseHandle(process_information.hProcess);
    }
    else
    {
      exit_code= 0;
    }
    if (pid != NULL)
      *pid= process_information.hProcess;
  }
  else
  {
    exit_code= -1;
  }
  if (input)
    freopen("CONIN$","rb",stdin);
  if (output)
    freopen("CONOUT$","wb",stdout);
  if (error)
    freopen("CONOUT$","wb",stderr);

  return exit_code;
}
#else
int spawn(char *path, arg_list_t *al, int join, char *input,
unknown's avatar
unknown committed
407
          char *output, char *error, char *pid_file __attribute__((unused)))
unknown's avatar
unknown committed
408 409
{
  pid_t pid;
unknown's avatar
unknown committed
410 411 412 413 414
  int res_exec= 0;
  int result= 0;

  pid= fork();

unknown's avatar
unknown committed
415 416 417
  if (pid == -1)
  {
    fprintf(stderr, "fork was't created\n");
unknown's avatar
unknown committed
418 419
    /* We can't create the fork...exit with error */
    return EXIT_FAILURE;
unknown's avatar
unknown committed
420
  }
unknown's avatar
unknown committed
421

unknown's avatar
unknown committed
422 423
  if (pid  > 0)
  {
unknown's avatar
unknown committed
424
    /* The parent process is waiting for child process if join is not zero */
unknown's avatar
unknown committed
425 426
    if (join)
    {
unknown's avatar
unknown committed
427 428 429 430 431 432 433 434 435 436
      waitpid(pid, &result, 0);
      if (WIFEXITED(result) != 0)
      {
        result= WEXITSTATUS(result);
      }
      else
      {
        result= EXIT_FAILURE;
      }
    }
unknown's avatar
unknown committed
437 438 439 440
  }
  else
  {

unknown's avatar
unknown committed
441 442
    /* Child process */
    add_arg(al, NULL);
unknown's avatar
unknown committed
443

unknown's avatar
unknown committed
444
    /* Reassign streams */
unknown's avatar
unknown committed
445
    if (input)
unknown's avatar
unknown committed
446
      freopen(input, "r", stdin);
unknown's avatar
unknown committed
447 448 449 450 451 452 453 454

    if (output)
      freopen(output, "w", stdout);

    if (error)
      freopen(error, "w", stderr);

    /* Spawn the process */
unknown's avatar
unknown committed
455
    if ((res_exec= execve(path, al->argv, environ)) < 0)
unknown's avatar
unknown committed
456 457
      exit(EXIT_FAILURE);

unknown's avatar
unknown committed
458
    /* Restore streams */
unknown's avatar
unknown committed
459
    if (input)
unknown's avatar
unknown committed
460
      freopen("/dev/tty", "r", stdin);
unknown's avatar
unknown committed
461 462 463 464 465 466 467 468 469

    if (output)
      freopen("/dev/tty", "w", stdout);

    if (error)
      freopen("/dev/tty", "w", stderr);

    exit(0);
  }
unknown's avatar
unknown committed
470

unknown's avatar
unknown committed
471 472 473 474 475
  return result;
}
#endif
/******************************************************************************

unknown's avatar
unknown committed
476 477 478
        stop_server()

        Stop the server with the given port and pid file.
unknown's avatar
unknown committed
479 480

******************************************************************************/
unknown's avatar
unknown committed
481 482 483

int stop_server(char *bin_dir __attribute__((unused)), char *mysqladmin_file,
                char *user, char *password, int port,
unknown's avatar
unknown committed
484
#ifndef __WIN__
unknown's avatar
unknown committed
485
                char *pid_file,
unknown's avatar
unknown committed
486
#else
unknown's avatar
unknown committed
487
                HANDLE pid_file,
unknown's avatar
unknown committed
488
#endif
unknown's avatar
unknown committed
489
                char *tmp_dir)
unknown's avatar
unknown committed
490 491
{
  arg_list_t al;
unknown's avatar
unknown committed
492
  int err= 0;
493
  char trash[FN_REFLEN];
unknown's avatar
unknown committed
494

495
  snprintf(trash, FN_REFLEN, "%s/trash.out",tmp_dir);
unknown's avatar
unknown committed
496 497

  /* args */
unknown's avatar
unknown committed
498 499 500 501 502 503 504 505 506 507
  init_args(&al);
  add_arg(&al, "%s", mysqladmin_file);
  add_arg(&al, "--no-defaults");
  add_arg(&al, "--port=%u", port);
  add_arg(&al, "--user=%s", user);
  add_arg(&al, "--password=%s", password);
  add_arg(&al, "-O");
  add_arg(&al, "shutdown_timeout=20");
#ifndef __NETWARE__
  add_arg(&al, "--protocol=tcp");
unknown's avatar
unknown committed
508
#endif
unknown's avatar
unknown committed
509 510
  add_arg(&al, "shutdown");

unknown's avatar
unknown committed
511 512 513
  /* spawn */
  if ((err= spawn(mysqladmin_file, &al, TRUE, NULL,
                  trash, NULL, NULL)) == 0)
unknown's avatar
unknown committed
514 515 516 517 518 519
  {
    sleep_until_file_deleted(pid_file);
  }
  else
  {
#ifndef __WIN__
unknown's avatar
unknown committed
520 521 522
    pid_t pid= get_server_pid(pid_file);

    /* shutdown failed - kill server */
unknown's avatar
unknown committed
523
   kill_server(pid);
unknown's avatar
unknown committed
524

unknown's avatar
unknown committed
525
   sleep(TRY_MAX);
unknown's avatar
unknown committed
526 527 528

   /* remove pid file if possible */
   err= remove(pid_file);
unknown's avatar
unknown committed
529 530 531 532
#else
  TerminateProcess(pid_file,err);
#endif
  }
unknown's avatar
unknown committed
533 534

  /* free args */
unknown's avatar
unknown committed
535 536 537 538 539 540 541
  free_args(&al);

  return err;
}

/******************************************************************************

unknown's avatar
unknown committed
542 543 544
        get_server_pid()

        Get the VM id with the given pid file.
unknown's avatar
unknown committed
545 546

******************************************************************************/
unknown's avatar
unknown committed
547

unknown's avatar
unknown committed
548 549 550
#ifndef __WIN__
pid_t get_server_pid(char *pid_file)
{
551
  char buf[FN_REFLEN];
unknown's avatar
unknown committed
552 553
  int fd, err;
  char *p;
unknown's avatar
unknown committed
554 555 556 557 558
  pid_t id= 0;

  /* discover id */
  fd= open(pid_file, O_RDONLY);

559
  err= read(fd, buf, FN_REFLEN);
unknown's avatar
unknown committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

  close(fd);

  if (err > 0)
  {
    /* terminate string */
    if ((p= strchr(buf, '\n')) != NULL)
    {
      *p= '\0';

      /* check for a '\r' */
      if ((p= strchr(buf, '\r')) != NULL)
      {
        *p= '\0';
      }
    }
    else
    {
      buf[err]= '\0';
    }

    id= strtol(buf, NULL, 0);
  }

unknown's avatar
unknown committed
584 585 586 587 588
  return id;
}

/******************************************************************************

unknown's avatar
unknown committed
589 590 591
        kill_server()

        Force a kill of the server with the given pid.
unknown's avatar
unknown committed
592 593

******************************************************************************/
unknown's avatar
unknown committed
594

unknown's avatar
unknown committed
595 596 597 598 599 600 601 602 603 604
void kill_server(pid_t pid)
{
  if (pid > 0)
  {
#if !defined(__NETWARE__)
    /* Send SIGTERM to pid */
    kill(pid, SIGTERM);
#else /* __NETWARE__ */
    /* destroy vm */
    NXVmDestroy(pid);
unknown's avatar
unknown committed
605
#endif
unknown's avatar
unknown committed
606 607 608 609 610
  }
}
#endif
/******************************************************************************

unknown's avatar
unknown committed
611 612 613
        del_tree()

        Delete the directory and subdirectories.
unknown's avatar
unknown committed
614 615

******************************************************************************/
unknown's avatar
unknown committed
616

unknown's avatar
unknown committed
617 618 619
void del_tree(char *dir)
{
#ifndef __WIN__
unknown's avatar
unknown committed
620
  DIR *parent= opendir(dir);
unknown's avatar
unknown committed
621
  struct dirent *entry;
622
  char temp[FN_REFLEN];
unknown's avatar
unknown committed
623

unknown's avatar
unknown committed
624 625 626 627 628
  if (parent == NULL)
  {
    return;
  }

unknown's avatar
unknown committed
629
  while ((entry= readdir(parent)) != NULL)
unknown's avatar
unknown committed
630
  {
unknown's avatar
unknown committed
631
    /* create long name */
632
    snprintf(temp, FN_REFLEN, "%s/%s", dir, entry->d_name);
unknown's avatar
unknown committed
633 634 635

    if (entry->d_name[0] == '.')
    {
unknown's avatar
unknown committed
636
      /* Skip */
unknown's avatar
unknown committed
637
    }
unknown's avatar
unknown committed
638
    else
unknown's avatar
unknown committed
639
    {
640 641 642 643 644 645 646
/* FIXME missing test in acinclude.m4 */
#ifndef STRUCT_DIRENT_HAS_D_TYPE
      struct stat st;

      if (lstat(entry->d_name, &st) == -1)
      {
        /* FIXME error */
unknown's avatar
merge  
unknown committed
647
        return;  
648 649 650 651 652 653 654 655 656 657 658 659 660 661
      }
      if (S_ISDIR(st.st_mode))
#else
      if (S_ISDIR(entry->d_type))
#endif
      {
        /* delete subdirectory */
        del_tree(temp);
      }
      else
      {
        /* remove file */
        remove(temp);
      }
unknown's avatar
unknown committed
662 663
    }
  }
unknown's avatar
unknown committed
664
  /* remove directory */
unknown's avatar
unknown committed
665 666 667
  rmdir(dir);
#else
  struct _finddata_t parent;
unknown's avatar
unknown committed
668
#if defined(_MSC_VER) && _MSC_VER > 1200
unknown's avatar
unknown committed
669
  intptr_t handle;
unknown's avatar
unknown committed
670 671 672
#else
  long handle;
#endif  /* _MSC_VER && _MSC_VER > 1200  */ 
673 674
  char temp[FN_REFLEN];
  char mask[FN_REFLEN];
unknown's avatar
unknown committed
675

676
  snprintf(mask,FN_REFLEN,"%s/*.*",dir);
unknown's avatar
unknown committed
677 678 679 680 681 682 683 684

  if ((handle=_findfirst(mask,&parent)) == -1L)
  {
    return;
  }

  do
  {
unknown's avatar
unknown committed
685
    /* create long name */
686
    snprintf(temp, FN_REFLEN, "%s/%s", dir, parent.name);
unknown's avatar
unknown committed
687 688
    if (parent.name[0] == '.')
    {
unknown's avatar
unknown committed
689
      /* Skip */
unknown's avatar
unknown committed
690
    }
unknown's avatar
unknown committed
691
    else
unknown's avatar
unknown committed
692 693
    if (parent.attrib & _A_SUBDIR)
    {
unknown's avatar
unknown committed
694
      /* delete subdirectory */
unknown's avatar
unknown committed
695 696 697 698
      del_tree(temp);
    }
    else
    {
unknown's avatar
unknown committed
699
      /* remove file */
unknown's avatar
unknown committed
700 701 702 703 704 705
      remove(temp);
    }
  } while (_findnext(handle,&parent) == 0);

   _findclose(handle);

unknown's avatar
unknown committed
706
   /* remove directory */
unknown's avatar
unknown committed
707 708 709 710 711 712
   _rmdir(dir);
#endif
}

/******************************************************************************

unknown's avatar
unknown committed
713 714
        removef()

unknown's avatar
unknown committed
715
******************************************************************************/
unknown's avatar
unknown committed
716

unknown's avatar
unknown committed
717 718
int removef(const char *format, ...)
{
unknown's avatar
unknown committed
719
#ifdef __NETWARE__
unknown's avatar
unknown committed
720
  va_list ap;
721
  char path[FN_REFLEN];
unknown's avatar
unknown committed
722

unknown's avatar
unknown committed
723 724
  va_start(ap, format);

725
  vsnprintf(path, FN_REFLEN, format, ap);
unknown's avatar
unknown committed
726

unknown's avatar
unknown committed
727 728
  va_end(ap);
  return remove(path);
unknown's avatar
unknown committed
729

unknown's avatar
unknown committed
730
#elif __WIN__
unknown's avatar
unknown committed
731 732
  {
    va_list ap;
733
    char path[FN_REFLEN];
unknown's avatar
unknown committed
734
    struct _finddata_t parent;
unknown's avatar
unknown committed
735
#if defined(_MSC_VER) && _MSC_VER > 1200
unknown's avatar
unknown committed
736
    intptr_t handle;
unknown's avatar
unknown committed
737 738 739
#else
    long handle;
#endif  /* _MSC_VER && _MSC_VER > 1200  */ 
740
    char temp[FN_REFLEN];
unknown's avatar
unknown committed
741
    char *p;
unknown's avatar
unknown committed
742

unknown's avatar
unknown committed
743 744
    va_start(ap, format);

745
    vsnprintf(path, FN_REFLEN, format, ap);
unknown's avatar
unknown committed
746

unknown's avatar
unknown committed
747
    va_end(ap);
unknown's avatar
unknown committed
748 749

    p= path + strlen(path);
unknown's avatar
unknown committed
750 751 752 753
    while (*p != '\\' && *p != '/' && p > path) p--;

    if ((handle=_findfirst(path,&parent)) == -1L)
    {
unknown's avatar
unknown committed
754
      /* if there is not files....it's ok */
unknown's avatar
unknown committed
755 756 757
      return 0;
    }

unknown's avatar
unknown committed
758
    *p= '\0';
unknown's avatar
unknown committed
759 760 761 762 763

    do
    {
      if (! (parent.attrib & _A_SUBDIR))
      {
764
        snprintf(temp, FN_REFLEN, "%s/%s", path, parent.name);
unknown's avatar
unknown committed
765 766 767 768 769 770 771 772 773
        remove(temp);
      }
    }while (_findnext(handle,&parent) == 0);

    _findclose(handle);
  }
#else
  DIR *parent;
  struct dirent *entry;
774
  char temp[FN_REFLEN];
unknown's avatar
unknown committed
775
  va_list ap;
776
  char path[FN_REFLEN];
unknown's avatar
unknown committed
777
  char *p;
unknown's avatar
unknown committed
778
  /* Get path with mask */
unknown's avatar
unknown committed
779 780
  va_start(ap, format);

781
  vsnprintf(path, FN_REFLEN, format, ap);
unknown's avatar
unknown committed
782

unknown's avatar
unknown committed
783
  va_end(ap);
unknown's avatar
unknown committed
784 785

  p= path + strlen(path);
unknown's avatar
unknown committed
786
  while (*p != '\\' && *p != '/' && p > path) p--;
unknown's avatar
unknown committed
787
  *p= '\0';
unknown's avatar
unknown committed
788
  p++;
unknown's avatar
unknown committed
789 790

  parent= opendir(path);
unknown's avatar
unknown committed
791 792 793

  if (parent == NULL)
  {
unknown's avatar
unknown committed
794
    return 1;            /* Error, directory missing */
unknown's avatar
unknown committed
795
  }
unknown's avatar
unknown committed
796 797

  while ((entry= readdir(parent)) != NULL)
unknown's avatar
unknown committed
798
  {
unknown's avatar
unknown committed
799
    /* entry is not directory and entry matches with mask */
800 801 802
#ifndef STRUCT_DIRENT_HAS_D_TYPE
    struct stat st;

unknown's avatar
merge  
unknown committed
803 804 805 806
    /* create long name */
    snprintf(temp, FN_REFLEN, "%s/%s", path, entry->d_name);

    if (lstat(temp, &st) == -1)
807
    {
unknown's avatar
merge  
unknown committed
808
      return 1;  /* Error couldn't lstat file */
809 810 811 812
    }

    if (!S_ISDIR(st.st_mode) && !fnmatch(p, entry->d_name,0))
#else
unknown's avatar
unknown committed
813
    if (!S_ISDIR(entry->d_type) && !fnmatch(p, entry->d_name,0))
814
#endif
unknown's avatar
unknown committed
815
    {
unknown's avatar
unknown committed
816
      /* create long name */
817
      snprintf(temp, FN_REFLEN, "%s/%s", path, entry->d_name);
unknown's avatar
unknown committed
818
      /* Delete only files */
unknown's avatar
unknown committed
819 820 821 822
      remove(temp);
    }
  }
#endif
unknown's avatar
unknown committed
823
  return 0;
unknown's avatar
unknown committed
824 825 826 827
}

/******************************************************************************

unknown's avatar
unknown committed
828 829
        get_basedir()

unknown's avatar
unknown committed
830
******************************************************************************/
unknown's avatar
unknown committed
831

unknown's avatar
unknown committed
832 833
void get_basedir(char *argv0, char *basedir)
{
834
  char temp[FN_REFLEN];
unknown's avatar
unknown committed
835 836
  char *p;
  int position;
unknown's avatar
unknown committed
837

unknown's avatar
unknown committed
838 839 840 841
  ASSERT(argv0 != NULL);
  ASSERT(basedir != NULL);

  strcpy(temp, strlwr(argv0));
unknown's avatar
unknown committed
842 843 844
  while ((p= strchr(temp, '\\')) != NULL) *p= '/';

  if ((position= strinstr(temp, "/bin/")) != 0)
unknown's avatar
unknown committed
845
  {
unknown's avatar
unknown committed
846 847
    p= temp + position;
    *p= '\0';
unknown's avatar
unknown committed
848 849 850 851 852 853 854
    strcpy(basedir, temp);
  }
}

uint strinstr(reg1 const char *str,reg4 const char *search)
{
  reg2 my_string i,j;
unknown's avatar
unknown committed
855
  my_string start= (my_string) str;
unknown's avatar
unknown committed
856 857 858 859 860 861

 skipp:
  while (*str != '\0')
  {
    if (*str++ == *search)
    {
unknown's avatar
unknown committed
862 863
      i=(my_string) str;
      j= (my_string) search+1;
unknown's avatar
unknown committed
864
      while (*j)
unknown's avatar
unknown committed
865
        if (*i++ != *j++) goto skipp;
unknown's avatar
unknown committed
866 867 868 869 870 871 872 873
      return ((uint) (str - start));
    }
  }
  return (0);
}

/******************************************************************************

unknown's avatar
unknown committed
874 875
        remove_empty_file()

unknown's avatar
unknown committed
876
******************************************************************************/
unknown's avatar
unknown committed
877

unknown's avatar
unknown committed
878 879 880
void remove_empty_file(const char *file_name)
{
  struct stat file;
unknown's avatar
unknown committed
881

unknown's avatar
unknown committed
882 883 884 885 886 887
  if (!stat(file_name,&file))
  {
    if (!file.st_size)
      remove(file_name);
  }
}