Hi,

On Mon, Mar 19, 2012 at 6:32 PM, Jiri Svoboda <[email protected]> wrote:
> > I saw
> > that there is a 'struct posix_stat' in uspace/lib/posix/sys/stat.h, and
> > corresponding posix_stat() and posix_fstat() functions
> > in uspace/lib/posix/sys/stat.c. Is this 'struct posix_stat' meant to be
> > used in some way ?
> These are only for ported applications that make use of out POSIX 
> compatibility library that provides a limited level of POSIX-like C library 
> (we use it for PCC and GNU binutils). For these posix_xxx gets mapped to xxx 
> via preprocessor macros. Nevertheless since the OS does not suport *time 
> attributes, using libposix will not help you, it does not have any way of 
> getting these attributes now.

Thanks for the clarification. I guess porting GCC to HelenOS will also
require libposix and since GNU binutils have already been ported, the
work will be somewhat easier in the sense one can have a good
headstart. Complete port of GCC may also require changes and addition
in libposix and binutils. This is all completely based on guesswork
and requires closer inspection to determine the actual implementation
details, which i intend do from now onward. Any pointers and
suggestions regarding it will be very welcome :-)

I have attached a patch of the touch.c file, where i have added -c option.

Thanks,
Vivek Prakash
=== modified file 'uspace/app/bdsh/cmds/modules/touch/touch.c'
--- uspace/app/bdsh/cmds/modules/touch/touch.c	2011-06-17 16:00:53 +0000
+++ uspace/app/bdsh/cmds/modules/touch/touch.c	2012-03-19 14:24:08 +0000
@@ -36,6 +36,8 @@
 #include <dirent.h>
 #include <sys/types.h>
 #include <str.h>
+#include <getopt.h>
+#include <sys/stat.h>
 
 #include "config.h"
 #include "errors.h"
@@ -46,6 +48,11 @@
 
 static const char *cmdname = "touch";
 
+static struct option const long_options[] = {
+	{ "no-create", no_argument, 0, 'c' },
+	{ 0, 0, 0, 0 }
+};
+
 /* Dispays help for touch in various levels */
 void help_cmd_touch(unsigned int level)
 {
@@ -53,8 +60,13 @@
 		printf("`%s' updates access times for files\n", cmdname);
 	} else {
 		help_cmd_touch(HELP_SHORT);
-		printf("  `%s' <file>, if the file does not exist it will be "
-				"created\n", cmdname);
+		printf(
+		"Usage: %s <file>\n"
+		"If the file does not exist it will be created empty, unless -c is "
+		"supplied.\n"
+		"Options:\n"
+		"   -c, --no-create	 Do not create new files\n",
+		cmdname);
 	}
 
 	return;
@@ -64,6 +76,9 @@
 int cmd_touch(char **argv)
 {
 	unsigned int argc, i = 0, ret = 0;
+	int c, opt_ind;
+	bool cflag;
+	struct stat file_stat;
 	int fd;
 	char *buff = NULL;
 
@@ -71,13 +86,27 @@
 
 	argc = cli_count_args(argv);
 
-	if (argc == 1) {
+	fd = -1;
+	cflag = false;
+
+	for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
+		c = getopt_long(argc, argv, "c", long_options, &opt_ind);
+		switch (c) {
+		case 'c':
+			cflag = true;
+			break;
+		}
+	}
+	
+	argc -= optind;
+
+	if (argc < 1) {
 		printf("%s - incorrect number of arguments. Try `help %s extended'\n",
 			cmdname, cmdname);
 		return CMD_FAILURE;
 	}
 
-	for (i = 1; i < argc; i ++) {
+	for (i = optind; argv[i] != NULL; i++) {
 		buff = str_dup(argv[i]);
 		dirp = opendir(buff);
 		if (dirp) {
@@ -87,13 +116,19 @@
 			continue;
 		}
 
-		fd = open(buff, O_RDWR | O_CREAT);
+		/* Check whether file exists if --no-create option is given */
+		if (!cflag || (cflag && !stat(argv[i], &file_stat))) {
+			fd = open(buff, O_RDWR | O_CREAT);
+		}
+
 		if (fd < 0) {
 			cli_error(CL_EFAIL, "Could not update / create %s ", buff);
 			ret ++;
 			continue;
-		} else
+		} else {
 			close(fd);
+			fd = -1;
+		}
 
 		free(buff);
 	}
_______________________________________________
HelenOS-devel mailing list
[email protected]
http://lists.modry.cz/cgi-bin/listinfo/helenos-devel

Reply via email to