bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

argve0, psfool


From: Daniel C. Bastos
Subject: argve0, psfool
Date: Mon, 24 Dec 2007 13:29:47 -0500

I always miss these two programs on every system I meet. argv0 is very
handy when dealing with programs that care about argv[0] and psfool is
essential when giving out passwords through the command line. I figure
these two should be in coreutils.

/* psfool - formats argv[] with enough bytes to go beyond ps' reach.
   Copyright (C) 2007 Free Software Foundation, Inc.

   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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   */

/* Daniel C. Bastos <address@hidden> */

#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>

#include "system.h"
#include "error.h"
#include "long-options.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "psfool"

#define AUTHORS "Daniel C. Bastos"

/* The name this program was run with. */
char *program_name;

#define min(a,b) (a) <= (b) ? (a) : (b)
#define SIZE 256

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);
  else
    {
      printf (_("\
Usage: %s fake real [arg1 arg2 ... argn]\n\
"),
              program_name, program_name);

      fputs (_("\
Runs `real' fooling ps into thinking it was `fake'\n\
\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);
}

void run(char *argv1, char **argv, char **envp)
{
    execve(argv1, argv, envp);
}

int
main (int argc, char **argv, char **envp)
{
  int i; char real[128]; char fake[128]; char vargv[8192]; int n;

  initialize_main (&argc, &argv);
  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);

  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                           usage, AUTHORS, (char const *) NULL);
  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
    usage (EXIT_FAILURE);

  if (argc < 2)
    usage(EXIT_FAILURE);


  if (strlen(argv[1]) >= (sizeof fake - 1)) {
    error (EXIT_FAILURE, 0, _("fake is too long"), argv[1]);
  }

  if (strlen(argv[2]) >= (sizeof real - 1)) {
    error (EXIT_FAILURE, 0, _("real is too long"), argv[1]);
  }

  /* copies (argv[1] = fake) into fake[] */
  n = min(sizeof fake - 1, strlen(argv[1])); 
  strncpy(fake, argv[1], n); fake[n] = '\0';

  /* copies (argv[2] = real) into real[] */
  n = min(sizeof real - 1, strlen(argv[2])); 
  strncpy(real, argv[2], n); real[n] = '\0';

  memset(vargv, ' ', SIZE); /* format vector argv[] with spaces */
  
  /* put fake into vargv[] */
  strncpy(vargv, fake, strlen(fake)); 
  
  /* put real into argv[] */
  strncpy(vargv + SIZE, real, min(sizeof vargv - SIZE, strlen(real)));

  argv[2] = vargv; 

  run(real, argv + 2, envp); 

  error (EXIT_FAILURE, 0, _("Could not run %s"), argv[1]);
}

/* argv0 - run program with argv[0] as specified by user
   Copyright (C) 2007 Free Software Foundation, Inc.

   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, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   */

/* Daniel C. Bastos <address@hidden> */

#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>

#include "system.h"
#include "error.h"
#include "long-options.h"

/* The official name of this program (e.g., no `g' prefix).  */
#define PROGRAM_NAME "argv0"

#define AUTHORS "Daniel C. Bastos"

/* The name this program was run with. */
char *program_name;

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
         program_name);
  else
    {
      printf (_("\
Usage: %s program argv0 [arg1 arg2 ... argn]\n\
"),
              program_name, program_name);

      fputs (_("\
Runs program with argv[0] set to argv0 and args as other arguments\n\
\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    }
  exit (status);
}

void run(char *argv1, char **argv, char **envp)
{
    execve(argv1, argv, envp);
}

int
main (int argc, char **argv, char **envp)
{
  initialize_main (&argc, &argv);
  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdout);

  parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
                           usage, AUTHORS, (char const *) NULL);
  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
    usage (EXIT_FAILURE);

  if (argc < 2)
    usage(EXIT_FAILURE);

  run(argv[1] , argv + 2, envp);

  error (EXIT_FAILURE, 0, _("Could not run %s"), argv[1]);
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]