Sponsored Content
Top Forums UNIX for Advanced & Expert Users Retrieving command line arguments of a particular PID Post 302252171 by Andrewkl on Tuesday 28th of October 2008 10:05:38 PM
Old 10-28-2008
hi,

Thank you for the tip.

I am able to read in the contents of the "cmdline" file into a variable,
but when I try to print out the string, the output shows only characters before
the first null character.

How do I get the other characters after the first null character.
You suggested that i use pointer arithmetic, but how do i'm not sure
how to get the index of the '\0'.

I used strtok(), but it stops processing after the first null.

my code is below.
-------------------------------------------------

% cd /proc/14007
% cat cmdline
emacs--background-colorblack/tmp/abc

---------------------------

% ps -e -o pid,args | grep 14007
14007 emacs --background-color black /tmp/abc

---------------------------------------

% a.out /proc/14007/cmdline
file length is: 0
content is [emacs] <<<<<----- everything after first null character is not shown
strlen: 5
1: emacs
Tokenizing complete


==============================================================
Code:
#include <stdio.h>


main (int argc, char *argv[])
{
  FILE *fp;
  char *tok;
  int x = 1;
                  
  long  lFileLen;               /* Length of file */
  char *contents;                  /* Dynamically allocated buffer (entire file) */

  if (argc < 2) {
     printf("need a filename\n");
     exit(1);
  }

  fp = fopen(argv[1], "r"); /* Open in BINARY mode */
  if (fp == NULL) {
      printf("could not open file\n");
      exit(2);
  }

  fseek(fp, 0L, SEEK_END);  /* Position to end of file */
  lFileLen = ftell(fp);     /* Get file length */
  rewind(fp);               /* Back to start of file */

  printf("file length is: %ld\n", lFileLen);
 
  lFileLen = 200; /* assume there are 200 bytes at most */
  
  contents = (char*)calloc(lFileLen + 1, sizeof(char));

  if(contents == NULL )
  {
    printf("\nInsufficient memory to read file.\n");
    return 0;
  }

  /* Read the entire file into contents */
  fread(contents, lFileLen, 1, fp); 
  
  fclose(fp);

  printf("content is [%s]\n", contents);  /* nothing after first null character !!! */
  printf("strlen: %d\n", strlen(contents)); 
  
   /* extract first string from string sequence */
  tok = (char*)strtok(contents, "\0");

  /* print first string after tokenized */
  printf("%i: %s\n", x, tok);

  /* loop until finishied */
  while (1) 
  { 
          /* extract string from string sequence */
          tok = (char*)strtok(NULL, "\0");

          /* check if there is nothing else to extract */
          if (tok == NULL)
          {
                  printf("Tokenizing complete\n");
               /*   exit(0); */
                  break;
          } 

          /* print string after tokenized */
          printf("/%i: %s\n", x, tok);
          x++;
  } 


  free(contents);

}


Last edited by Andrewkl; 10-29-2008 at 01:40 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Parsing the command line arguments

Is there a way to get the command line arguments. I am using getopt(3) but if the arguments are more than one for a particular option than it just ignores the second argument. For eg ./a.out -x abc def now abd will be got with -x using getopt "( x : )" and string abc\0def will get stored... (7 Replies)
Discussion started by: jayakhanna
7 Replies

2. Programming

command line arguments

Hi How to pass multi line text as a command line argument to a program. (i.e) ./a.out hi this is sample 0 file1 where hi this is sample should be stored in argv 0 in argv and so on... (3 Replies)
Discussion started by: bankpro
3 Replies

3. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies

4. UNIX for Dummies Questions & Answers

Retrieving PID from a file

Hello I need to retrieve the content of a file in the shell script file(.sh file). I store the Process ID of the a process in file.Only the PID is available in that file. Inside the shell script i want to retireve the content(PID) and need to check for the existence of the Process.Basically... (5 Replies)
Discussion started by: appleforme1415
5 Replies

5. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

6. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

7. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

8. Shell Programming and Scripting

Command Line Arguments - not working

I am new to the world of Unix and shell scripting and have been trying to get the following simple script to work: #!/bin/bash echo "what is your age?" echo "you are $1 years old"I want to be able to enter my age on the command line, when prompted, and it return the... (1 Reply)
Discussion started by: meursault
1 Replies

9. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 Replies

10. UNIX for Beginners Questions & Answers

Loop with command line arguments

Ubuntum, Bash version: 4.3.46 Hi, how can I create a loop where the command line arguments change (increase) and every time the number of arguments is different ? ### I have many gene names... that mean gene1=$2, gene2=$3, ...... geneN=$N+1 ### some time the number of gene is 25, other... (7 Replies)
Discussion started by: echo manolis
7 Replies
RESIZE(1)						      General Commands Manual							 RESIZE(1)

NAME
resize - set TERMCAP and terminal settings to current xterm window size SYNOPSIS
resize [ -u | -c ] [ -s [ row col ] ] DESCRIPTION
Resize prints a shell command for setting the TERM and TERMCAP environment variables to indicate the current size of xterm window from which the command is run. For this output to take effect, resize must either be evaluated as part of the command line (usually done with a shell alias or function) or else redirected to a file which can then be read in. From the C shell (usually known as /bin/csh), the follow- ing alias could be defined in the user's .cshrc: % alias rs 'set noglob; eval `resize`' After resizing the window, the user would type: % rs Users of versions of the Bourne shell (usually known as /bin/sh) that don't have command functions will need to send the output to a tempo- rary file and the read it back in with the ``.'' command: $ resize > /tmp/out $ . /tmp/out OPTIONS
The following options may be used with resize: -u This option indicates that Bourne shell commands should be generated even if the user's current shell isn't /bin/sh. -c This option indicates that C shell commands should be generated even if the user's current shell isn't /bin/csh. -s [rows columns] This option indicates that Sun console escape sequences will be used instead of the VT100-style xterm escape codes. If rows and columns are given, resize will ask the xterm to resize itself. However, the window manager may choose to disallow the change. Note that the Sun console escape sequences are recognized by XFree86 xterm and by dtterm. The resize program may be installed as sunsize, which causes makes it assume the -s option. The rows and columns arguments must appear last; though they are normally associated with the -s option, they are parsed sepa- rately. FILES
/etc/termcap for the base termcap entry to modify. ~/.cshrc user's alias for the command. SEE ALSO
csh(1), tset(1), xterm(1) AUTHORS
Mark Vandevoorde (MIT-Athena), Edward Moy (Berkeley) Copyright (c) 1984, 1985 by X Consortium See X() for a complete copyright notice. X Window System RESIZE(1)
All times are GMT -4. The time now is 11:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy