FOSSology  4.4.0
Open Source License Compliance by Open Source Software
util.c File Reference

misc utilites More...

#include <stdarg.h>
#include <stdio.h>
#include "nomos.h"
#include "util.h"
#include "list.h"
#include "nomos_regex.h"
#include "nomos_utils.h"
Include dependency graph for util.c:

Go to the source code of this file.

Macros

#define MM_CACHESIZE   20
 MM Cache size.
 
#define MAXLENGTH   100
 Buffer length.
 

Functions

int isDIR (char *dpath)
 Check if given path is a directory. More...
 
int isEMPTYFILE (char *fpath)
 Check if given file is empty. More...
 
int isBLOCK (char *bpath)
 Check if given path is a Block device. More...
 
int isCHAR (char *cpath)
 Check if given path is a character device. More...
 
int isPIPE (char *ppath)
 Check if given path is a pipe. More...
 
int isSYMLINK (char *spath)
 Check if given path is a symbolic link. More...
 
int isINODE (char *ipath, int typ)
 Check for a inode against a flag. More...
 
char * newReloTarget (char *basename)
 Check if a relocation target is accessible. More...
 
char * findBol (char *s, char *upperLimit)
 Find Begin of Line in a string. More...
 
char * findEol (char *s)
 Find first ROL in a string. More...
 
void renameInode (char *oldpath, char *newpath)
 Rename an inode at oldpath to newpath. More...
 
void chmodInode (char *pathname, int mode)
 Change inode mode bits. More...
 
FILE * fopenFile (char *pathname, char *mode)
 Open a file and return the file pointer. More...
 
FILE * popenProc (char *command, char *mode)
 Open a process pipe using popen() More...
 
char * wordCount (char *textp)
 VERY simple line count, does NOT have to be perfect! More...
 
char * copyString (char *s, char *label)
 Create a copy of a string. More...
 
char * pathBasename (char *path)
 Get the basename from a file path. More...
 
char * getInstances (char *textp, int size, int nBefore, int nAfter, char *regex, int recordOffsets)
 Get occurrence of a regex in a given string pointer.
 
char * curDate ()
 Get the current date. More...
 
void makeSymlink (char *path)
 Create symbolic links for a given path in current directory. More...
 
void printRegexMatch (int n, int cached)
 CDB – Need to review this code, particularly for the use of an external file (Nomos.strings.txt). Despite the fact that variable is named debugStr, the file appears to be used for more than just debugging. More...
 
void ReplaceNulls (char *Buffer, int BufferSize)
 Replace all nulls in Buffer with blanks. More...
 
char * mmapFile (char *pathname)
 Blarg. Files that are EXACTLY a multiple of the system pagesize do not get a NULL on the end of the buffer. We need something creative, else we'll need to just calloc() the size of the file (plus one) and read() the whole thing into memory.
 
void mmapOpenListing ()
 
void munmapFile (void *ptr)
 
int bufferLineCount (char *p, int len)
 Finds the length of first line in a buffer. More...
 
void appendFile (char *pathname, char *str)
 Append a string at the end of the file. More...
 
int mySystem (const char *fmt,...)
 Run a system command. More...
 
int isFILE (char *pathname)
 Check if an inode is a file. More...
 
int addEntry (char *pathname, int forceFlag, const char *fmt,...)
 adds a line to the specified pathname More...
 
void Msg (const char *fmt,...)
 DO NOT automatically add
to a string passed to Msg(); in parseDistro, we sometimes want to dump a partial line.
 
void Assert (int fatalFlag, const char *fmt,...)
 Raise an assert. More...
 
void traceFunc (char *fmtStr,...)
 

Variables

static va_list ap
 
static char utilbuf [myBUFSIZ]
 
static struct mm_cache mmap_data [MM_CACHESIZE]
 
static char cmdBuf [512]
 

Detailed Description

misc utilites

Version
"$Id: util.c 4032 2011-04-05 22:16:20Z bobgo $"

Definition in file util.c.

Function Documentation

◆ addEntry()

int addEntry ( char *  pathname,
int  forceFlag,
const char *  fmt,
  ... 
)

adds a line to the specified pathname

Adds a line to the specified pathname if either:

  • the line does NOT already exist in the line, or
  • the variable 'forceFlag' is set to non-zero

Definition at line 1358 of file util.c.

◆ appendFile()

void appendFile ( char *  pathname,
char *  str 
)

Append a string at the end of the file.

Parameters
pathnamePath to the file
strString to be appended

Definition at line 1283 of file util.c.

◆ Assert()

void Assert ( int  fatalFlag,
const char *  fmt,
  ... 
)

Raise an assert.

Parameters
fatalFlagSet to TRUE if assert is fatal. Function will BAIL
fmtAssert format

Definition at line 1395 of file util.c.

◆ bufferLineCount()

int bufferLineCount ( char *  p,
int  len 
)

Finds the length of first line in a buffer.

Function traverse the buffer and breaks at the first occurance of EOL or NULL_CHAR

Parameters
pBuffer to look into
lenUpper limit in p
Returns
Length of the line

Definition at line 1253 of file util.c.

◆ chmodInode()

void chmodInode ( char *  pathname,
int  mode 
)

Change inode mode bits.

Note
The function prints error and call Bail() if chmod() fails
Parameters
pathnamePath of the inode
modeNew mode bits (e.g. 0660)

Definition at line 450 of file util.c.

◆ copyString()

char* copyString ( char *  s,
char *  label 
)

Create a copy of a string.

Parameters
sString to be copied
labelLabel on the new memory
Returns
New string
See also
memAlloc()

Definition at line 593 of file util.c.

◆ curDate()

