Skip to content

Commit e2a73dc

Browse files
committed
No need to fork, just use rindex to find the name
1 parent c6847fb commit e2a73dc

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

cxc.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdio.h>
1212
#include <stdlib.h>
1313
#include <string.h>
14+
#include <strings.h>
1415
#include <unistd.h>
1516

1617
#include "cx.h"
@@ -212,12 +213,8 @@ main(int argc, char **argv)
212213
return 0;
213214
}
214215

215-
if (pflag) {
216-
if (fork()) // Fork to avoid waiting
217-
return 0;
218-
else
219-
return push_path(pushpath);
220-
}
216+
if (pflag)
217+
return push_path(pushpath);
221218

222219
if (id == -1) {
223220
get_match(argv[argc - 1], match, sizeof(match));
@@ -371,9 +368,6 @@ static int
371368
push_path(char *path)
372369
{
373370
sqlite3_stmt *stmt;
374-
unsigned i, j;
375-
char name[256];
376-
memset(name, 0, sizeof(name));
377371

378372
if (access(path, X_OK) != 0)
379373
return 1;
@@ -385,16 +379,9 @@ push_path(char *path)
385379
sqlite3_bind_int(stmt, 1, id);
386380
} else {
387381
/* the entry doesn't exist, parse the name from the path and add to db */
388-
j = 0;
389-
for (i = 0; path[i] != '\0' && j < sizeof(name) - 1; i++) {
390-
if (path[i] == '/')
391-
j = 0;
392-
else
393-
name[j++] = path[i];
394-
}
395-
name[j] = '\0';
382+
char *name = rindex(path, '/') + 1;
396383

397-
/* now that we've got the name, insert the new entry to sqlite */
384+
/* insert the new entry to sqlite */
398385
stmt = cdbq[DB_INSERT];
399386
sqlite3_bind_text(stmt, 1, path, strlen(path), SQLITE_STATIC);
400387
sqlite3_bind_text(stmt, 2, name, strlen(name), SQLITE_STATIC);

0 commit comments

Comments
 (0)