Einlesen von /proc/<pid>/cmdline [solved]
-
Hallo zusammen,
ich habe ein Problem mit dem Auslesen der vollständigen Zeile von /proc/<pid>/cmdline. Es wird immer nur der Anfang gelesen.
Ausgabe von cat:
> cat /proc/<pid>/cmdline
> ssh-L7000:btnn.de:25mein@serverAusgabe meines Programms
> ./lesen /proc/<pid>/cmdline
> sshIch möchte jedoch die vollständige Zeile einlesen.
Mit /proc/<pid>/status funktioniert alles.
Hier mein verwendeter C Code.
/* lesen.c */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dirent.h> #include <dirent.h> #include <regex.h> #include <string.h> int main(int argc, char *argv[]) { int i = 0; FILE *fz; char buffer[256]; memset (buffer, 0, 256); fz = fopen(argv[1], "r"); fgets(buffer, 256, fz); fprintf(stdout, "%s", buffer); fclose(fz); fz = NULL; return EXIT_SUCCESS; }
Vielen Dank für euren Support.
Gruß
-
man proc:
[c]
/proc/[pid]/cmdline
This holds the complete command line for the process, unless
the process is a zombie. In the latter case, there is noth‐
ing in this file: that is, a read on this file will return 0
characters. The command-line arguments appear in this file
as a set of null-separated strings, with a further null byte
('\0') after the last string.
[/c]printf gibt nur bis zu ersten null aus.
-
Danke für die Antwort!
Hier meine Lösung für das Problem.
char buffer[256]; while( ((i=fgetc(fz)) != EOF) || (c > 255)) { if(i!=0) { buffer[c]=(char)i; c++; } }
-
Ich würde das besser so realisieren
#include <stdio.h> #include <string.h> int main() { FILE *fh = fopen("/proc/self/cmdline", "r"); if(!fh) { perror("fopen"); return 1; } enum { BufferSize = 2048 }; char buffer[BufferSize]; while( fgets(buffer, BufferSize, fh) ) { size_t n = 0; while( n < BufferSize && buffer[n] != '\0' ) { printf("%s\n", buffer+n); n += strlen(buffer+n) + 1; } } fclose(fh); }
-
Hallo rüdiger,
dein Programm gefällt mir ebenfalls besser. Werde deine Umsetzung in meinen Programmcode aufnehmen.
Schönes Forum! Werde mich hier öfter blicken lassen.
Gruß