char* curDate ( )

Get the current date.

Note
The function prints a fatal log and call Bail() if ctime_r() fails
Returns
Current date

Definition at line 913 of file util.c.

◆ findBol()

char* findBol ( char *  s,
char *  upperLimit 
)

Find Begin of Line in a string.

The function starts from the starting position and tracks backward till a EOL is hit.

Parameters
sStarting position in string to scan
upperLimitUpper limit of the string
Returns
- Location of BOL if found
  • If we hit upperLimit, return upper limit
  • NULL otherwise

Definition at line 340 of file util.c.

◆ findEol()

char* findEol ( char *  s)

Find first ROL in a string.

Parameters
sStarting position in a string
Returns
- Position of first EOL hit
  • Last element of the string if EOL not found
  • NULL otherwise

Definition at line 378 of file util.c.

◆ fopenFile()

FILE* fopenFile ( char *  pathname,
char *  mode 
)

Open a file and return the file pointer.

Note
The function prints error and call Bail() on failure
Parameters
pathnamePath of the file to open
modeMode in which the file should be opened
Returns
File pointer

Definition at line 476 of file util.c.

◆ isBLOCK()

int isBLOCK ( char *  bpath)

Check if given path is a Block device.

Parameters
bpathPath to check
Returns
True if path is a block device, false otherwise
See also
isINODE()

Definition at line 83 of file util.c.

◆ isCHAR()

int isCHAR ( char *  cpath)

Check if given path is a character device.

Parameters
cpathPath to check
Returns
True if path is a character device, false otherwise
See also
isINODE()

Definition at line 98 of file util.c.

◆ isDIR()

int isDIR ( char *  dpath)

Check if given path is a directory.

Parameters
dpathPath to check
Returns
True if path is a directory, false otherwise

Definition at line 50 of file util.c.

◆ isEMPTYFILE()

int isEMPTYFILE ( char *  fpath)

Check if given file is empty.

Parameters
fpathPath of file to check
Returns
True if file is empty, false otherwise
See also
isFILE()

Definition at line 65 of file util.c.

◆ isFILE()

int isFILE ( char *  pathname)

Check if an inode is a file.

Parameters
pathnamePath to check
Returns
True if it is a file, false otherwise

Definition at line 1340 of file util.c.

◆ isINODE()

int isINODE ( char *  ipath,
int  typ 
)

Check for a inode against a flag.

Parameters
ipathPath of inode
typFlag to check against to
Returns
Return true if inode matches with the flag, false otherwise

Definition at line 143 of file util.c.

◆ isPIPE()

int isPIPE ( char *  ppath)

Check if given path is a pipe.

Parameters
ppathPath to check
Returns
True if path is a pipe, false otherwise
See also
isINODE()

Definition at line 113 of file util.c.

◆ isSYMLINK()

int isSYMLINK ( char *  spath)

Check if given path is a symbolic link.

Parameters
spathPath to check
Returns
True if path is a symbolic link, false otherwise
See also
isINODE()

Definition at line 128 of file util.c.

◆ makeSymlink()

void makeSymlink ( char *  path)

Create symbolic links for a given path in current directory.

Parameters
pathPath to the inode

Definition at line 960 of file util.c.

◆ munmapFile()

void munmapFile ( void *  ptr)
Warning
do NOT use a string/buffer AFTER calling munmapFile()!!!
We don't explicitly zero out the memory, but apparently glibc DOES.

Definition at line 1197 of file util.c.

◆ mySystem()

int mySystem ( const char *  fmt,
  ... 
)

Run a system command.

Parameters
fmtThe command to run along with parameters
Note
The function will log errors if and error occurs
Returns
The return code from the command.

Definition at line 1303 of file util.c.

◆ newReloTarget()

char* newReloTarget ( char *  basename)

Check if a relocation target is accessible.

Parameters
basenameBase name of the original target
Returns
Path of the new target, NULL on failure

Definition at line 175 of file util.c.

◆ pathBasename()

char* pathBasename ( char *  path)

Get the basename from a file path.

Parameters
pathPath to the file
Returns
Basename start pointer on the path

Definition at line 615 of file util.c.

◆ popenProc()

FILE* popenProc ( char *  command,
char *  mode 
)

Open a process pipe using popen()

Note
The function prints error and call Bail() on failure
Parameters
commandCommand to open a pipe to
modeMode of the pipe
Returns
File pointer to the opened pipe

Definition at line 521 of file util.c.

◆ printRegexMatch()

void printRegexMatch ( int  n,
int  cached 
)

CDB – Need to review this code, particularly for the use of an external file (Nomos.strings.txt). Despite the fact that variable is named debugStr, the file appears to be used for more than just debugging.

Although it might be the case that it only gets called from debug code. It does not appear to be called during a few test runs of normal file scans that I tried.

Definition at line 986 of file util.c.

◆ renameInode()

void renameInode ( char *  oldpath,
char *  newpath 
)

Rename an inode at oldpath to newpath.

The functions calls rename() first. If it fails, another try is done using mv command.

Note
If everything fails, function prints an error and call Bail()
Parameters
oldpathOriginal path to be renamed
newpathNew path of the inode

Definition at line 409 of file util.c.

◆ ReplaceNulls()

void ReplaceNulls ( char *  Buffer,
int  BufferSize 
)

Replace all nulls in Buffer with blanks.

Parameters
BufferThe data having its nulls replaced.
BufferSizeBuffer size

Definition at line 1068 of file util.c.

◆ wordCount()

char* wordCount ( char *  textp)

VERY simple line count, does NOT have to be perfect!

Parameters
textpPointer to string
Returns
Number of lines found

Definition at line 551 of file util.c.